在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>

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。