Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
850 views
in Technique[技术] by (71.8m points)

jsp - request.getParameter() does not display properly character encoding in java servlet

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 header already has

<%@ 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.

<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

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>

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

in the Java use the URLDecoder to decode parameter back.

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.

However, I don't remember when I decode those parameters are automatically decoded in 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...