zoukankan      html  css  js  c++  java
  • Servlet过滤器

    过滤器是web服务器上的组件,它们对客户和资源之间的请求和响应进行过滤。

    过滤器的工作原理是:当servlet容器接收到对某个资源的请求,它要检查是否有过滤器与之关联。如果有过滤器与该资源关联,servlet容器将把该请求发送给过滤器。在过滤器处理完请求后,它将做下面3件事:

    • 产生响应并将其返回给客户;
    • 如果有过滤器链,它将把(修改过或没有修改过)请求传递给下一个过滤器;
    • 将请求传递给不同的资源。

    当请求返回到客户时,它是以相反的方向经过同一组过滤器返回。过滤器链中的每个过滤器够可能修改响应。

    过滤器API主要包括:FilterFilterConfigFilterChain接口。

    1.登录页面

    <html>
      <head>
        <title>使用过滤器改变请求编码</title>
        <meta http-equiv="Content-Type" content="text/html;charset=GB2312">
      </head>
      <body>
      <center>
      <h2>请输入用户名和口令:</h2>
      <form method="post" action="servlet/CheckParamServlet">
        <table>
          <tr>
            <td>用户名:</td>
            <td><input name="name" type="text"></td>
          </tr>
          <tr>
            <td>口    令:</td>
            <td><input name="pass" type="password"></td>
          </tr>
          <tr>
                <td></td>
                    <td>
                      <input name="ok" type="submit" value="提交">
                      <input name="cancel" type="reset" value="重置">
                    </td>
          </tr>
        </table>
      </form>
      </center>
     </body>
    </html>

    2.Servlet获取表单

    package test;
    
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    public class CheckParamServlet extends HttpServlet{
       public void doGet(HttpServletRequest request,
                          HttpServletResponse response)
            throws ServletException, IOException {
    
         String name = request.getParameter("name");
         String pass = request.getParameter("pass");
         response.setContentType("text/html;charset=gb2312");
         PrintWriter out = response.getWriter();
         
         out.println("<html><head><title>Param Test</title></head>");   
         out.println("<h3 align=center>你的用户名为:"+name+"</h3>");
         out.println("<h3 align=center>你的口令为:"+pass+"</h3>");
         out.println("</body></html>");          
       }
       
       public void doPost(HttpServletRequest request,
                          HttpServletResponse response)
            throws ServletException, IOException {
           
           doGet(request,response);
       } 
    }

    3.过滤器

    package filter;
    
    import java.io.IOException;
    import javax.servlet.*;
    public class EncodingFilter implements Filter {
        protected String encoding = null;
        protected FilterConfig config;
        public void init(FilterConfig filterConfig) throws ServletException {
            this.config = filterConfig;
            // 得到在web.xml中配置的编码
            this.encoding = filterConfig.getInitParameter("Encoding");
        }
        public void doFilter(
            ServletRequest request,
            ServletResponse response,
            FilterChain chain)
            throws IOException, ServletException {
            if (request.getCharacterEncoding() == null) {            
                // 得到指定的编码
                String encode = getEncoding();
                if (encode != null) {                
                    //设置request的编码
                    request.setCharacterEncoding(encode);
                    response.setCharacterEncoding(encode);        
                }
            }
            chain.doFilter(request, response);
        }
        protected String getEncoding() {
            return encoding;
        }
        public void destroy() {
        }
    }

    过滤器使用前后对比

  • 相关阅读:
    tyvj 1031 热浪 最短路
    【bzoj2005】 [Noi2010]能量采集 数学结论(gcd)
    hdu 1394 Minimum Inversion Number 逆序数/树状数组
    HDU 1698 just a hook 线段树,区间定值,求和
    ZeptoLab Code Rush 2015 C. Om Nom and Candies 暴力
    ZeptoLab Code Rush 2015 B. Om Nom and Dark Park DFS
    ZeptoLab Code Rush 2015 A. King of Thieves 暴力
    hdoj 5199 Gunner map
    hdoj 5198 Strange Class 水题
    vijos 1659 河蟹王国 线段树区间加、区间查询最大值
  • 原文地址:https://www.cnblogs.com/ljs-666/p/7779022.html
Copyright © 2011-2022 走看看