在Web.xml中定义自己的过滤器
1.过滤器代码
1 package com.zkj.filter; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.Filter; 7 import javax.servlet.FilterChain; 8 import javax.servlet.FilterConfig; 9 import javax.servlet.ServletException; 10 import javax.servlet.ServletRequest; 11 import javax.servlet.ServletResponse; 12 import javax.servlet.http.HttpServlet; 13 import javax.servlet.http.HttpServletRequest; 14 import javax.servlet.http.HttpServletResponse; 15 16 public class MyFilter extends HttpServlet implements Filter { 17 18 public void doFilter(ServletRequest arg0, ServletResponse arg1, 19 FilterChain arg2) throws IOException, ServletException { 20 // TODO Auto-generated method stub 21 arg0.setCharacterEncoding("utf-8"); 22 arg2.doFilter(arg0, arg1); 23 } 24 25 public void init(FilterConfig arg0) throws ServletException { 26 // TODO Auto-generated method stub 27 28 } 29 30 }
2.web.xml配置
<!--配置自己的过滤器--> <filter> <filter-name>MyFilter</filter-name> <filter-class>com.zkj.filter.MyFilter</filter-class> </filter> <filter-mapping> <filter-name>MyFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
3.jsp文件放入WEB-INF目录巧处理之index.jsp代码
<body> <jsp:forward page="WEB-INF/register.jsp"></jsp:forward> </body>