实验五 Servlet过滤器

一、实验目的

1. 了解过滤器的作用;

2. 掌握过滤器的开发与部署的步骤;

3. 了解过滤器链。

二、实验原理

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

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

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

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

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

三、实验内容与步骤

编写一个过滤器改变请求编码。

【步骤1】编写一个loginform.html文件,代码如下:

 1 <html>
 2 
 3   <head>
 4 
 5     <title>使用过滤器改变请求编码</title>
 6 
 7     <meta http-equiv="Content-Type" content="text/html;charset=GB2312">
 8 
 9   </head>
10 
11   <body>
12 
13   <center>
14 
15   <h2>请输入用户名和口令:</h2>
16 
17   <form method="post" action="servlet/CheckParamServlet">
18 
19 <table>
20 
21   <tr>
22 
23 <td>用户名:</td>
24 
25 <td><input name="name" type="text"></td>
26 
27   </tr>
28 
29   <tr>
30 
31 <td>口    令:</td>
32 
33 <td><input name="pass" type="password"></td>
34 
35   </tr>
36 
37   <tr>
38 
39         <td></td>
40 
41                 <td>
42 
43                   <input name="ok" type="submit" value="提交">
44 
45                   <input name="cancel" type="reset" value="重置">
46 
47                 </td>
48 
49   </tr>
50 
51 </table>
52 
53   </form>
54 
55   </center>
56 
57  </body>
58 
59 </html>

【步骤2】编写处理请求参数的Servlet,代码如下:

 1  packet servlet;
 2 
 3 import java.io.*;
 4 
 5 import javax.servlet.*;
 6 
 7 import javax.servlet.http.*;
 8 
 9  
10 
11 public class CheckParamServlet extends HttpServlet{
12 
13    public void doGet(HttpServletRequest request,
14 
15                       HttpServletResponse response)
16 
17         throws ServletException, IOException {
18 
19  
20 
21      String name = request.getParameter("name");
22 
23      String pass = request.getParameter("pass");
24 
25      response.setContentType("text/html;charset=gb2312");
26 
27      PrintWriter out = response.getWriter();
28 
29      
30 
31      out.println("<html><head><title>Param Test</title></head>");   
32 
33      out.println("<h3 align=center>你的用户名为:"+name+"</h3>");
34 
35      out.println("<h3 align=center>你的口令为:"+pass+"</h3>");
36 
37      out.println("</body></html>");          
38 
39    }
40 
41    
42 
43    public void doPost(HttpServletRequest request,
44 
45                       HttpServletResponse response)
46 
47         throws ServletException, IOException {
48 
49        
50 
51        doGet(request,response);
52 
53    }
54 
55 }

【步骤3】修改web.xml文件,加入下面代码:

<servlet>

     <servlet-name>CheckParamServlet</servlet-name>

     <servlet-class>servlet.CheckParamServlet</servlet-class>

  </servlet>

  <servlet-mapping>

     <servlet-name>CheckParamServlet</servlet-name>

     <url-pattern>/servlet/CheckParamServlet</url-pattern>

  </servlet-mapping>

【步骤4】在浏览器的地址栏中输入下面URL

    http://localhost:8080/test5/loginform.html

    输入用户名和口令,如下图所示:

然后点击“提交”按钮,经CheckParamServlet处理后返回的结果如下图所示::

 

 

从这里我们可以看到,从服务器返回的汉字成了乱码。原因是没有指定request的编码。

下面通过编写一个过滤器改变请求编码。

【步骤5】过滤器代码如下:

 1 package filter;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.*;
 6 
 7 public class EncodingFilter implements Filter {
 8 
 9 protected String encoding = null;
10 
11 protected FilterConfig config;
12 
13 public void init(FilterConfig filterConfig) throws ServletException {
14 
15 this.config = filterConfig;
16 
17 // 得到在web.xml中配置的编码
18 
19 this.encoding = filterConfig.getInitParameter("Encoding");
20 
21 }
22 
23 public void doFilter(
24 
25 ServletRequest request,
26 
27 ServletResponse response,
28 
29 FilterChain chain)
30 
31 throws IOException, ServletException {
32 
33 if (request.getCharacterEncoding() == null) {
34 
35 // 得到指定的编码
36 
37 String encode = getEncoding();
38 
39 if (encode != null) {
40 
41 //设置request的编码
42 
43 request.setCharacterEncoding(encode);
44 
45 response.setCharacterEncoding(encode);
46 
47 }
48 
49 }
50 
51 chain.doFilter(request, response);
52 
53 }
54 
55 protected String getEncoding() {
56 
57 return encoding;
58 
59 }
60 
61 public void destroy() {
62 
63 }
64 
65 }

【步骤6】在web.xml文件中配置过滤器,加入下面代码:

<filter>

   <filter-name>EncodingFilter</filter-name>

   <filter-class>filter.EncodingFilter</filter-class>

      <init-param>

        <param-name>Encoding</param-name>

        <param-value>gb2312</param-value>

      </init-param>

 </filter>

 

 <filter-mapping>

   <filter-name>EncodingFilter</filter-name>

   <url-pattern>/*</url-pattern>

 </filter-mapping>

【步骤7】重复第(4)步操作,结果如下:

原文地址:https://www.cnblogs.com/liao-pxsoftware15/p/7771728.html