Reputation: 141
I have developed a website with j2ee. I had no problem with character encoding on my development environment but when i deploy my app to openshift (jboss 7, mysql 5.5) UTF formatting seems to be not working. I tried logging on server side and seems like it's jboss environmental issue as i can see the servlet can not decode the chars. Please find the code for front/backend below, after the codes, i list what i have already tried.
Thanks,
<form role="form" id="createadform" name="createadform" action="createads" method="POST" enctype="multipart/form-data">
<div class="row" >
<div class="col-md-6 col-md-offset-3" >
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Aşağıdaki formu doldurarak ilanınızı oluşturabilirsiniz.</h3>
</div>
<div class="panel-body ">
<div class="col-md-6" >
<div class="form-group has-feedback" style="width: 100%">
<label for="isim">Başlık (En fazla 25 karakter)</label>
<input type="text" class="form-control" id="headline" name="headline" maxlength="25" required>
</div>
<div class="form-group has-feedback">
<label for="minSalary">Maaş Alt Sınırı</label>
<input type="number" class="form-control" id="minsalary" name="minsalary" required>
</div>
<div class="form-group has-feedback">
<label for="maxSalary">Maaş Üst Sınırı</label>
<input type="number" class="form-control" id="maxsalary" name="maxsalary" required>
</div>
<div class="form-group has-feedback">
<label for="currency">Para Birimi</label>
<select class="form-control" id="currency" name="currency">
<c:forEach var="currency" items="${currencies}">
<option name="currency" value=${currency.currencyid}>${currency.currencydescription} </option>
</c:forEach>
</select>
</div>
<div class="form-group has-feedback" >
<label for="city">Şehir</label>
<select class="form-control" id="city" name="city">
<c:forEach var="city" items="${cities}">
<option name="city" value=${city.plateno}>${city.name} </option>
</c:forEach>
</select>
</div>
<div class="form-group has-feedback">
<label for="worktype">İş tipi</label>
<select class="form-control" id="worktype" name="worktype">
<c:forEach var="worktype" items="${worktypes}">
<option name="worktype" value=${worktype.worktypeid}>${worktype.name} </option>
</c:forEach>
</select>
</div>
<div class="col-md-6 text-center">
<input type="file" name="resim" id="resim" accept="image/*"/>
</div>
</div>
<div class="col-md-6" >
<div class="form-group has-feedback">
<label for="freetext">İş Tanımı</label>
<textarea class="form-control" style="height: 300px;" id="freetext" name="freetext"></textarea>
</div>
<button type="submit" style="float: right; " class="btn btn-info ">Kaydı oluştur</button>
</div>
</div>
</div>
</div>
</div>
</form>
I also have a javascript for file extension validation :
<script>
$("#createadform").submit(function(e){
e.preventDefault();
var resim = $('resim');
var headline = $('headline');
var minsalary = $('minsalary');
var maxsalary = $('maxsalary');
var currency = $('currency');
var city = $('city');
var worktype = $('worktype');
var freetext = $('freetext');
if ($('#resim').hasExtension(['.jpg', '.jpeg', '.bmp', '.gif', '.png'])) {
this.submit();
}else{
alert("Seçeceğiniz resim için 'jpg, jpeg,bmp,gif ve png' formatlarını kullanabilirsiniz.");
}
});
</script>
My servlet code :
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
Part resim = request.getPart("resim");
InputStream is = resim.getInputStream();
String resimFilename = FileUtilities.getFileName(resim);
String headline = request.getParameter("headline");
String freetext = request.getParameter("freetext");
System.out.println("FREETEXT in servlet : " + freetext);
int minsalary = Integer.parseInt(request.getParameter("minsalary"));
int maxsalary = Integer.parseInt(request.getParameter("maxsalary"));
int currencyId = Integer.parseInt(request.getParameter("currency"));
int cityId = Integer.parseInt(request.getParameter("city"));
int worktype = Integer.parseInt(request.getParameter("worktype"));
Person user = (Person) request.getSession().getAttribute("user");
int employeeId = user.getPersonid();
I have added following properties to my standalone.xml file :
<system-properties>
<property name="org.apache.coyote.http11.Http11Protocol.COMPRESSION" value="on"/>
<property name="org.apache.catalina.connector.URI_ENCODING" value="UTF-8"/>
<property name="org.apache.catalina.connector.USE_BODY_ENCODING_FOR_QUERY_STRING" value="true"/>
</system-properties>
After that failed, i moved these properties to the end of the file and jboss failed to start that time.
Also i have created a pre_start_jbossas-7 file in .openshift\action_hooks folder and added following lines in it :
export JAVA_OPTS="$JAVA_OPTS -Dorg.apache.catalina.connector.URI_ENCODING=\"UTF-8\" \
-Dorg.apache.catalina.connector.USE_BODY_ENCODING_FOR_QUERY_STRING=\"true\""
EDIT 1 :
I have also tried this solution, it didn't work either.
Not able to set options in JAVA_OPTS in JBoss openshift
EDIT 2 :
I have read the section for action hooks scripts and made them executable and tried to deploy again, still not working
Upvotes: 1
Views: 1051
Reputation: 141
I have solved the problem and wanted to log it in here just in case anybody else searches for the answer to the same problem.
First of all problem was about the multipart-form-data. Eventhough servlet 3.0 api was letting me getting the parameter via
request.getParameter("parameterName")
servlet is unable to get strings with UTF-8 format. Hence, i have changed this line with the following lines and this works like charm.
Part partname= request.getPart("parameterName");
InputStream is= partname.getInputStream();
String parameter = IOUtils.toString(is, "UTF-8");
Upvotes: 2