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"));
}

我还在 java servlet 中使用 request.setCharacterEncoding() 方法进行了测试,但它并没有真正起作用.虽然我尝试了很多方法来回答其他人在stackoverflow中与servlet中字符编码相关的问题,但我无法解决我的问题.

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 中编码 URL.代码应该改变

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);

在Java中使用URLDecoder将参数解码回来.

in the Java use the URLDecoder to decode parameter back.

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.

作为一项规则,您应该知道,如果您在 URL 中传递参数,它们应该是 URL 编码的.如果您提交表单,则无需这样做,因为表单是 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.

相关文章