webcontent目录下的jsp: <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <form action="Servlet1" method="post"> <table> <tr> <td>姓名:</td> <td><input type="text" name="name"></td> </tr> <tr> <td>ename:</td> <td><input type="text" name="ename"></td> </tr> </table> <button class="submit">提交</button> </form> </body> </html> servlet包里面的Servlet1: package servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.catalina.Session; /** * Servlet implementation class Servlet1 */ @WebServlet("/Servlet1") public class Servlet1 extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Servlet1() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //response.setContentType("text/html;charset=utf-8"); // request.setCharacterEncoding("utf-8"); PrintWriter out=response.getWriter(); String info=request.getParameter("name"); String ename=request.getParameter("ename"); out.println(info); out.println(ename); out.close(); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } } filter包里面的: package filter; import java.io.IOException; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; 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.annotation.WebFilter; /** * Servlet Filter implementation class EncodingFilter */ @WebFilter("/EncodingFilter") public class EncodingFilter implements Filter { private String encoding; /** * @see Filter#destroy() */ public void destroy() { // TODO Auto-generated method stub System.out.println("end do the encoding filter"); encoding=null; } /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub // place your code here // pass the request along the filter chain System.out.println("before encoding"+encoding+"filter"); request.setCharacterEncoding(encoding); response.setContentType("text/html;charset="+encoding); chain.doFilter(request, response); System.out.println("after encoding"+encoding+"filter"); } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { // TODO Auto-generated method stub System.out.println("begin do the encoding filter"); encoding=fConfig.getInitParameter("encoding"); } } web.xml的配置: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>JavaEETask1</display-name> <filter> <filter-name>setCE</filter-name> <filter-class>filter.EncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>setCE</filter-name> <url-pattern>/JavaEETask1/Servlet1</url-pattern> </filter-mapping> </web-app>
为什么过滤器不起作用啊??求指点
qq_20151109_0
柒月君
元气满满GO
相关分类