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

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

问题描述

如何使servlet接受从JSP传递的非ascii(阿拉伯文,中文等)字符?



我试图将以下内容添加到JSP:

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

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

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

我试图添加一个执行上面两个语句而不是在servlet中的Filter。 / p>

说实话,这些都是在过去工作,但现在它不再工作了。



我使用tomcat 5.0.28 / 6.xx在JDK1.6上的Win& Linux框。



以下是一个示例:
JSP页面:

 <%@ page language =javacontentType =text / html; charset = UTF-8pageEncoding =UTF-8%& 
< html>
< head>
< title>推送引擎< / title>
< / head>
< body>
Hello $ {requestScope ['val']}
< form action =ControllerServletmethod =POST>
< table>
< tr>
< td> ABC< / td>
< td>< input name =ABCtype =text/>< / td>
< / tr>
< tr>
< td>< / td>
< td>< input type =submitvalue =Submit>< / td>
< / tr>
< / table>
< / form>

< / body>
< / html>

Servlet doGet方法:

  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页面




???打印到控制台是在我运行这个测试的机器的一个问题。
我在另一台机器上运行相同的例子,它工作正常!

解决方案

需要设置请求编码 a>。



对于GET请求(其中的参数通过请求URL传递),您需要在appserver级别配置。例如,在 Tomcat 6.0 中,只需设置<$ /conf/server.xml 中的< Connector> 元素的 $ c>到 UTF-8

  ..)URIEncoding =UTF-8/> 

对于POST请求(其中参数通过请求主体不可见传递),您需要调用 ServletRequest#setCharacterEncoding() UTF-8 之前收集任何请求参数。最好的方法是在过滤器中将其作为链中第一个

过滤器的过滤器。

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


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

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

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

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

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

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.

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

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

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!

解决方案

To the point, you need to set the request encoding.

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" />

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

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

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