Jsp和Servlet如何使用中文参数
首先在java中添加如下类:
1 package com.filter;//包名
2
3 import java.io.IOException;
4
5 import javax.servlet.Filter;
6 import javax.servlet.FilterChain;
7 import javax.servlet.FilterConfig;
8 import javax.servlet.ServletException;
9 import javax.servlet.ServletRequest;
10 import javax.servlet.ServletResponse;
11
12 public class EncodeFilter implements Filter{
13
14 public void destroy() {
15 // TODO Auto-generated method stub
16
17 }
18
19 public void doFilter(ServletRequest req, ServletResponse res, FilterChain c) throws IOException, ServletException {
20 // TODO Auto-generated method stub
21 req.setCharacterEncoding("utf-8");
22 res.setCharacterEncoding("utf-8");
23 res.setContentType("text/html;charset=utf-8");
24 c.doFilter(req, res);
25
26 }
27
28 public void init(FilterConfig arg0) throws ServletException {
29 // TODO Auto-generated method stub
30
31 }
32
33 }
然后再在web.xml中添加一下内容:
<filter>
<filter-name>encodeFilter</filter-name>
<filter-class>com.filter.EncodeFilter</filter-class> <!--这里写你自己的这个类的路径-->
</filter>
<filter-mapping>
<filter-name>encodeFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>