目的是让浏览网站的用户所接收到的信息的编码方式统一为UTF-8,防止乱码的产生
1.没加过滤器之前:
拿Jsp工程(名叫web)中的两个Servlet做实验
ChineseServlet.java:
package cn.edu.hpu.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ChineseServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String data="我的中国心"; request.setAttribute("data", data); request.getRequestDispatcher("/result").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
ResultServlet.java
package cn.edu.hpu.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ResultServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String text=(String)request.getAttribute("data"); PrintWriter out=response.getWriter(); out.write("传过去的那句话是(The word get is):"+text); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
在web.xml中配置:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>ChineseServlet</servlet-name> <servlet-class>cn.edu.hpu.servlet.ChineseServlet</servlet-class> </servlet> <servlet> <servlet-name>ResultServlet</servlet-name> <servlet-class>cn.edu.hpu.servlet.ResultServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ChineseServlet</servlet-name> <url-pattern>/chin</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>ResultServlet</servlet-name> <url-pattern>/result</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
启动服务器,在地址栏上输入:http://localhost:8080/web/chin,得到:
?????????The word get is?:?????
出现乱码,说明没有指定编码。
解决办法:
两边servlet同时设置(这里以UTF-8为编码标准):
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
需要显示的时候:
String text=(String)request.getAttribute("data")+"是传过来的那句话";
System.out.println(text);
OutputStream out =response.getOutputStream();
out.write("<html>".getBytes());
//用html技术中meta标签模拟了一个http响应头,来控制浏览器的行为
out.write("<meta http-equiv='content-type' content='text/html;charset=UTF-8'>".getBytes());
out.write(text.getBytes("UTF-8"));
out.write("</html>".getBytes());
或者直接:
response.setContentType("text/html;charset=UTF-8");
以上方法很臃肿,而且非常不简介,无法应对多个界面,所以要使用过滤器来统一设置编码。
2.加过滤器之后:
ChineseServlet.java:
package cn.edu.hpu.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ChineseServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String data="我的中国心"; request.setAttribute("data", data); request.getRequestDispatcher("/result").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
package cn.edu.hpu.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ResultServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String text=(String)request.getAttribute("data"); PrintWriter out=response.getWriter(); out.write("传过去的那句话是(The word get is):"+text); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
过滤器:
package cn.edu.hpu.filter; 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; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class TestFilter implements Filter { private String characterEncoding=null; public void destroy() { // TODO Auto-generated method stub } public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterchain) throws IOException, ServletException { request.setCharacterEncoding(characterEncoding); response.setCharacterEncoding(characterEncoding); response.setContentType("text/html;charset="+characterEncoding); //获取URL的路径和参数 HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse resp = (HttpServletResponse) response; String path=req.getServletPath(); String param=req.getQueryString(); if(path!=null&¶m!=null){ path=path+"?"+param;//全请求路径 } System.out.println("Path:"+path); //跳转或带参数跳转 request.getRequestDispatcher(path).forward(request, response); } public void init(FilterConfig filterConfig) throws ServletException { characterEncoding = filterConfig.getInitParameter("characterEncoding"); } }
在web.xml中配置:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter> <filter-name>TestFilter</filter-name> <filter-class>cn.edu.hpu.filter.TestFilter</filter-class> <init-param> <param-name>characterEncoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>TestFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>ChineseServlet</servlet-name> <servlet-class>cn.edu.hpu.servlet.ChineseServlet</servlet-class> </servlet> <servlet> <servlet-name>ResultServlet</servlet-name> <servlet-class>cn.edu.hpu.servlet.ResultServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ChineseServlet</servlet-name> <url-pattern>/chin</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>ResultServlet</servlet-name> <url-pattern>/result</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
启动服务器,在地址栏上输入:http://localhost:8080/web/chin,得到:
传过去的那句话是(The word get is):我的中国心
没有发生乱码,说明过滤成功!!
转载请注明出处:http://blog.csdn.net/acmman/article/details/44100531