目的是让浏览网站的用户所接收到的信息的编码方式统一为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 { } }
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 { } }
<?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>
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"); } }
<?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://blog.csdn.net/acmman/article/details/44100531