Openshift JBoss 字符编码
Openshift JBoss Character Encoding
我用j2ee开发了一个网站。我的开发环境中的字符编码没有问题,但是当我将我的应用程序部署到 openshift (jboss 7, mysql 5.5) 时,UTF 格式似乎不起作用。我尝试在服务器端登录,似乎是 jboss 环境问题,因为我可以看到 servlet 无法解码字符。请在下面找到 front/backend 的代码,在代码之后,我列出了我已经尝试过的内容。
谢谢,
<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>
我还有一个 javascript 用于文件扩展名验证:
<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>
我的 servlet 代码:
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();
我已将以下属性添加到我的 standalone.xml 文件中:
<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>
那次失败后,我将这些属性移到了文件的末尾,jboss 那次启动失败。
我还在 .openshift\action_hooks 文件夹中创建了一个 pre_start_jbossas-7 文件,并在其中添加了以下行:
export JAVA_OPTS="$JAVA_OPTS -Dorg.apache.catalina.connector.URI_ENCODING=\"UTF-8\" \
-Dorg.apache.catalina.connector.USE_BODY_ENCODING_FOR_QUERY_STRING=\"true\""
编辑 1:
我也试过这个办法,也没用。
Not able to set options in JAVA_OPTS in JBoss openshift
编辑 2:
我已阅读操作挂钩脚本部分并使它们可执行并尝试再次部署,但仍然无法正常工作
我已经解决了这个问题并想在这里记录它以防其他人搜索相同问题的答案。
首先是关于多部分表单数据的问题。尽管 servlet 3.0 api 让我通过
获取参数
request.getParameter("parameterName")
servlet 无法获取 UTF-8 格式的字符串。
因此,我用以下几行更改了这一行,这就像魅力一样。
Part partname= request.getPart("parameterName");
InputStream is= partname.getInputStream();
String parameter = IOUtils.toString(is, "UTF-8");
我用j2ee开发了一个网站。我的开发环境中的字符编码没有问题,但是当我将我的应用程序部署到 openshift (jboss 7, mysql 5.5) 时,UTF 格式似乎不起作用。我尝试在服务器端登录,似乎是 jboss 环境问题,因为我可以看到 servlet 无法解码字符。请在下面找到 front/backend 的代码,在代码之后,我列出了我已经尝试过的内容。
谢谢,
<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>
我还有一个 javascript 用于文件扩展名验证:
<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>
我的 servlet 代码:
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();
我已将以下属性添加到我的 standalone.xml 文件中:
<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>
那次失败后,我将这些属性移到了文件的末尾,jboss 那次启动失败。
我还在 .openshift\action_hooks 文件夹中创建了一个 pre_start_jbossas-7 文件,并在其中添加了以下行:
export JAVA_OPTS="$JAVA_OPTS -Dorg.apache.catalina.connector.URI_ENCODING=\"UTF-8\" \
-Dorg.apache.catalina.connector.USE_BODY_ENCODING_FOR_QUERY_STRING=\"true\""
编辑 1:
我也试过这个办法,也没用。
Not able to set options in JAVA_OPTS in JBoss openshift
编辑 2:
我已阅读操作挂钩脚本部分并使它们可执行并尝试再次部署,但仍然无法正常工作
我已经解决了这个问题并想在这里记录它以防其他人搜索相同问题的答案。
首先是关于多部分表单数据的问题。尽管 servlet 3.0 api 让我通过
获取参数request.getParameter("parameterName")
servlet 无法获取 UTF-8 格式的字符串。 因此,我用以下几行更改了这一行,这就像魅力一样。
Part partname= request.getPart("parameterName");
InputStream is= partname.getInputStream();
String parameter = IOUtils.toString(is, "UTF-8");