request.getParameter() 在 java servlet 中没有正确显示字符编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19086190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
request.getParameter() does not display properly character encoding in java servlet
提问by Sandar Min Aye
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.
我对 java servlet 文件中的 UTF-8 有一些问题。当我在 URL 中获取参数值时,我对 UTF-8 字符有一些问题。它不能正确显示日语字符。
Jsp header already has
Jsp 头已经有了
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
I added the URIEncoding setting in the connector to UTF-8 in server.xml.
我将连接器中的 URIEncoding 设置添加到 server.xml 中的 UTF-8。
<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
I wrote the code as the following in jsp.
我在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>
And then, I wrote the code as the following in java servlet.
然后,我在 java servlet 中编写了如下代码。
if(g_request.getParameter("name") != null){
g_session.setAttribute(NAME, g_request.getParameter("name"));
}
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.
我也用request.setCharacterEncoding()
java servlet 中的方法进行了测试,但它并没有真正起作用。虽然我从其他人在 stackoverflow 中与 servlet 中的字符编码相关的问题的答案中尝试了很多方法,但直到我无法解决我的问题。
What can I do to display correctly the character encoding? Thanks in advance.
我该怎么做才能正确显示字符编码?提前致谢。
采纳答案by Roman C
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
大多数服务器,包括 Apache Tomcat 服务器,ISO-8859-1
默认配置为参数编码。我认为除非你有一个私有的专用服务器实例,否则你不会改变这一点。因此,程序员的技术是手动编码/解码这些参数。因为您使用的是 javascript,所以有encodeURI()
或encodeURIComponent()
内置函数。请参阅如何在 JavaScript 中对 URL 进行编码。代码应该改变
x_URL += "&name="+encodeURI(document.getElementById("txt_name").value);
in the Java use the URLDecoder
to decode parameter back.
在 Java 中使用URLDecoder
to decode 参数返回。
java.net.URLDecoder.decode(((String[])request.getParameterMap().get("name"))[0], "UTF-8"));
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
.
请注意,如果您使用的是 Struts2dispatcher
结果类型,那么您不需要解码查询字符串中的参数。这些参数通过UrlHelper
.
However, I don't remember when I decode those parameters are automatically decoded in Struts2.
但是,我不记得我解码时那些参数在 Struts2 中自动解码了。
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.
作为一项规则,您应该知道,如果您在 URL 中传递参数,它们应该是 URL 编码的。如果您提交表单,则无需执行此操作,因为表单是x-www-form-urlencoded
,请参阅17.13.4 表单内容类型。
回答by stepanian
Are you testing the output using System.out.println
? If so, it may be that it is not configured for UTF-8.
您是否正在使用System.out.println
? 如果是这样,则可能是未针对 UTF-8 进行配置。
See this: Can not send special characters (UTF-8) from JSP to Servlet: question marks displayed
请参阅:Can not send special characters (UTF-8) from JSP to Servlet: 问号显示
Also make sure that you run request.setCharacterEncoding("UTF-8")
BEFORE reading the parameters.
还要确保request.setCharacterEncoding("UTF-8")
在读取参数之前运行。
回答by erhun
I have similar error and solve it use Servlet filter if you are using Spring, just add this definition to web.xml
如果您使用的是 Spring,我有类似的错误并使用 Servlet 过滤器解决它,只需将此定义添加到 web.xml
<!-- CharacterEncodingFilter / UTF-8 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
if you are not using Spring write a servlet filter like :
如果您不使用 Spring,请编写一个 servlet 过滤器,例如:
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
//Set character encoding as UTF-8
request.setCharacterEncoding("UTF-8");
filterChain.doFilter(request, response);
}
回答by Jarekczek
I would like to escale a comment from f.khantsisto an answer, because it turned out to be important in my case.
我想将f.khantsis的评论升级为答案,因为它对我来说很重要。
Important: make sure this filter is carried out FIRST, before any others, otherwise it won't work.
重要提示:确保此过滤器在任何其他过滤器之前首先执行,否则它将无法工作。
We had a correct encoding setting in our servlet filter:
我们在 servlet 过滤器中有一个正确的编码设置:
request.setCharacterEncoding("UTF-8");
But when I put a breakpoint in request.getParameter
it showed, that we are reading parameters before setting the encoding. It was happening in another filter (csrf). At this moment the encoding was frozen and setting it further was ineffective. This may be server dependent but in our case (Websphere) it was the case.
但是当我在request.getParameter
里面放一个断点时,它显示我们在设置编码之前正在读取参数。它发生在另一个过滤器(csrf)中。此时编码被冻结,进一步设置无效。这可能取决于服务器,但在我们的案例(Websphere)中就是这种情况。