utf-8格式在tomcat服务器的servlet中不起作用 [英] UtF-8 format not working in servlet for tomcat server
问题描述
我有一个Java Servlet,它可以通过ajax javascript从HTML接受数据. HTML从用户那里接收印地文文本,并通过ajax将其发送到servlet中.我在所有地方都指定了UTF-8格式,但是它不起作用.
I have a Java Servlet which accepts data from HTML through ajax javascript. Html receives hindi text from user and sends it through ajax into a servlet. I have specified UTF-8 format everywhere but it is not working.
我已将请求和响应编码设置为utf-8,但仍然无法正常工作. 服务器是Tomcat.
I have set request and response encoding as utf-8 , still not working. The server is Tomcat.
如果有人可以提供帮助.
If anyone can help.
推荐答案
按照以下步骤将Tomcat配置为正确地服务于UTF-8页面(这是波斯语,阿拉伯语或从右到左书写的任何语言):
Follow the following steps to configure your Tomcat to serve your UTF-8 pages correctly either this is Persian, Arabic or any language that is written from right to left:
(1)如下编辑您的server.xml
(1) Edit your server.xml as following
2)CharsetFilter强制Java Web应用处理UTF-8编码的所有请求和响应.这就要求我们定义一个字符集过滤器,如下所示:
2) CharsetFilter force the java webapp to handle all requests and responses as UTF-8 encoded. This requires that we define a character set filter like the following:
package charsetFilter.classes;
package charsetFilter.classes;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class CharsetFilter implements Filter{
private String encoding;
public void init(FilterConfig config) throws ServletException{
encoding = config.getInitParameter("requestEncoding");
if( encoding==null ) encoding="UTF-8";
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain next)
throws IOException, ServletException{
if(null == request.getCharacterEncoding())
request.setCharacterEncoding(encoding);
response.setContentType("text/html; charset=UTF-8");
response.setCharacterEncoding("UTF-8");
next.doFilter(request, response);
}
public void destroy(){}
}
此过滤器可确保如果浏览器未设置请求中使用的编码,则将其设置为UTF-8.该过滤器完成的另一件事是设置默认响应编码,即.返回的html/所使用的编码.另一种方法是在应用程序的每个控制器中设置响应编码等.
This filter makes sure that if the browser hasn't set the encoding used in the request, that it's set to UTF-8. The other thing done by this filter is to set the default response encoding ie. the encoding in which the returned html/whatever is. The alternative is to set the response encoding etc. in each controller of the application.
3)然后将该过滤器添加到web.xml中,如下所示:
3) Then add this filter to the web.xml like:
字符集过滤器 charsetFilter.classes.CharsetFilter requestEncoding UTF-8
CharsetFilter charsetFilter.classes.CharsetFilter requestEncoding UTF-8
<filter-mapping>
<filter-name>CharsetFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
然后将您的servlet编写为:
Then write your servlet as:
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
response.setCharacterEncoding("UTF-8");
,并使用您要在servlet中使用的任何语言.
and use whatever language you want to use in your servlet.
您可以使用它来获取参数:
You can use this for getting parameters:
字符串输入=新的字符串(request.getParameter("foo").getBytes("iso-8859-1"),"utf-8");
String input = new String(request.getParameter("foo").getBytes("iso-8859-1"), "utf-8");
干杯!
这篇关于utf-8格式在tomcat服务器的servlet中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!