如何在 Servlets 中启用读取非 ascii 字符 [英] How to enable reading non-ascii characters in Servlets

查看:27
本文介绍了如何在 Servlets 中启用读取非 ascii 字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让 servlet 接受从 JSP 传递过来的非 ascii(阿拉伯文、中文等)字符?

How to make the servlet accept non-ascii (Arabian, chines, etc) characters passed from JSPs?

我尝试将以下内容添加到 JSP 的顶部:

I've tried to add the following to top of JSPs:

<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

并在 servlet 的每个 post/get 方法中添加以下内容:

And to add the following in each post/get method in the servlet:

request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");

我尝试添加一个过滤器来执行上述两个语句,而不是在 servlet 中.

I've tried to add a Filter that executes the above two statements instead of in the servlet.

老实说,这些在过去有效,但现在不再有效.

To be quite honest, these was working in the past, but now it doesn't work anymore.

我在 Win & JDK1.6 上使用 tomcat 5.0.28/6.x.xLinux 盒子.

I am using tomcat 5.0.28/6.x.x on JDK1.6 on both Win & Linux boxes.

这是一个例子:JSP页面:

Here's an example: JSP Page:

<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<title>Push Engine</title>
</head>
<body>
Hello ${requestScope['val']}
<form action="ControllerServlet" method="POST">
<table>
    <tr>
        <td>ABC</td>
        <td><input name="ABC" type="text" /></td>
    </tr>
    <tr>
        <td></td>
        <td><input type="submit" value="Submit"></td>
    </tr>
</table>
</form>

</body>
</html>

Servlet doGet 方法:

Servlet doGet method:

protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String val = "request.getParameter('ABC') : " + request.getParameter("ABC");
        System.out.println(val);
        request.setAttribute("val", val);
        request.getRequestDispatcher("index.jsp").forward(request, response);
    }

问题在于:在控制台中,值???"正在打印,但是返回的值返回到包含正确 Unicode 字的 JSP 页面

THE PROBLEM IS: in the console, value "???" is being printed, however, the value returned backed to the JSP page containing the correct Unicode word

这 "???"打印到控制台是我运行此测试的机器中的问题.我在另一台机器上运行了同样的例子,它运行正常!

the "???" printed to the console is a problem in the machine that I ran this test on. I've ran the same example on another machine, and It works properly!

推荐答案

到此为止,需要设置请求编码.

对于 GET 请求(其中参数通过请求 URL 传递),您需要在应用程序服务器级别进行配置.例如在 Tomcat 6.0 中,设置 /conf/server.xml 元素的 URIEncoding 属性为 UTF-8.

For GET requests (wherein the parameters are passed through the request URL), you need to configure this at appserver level. In for example Tomcat 6.0 it suffices to set the URIEncoding attribute of the <Connector> element in /conf/server.xml to UTF-8.

<Connector (...) URIEncoding="UTF-8" />

对于POST请求(其中参数是通过请求体隐形"传递的),需要调用ServletRequest#setCharacterEncoding() with UTF-8 before 收集任何请求参数.最好的地方是在一个过滤器中,它被称为链中的第一个过滤器:

For POST requests (wherein the parameters are "invisibly" passed through the request body), you need to call ServletRequest#setCharacterEncoding() with UTF-8 before gathering any request parameter. The best place is to do this is in a filter which is been called as the very first filter in the chain:

if (request.getCharacterEncoding() == null) {
    request.setCharacterEncoding("UTF-8");
}
chain.doFilter(request, response);

这篇关于如何在 Servlets 中启用读取非 ascii 字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆