request.getParameter()在java servlet中无法正确显示字符编码
我在java servlet文件中有一个UTF-8的问题。当我得到URL中的参数值,我有一些UTF-8字符的问题。
I have some problem with UTF-8 in java servlet file. When I get the parameter value in the URL, I have some problem with UTF-8 characters. It does not display properly Japanese Characters.
Jsp标头已有
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
我在连接器中将URIEncoding设置添加到server.xml中的UTF-8。
I added the URIEncoding setting in the connector to UTF-8 in server.xml.
<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
我在jsp中编写了以下代码。
I wrote the code as the following in jsp.
<s:textfield key="txt_name" name="txt_name" id="txt_name"
maxlength="64"></s:textfield>
<a href="javascript:showModalWindow('PopUpFile!init.action?<%=Common.PASSWORD%>=<%=Common.encript(ID, Code)%>','',940,650);">
<s:property value="PopUp Link" />
</a>
<script>
function showModalWindow(x_URL, x_ARG, x_WIDTH, x_HEIGHT) {
var x_OPT = "dialogHeight: " + x_HEIGHT + "px; " + "dialogWidth: "
+ x_WIDTH + "px; "
+ "edge: Raised; center: Yes; resizable: Yes; status: Yes;";
x_URL += "&name="+document.getElementById("txt_name").value;
var retValue = window.showModalDialog(x_URL, x_ARG, x_OPT);
if (retValue != null) {
document.forms.frm.action = "ParentFile!getUser.action";
document.forms.frm.submit();
}
}
</script>
然后,我在java servlet中编写了以下代码。
And then, I wrote the code as the following in java servlet.
if(g_request.getParameter("name") != null){
g_session.setAttribute(NAME, g_request.getParameter("name"));
}
我也用 request.setCharacterEncoding / code>方法在java servlet但它不真的工作。
虽然我尝试了许多方法从其他人的问题相关字符编码在servlet在stackoverflow的答案,我不能解决我的问题,直到。
I also tested with request.setCharacterEncoding()
method in java servlet but it doesn't really work.
Although I tried many ways from answers of the other people's problem related with character encoding in servlet in stackoverflow, I can't solve my problem until.
我可以正确显示字符编码吗?提前感谢。
What can I do to display correctly the character encoding? Thanks in advance.
大多数服务器(包括Apache Tomcat服务器)都配置为使用 ISO -8859-1
。我想你不会改变这个,除非你有一个私人的专用服务器实例。因此,程序员的技术是手动编码/解码这些参数。因为你使用的是javascript,所以有 encodeURI()
或 encodeURIComponent()
内置函数。请参见如何在JavaScript中对网址进行编码。代码应该更改
Most servers, including Apache Tomcat server, are configured to parameter encoding with ISO-8859-1
by default. I think you won't change this unless you have a private dedicated server instance. So, the programmer's technique is to encode/decode those parameters manually. Because you are using javascript, there's encodeURI()
or encodeURIComponent()
built-in function. See How to encode a URL in JavaScript. The code should change
x_URL += "&name="+encodeURI(document.getElementById("txt_name").value);
使用 URLDecoder
参数返回。
java.net.URLDecoder.decode(((String[])request.getParameterMap().get("name"))[0], "UTF-8"));
注意,如果你使用Struts2 dispatcher
结果类型,那么你不需要解码查询字符串中的参数。这些参数通过 UrlHelper
解析。
Note, if you are using Struts2 dispatcher
result type then you don't need to decode parameters in the query string. Those parameters are parsed via UrlHelper
.
但是,我不记得当我解码那些参数是自动解码在Struts2。
However, I don't remember when I decode those parameters are automatically decoded in Struts2.
通常,您应该知道,如果您在网址中传递参数,则应将其进行网址编码。如果提交表单没有必要这样做,因为表单是 x-www-form-urlencoded
,请参阅 17.13.4表单内容类型。
As a rule you should know that if you pass parameters in the URL they should be URL encoded. If you submit the form there's no need to do it because the form is x-www-form-urlencoded
, see 17.13.4 Form content types.