day05—-过滤器。Servlet之Filter详解。

Filter即过滤器,是servlet中尽实用的技巧,可以针对web服务器管理的备web资源开展拦截,从而实现特殊意义;主要用于对用户请求进行先期处理,也得以对httpServletResponse进行后甩卖;完整流程:Filter对用户要进行预处理,接着用呼吁提交servlet进行拍卖并生成响应,最后Filter再指向服务器响应进行后甩卖

参考文献:http://www.cnblogs.com/zlbx/p/4888312.html

过滤器相关API

 

interface  Filter  过滤器核心接口

Filter,过滤器,顾名思义,即是指向数码等的过滤,预处理过程。为什么而引入过滤器呢?在凡看网站的当儿,有时候发片敏锐的信,发出后显得时
就会见将相机行事信息之所以*齐字符替代,这便是故过滤器对信息进行了拍卖。这才是一个概括的例子,当然,过滤器那么强劲,它的意义也不可能局限为此,它不只能预处
理数据,只要是发送过来的呼吁其都是可以预先处理的,同时,它还足以对服务器返回的响应进行事先处理,这样,大大减轻了服务器的下压力。例如,实现URL级别的
权限访问控制、过滤敏感词汇、压缩响应信息相当部分高档功能。下面来详细介绍一下过滤器。

void init(filterconfig)初始化方法,服务器启动时实行;void 
doFilter(request,response,filterchain)
过滤器拦截的业务处理方式;void destroy() 销毁过滤器实例是调用

一、定义

interface  FilterConfig  获取初始化参数信息

  学习一个东西,我们先是使知道它的定义。

interface  FilterChain  过滤器链参数,一个个过滤器形成一个执行链

 

过滤器编码统一处理

 

public class EncodingFilter implements Filter
{

    @Override
    public void destroy(){}

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException
    {
        //转型  httpservletrequest是servletrequest的子类,多了针对于http协议的方法
        final HttpServletRequest req = (HttpServletRequest) request;
        final HttpServletResponse rep = (HttpServletResponse) response;
        //公用业务处理
        req.setCharacterEncoding("utf-8");  //设置post提交的请求的编码
        req.setCharacterEncoding("utf-8");  //设置响应体的编码
        rep.setContentType("text/html;charset=utf-8");  //设置页面打开时的编码格式,设置响应体的编码
        //get中文乱码,request.getparameter方法内部没有进行提交方式判断和处理,使用代理处理
        HttpServletRequest proxy = (HttpServletRequest) Proxy.newProxyInstance(
                req.getClass().getClassLoader(), //指定当前使用的类加载器
                new Class[] {HttpServletRequest.class}, //对目标对象实现的接口类型
                new InvocationHandler()  //事件处理器
                {

                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args)
                            throws Throwable
                    {
                        //定义方法返回值
                        Object returnValue = null;
                        //获取方法名
                        String methodName = method.getName();
                        //判断:对getparameter方法进行get提交中文处理
                        if("getParameter".equals(methodName)){
                            //获取请求数据值
                            String value = req.getParameter(args[0].toString());
                            //获取提交方式
                            String methodSubmit = req.getMethod();
                            //如果是get提交,对数据进行处理
                            if("GET".equals(methodSubmit)){
                                if(value != null && "".equals(value.trim())){
                                    value = new String(value.getBytes("IS08859-1"),"UTF-8");
                                }
                            }
                            return value;
                        }else{
                            returnValue = method.invoke(req, args);
                        }
                        return returnValue;
                    }
                });
        //传入代理对象,执行下一个过滤器或者servlet
        chain.doFilter(proxy, rep);
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException{}

}

1.概念

 

 
 过滤作用,对由客户端向劳动器端发送的求进行过滤,也得以本着劳务器端返回的应进行处理。它使用户可更改一个request和修改一个
response.。Filter
不是一个servlet,它不能够有一个response,但是其会当一个request到达servlet之前预处理request,也得以于
response离开servlet时处理response。换句话说,filter其实是客户端和servlet中间的一个传递者,并且她可以针对如传递
的物进行修改。

无效数据过滤

澳门1495娱乐 1

报到权限判断

  注意:过滤器是用来堵住请求与应的,不克起响应,而servlet是用来处理要并有响应的。

 

 

 

2.适用场合

  实现URL级别的权访问控制,过滤敏感词汇,压缩响应信息相当。

 

 

3.过滤器如何促成拦阻

  • 当客户端有请求后,在HttpServletRequest 到达Servlet
    之前,过滤器拦截客户的HttpServletRequest 。 
  • 根据需要检讨HttpServletRequest ,也得以修改HttpServletRequest
    头和数据。 
  • 当过滤器中调用doFilter方法,对要放行。请求到达Servlet后,对要进行处理并生HttpServletResponse发送给客户端。
  • 每当HttpServletResponse 到达客户端之前,过滤器拦截HttpServletResponse
    。 
  • 因需要检查HttpServletResponse ,可以修改HttpServletResponse
    头和数据。
  • 说到底,HttpServletResponse到达客户端。

 

 

4.Filter接口

  Servlet API提供了一个Filter接口,编写的过滤器必须兑现该接口。

 

 

5.Filter的生命周期

(1)Filter接口中起三独至关重要之道。

  • init()方法:初始化参数,在创造Filter时活动调用。当我们要安装初始化参数的时,可以形容及拖欠法吃。
  • doFilter()方法:拦截到如履行之呼吁时,doFilter就见面实施。这中写我们对要与应的优先处理。
  • destroy()方法:在销毁Filter时自动调用。

 

(2)Filter的生命周期

    Filter的创同销毁由web服务器控制。

  • 服务器启动的时候,web服务器创建Filter的实例对象,并调用其init方法,完成目标的初始化功能。filter对象就会创同浅,init方法也只有见面履同一糟。
  • 挡到要时,执行doFilter方法。可以推行多次。
  • 服务器关闭时,web服务器销毁Filter的实例对象。

 

 

6.Filter对象——FilterConfig

  用
户在部署filter时,可以应用<init-param>为filter配置有初始化参数,当web容器实例化Filter对象,调用该
init方法时,会管包了filter初始化参数的filterConfig对象传递进入。因此开发人员在编制filter时,通过
filterConfig对象的道,就只是抱:

  • String getFilterName():得到filter的名称。
  • String getInitParameter(String name):
    返回在配备描述着指定名称的初始化参数的价。如果未有回null.
  • Enumeration
    getInitParameterNames():返回过滤器的兼具初始化参数的名字的枚举集合。
  • public ServletContext
    getServletContext():返回Servlet上下文对象的援。

 

 

7.过滤器链——FilterChain

  一组过滤器对少数web资源开展阻挠,那么这组过滤器就叫做过滤器链。过滤器的行各个及<filter-mapping>有关(谁当面前先行实施谁)。

澳门1495娱乐 2

其次、开发步骤

  了解了过滤器的相关概念,接下去进行实例开发。

 

1.编纂步骤

  • 编排java类实现Filter接口,并贯彻其doFilter方法。
  • 每当 web.xml
    文件被使用<filter>和<filter-mapping>元素对编写的filter类进行挂号,并设置它所能挡住的资源。

 

2.示例

(1)简单的Filter示例

  •  编写FilterDemo1类

澳门1495娱乐 3

 1 package com.oracle.filter;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.Filter;
 6 import javax.servlet.FilterChain;
 7 import javax.servlet.FilterConfig;
 8 import javax.servlet.ServletException;
 9 import javax.servlet.ServletRequest;
10 import javax.servlet.ServletResponse;
11 
12 public class FilterDemo1 implements Filter{
13 
14     /*
15      * 对Filter的整个生命周期了解的一个案例
16      * 注意事项:
17      * 1.实现Filter接口时导入的是javax.servlet.Filter包
18      * 2.方法均有web服务器自动调用,不需我们手动调用
19      * 3.init方法中一般写初始化参数,这里先不用,后面的例子再使用。
20      * 4.destroy方法一般不需要写任何代码
21      * 5.重写doFilter方法,可以写我们对拦截的请求和响应的处理动作。
22      * 6.写完该类后配置filter,在web.xml中配置。
23      * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
24      */
25     @Override
26     public void init(FilterConfig filterConfig) throws ServletException {
27         // TODO Auto-generated method stub
28         System.out.println("FilterDemo1的init方法被调用");
29     }
30 
31     @Override
32     public void doFilter(ServletRequest request, ServletResponse response,
33             FilterChain chain) throws IOException, ServletException {
34         // TODO Auto-generated method stub
35         System.out.println("我是FilterDemo1,客户端向Servlet发送的请求被我拦截到了");
36         chain.doFilter(request, response);
37         System.out.println("我是FilterDemo1,Servlet向客户端发送的响应被我拦截到了");
38     }
39 
40     @Override
41     public void destroy() {
42         // TODO Auto-generated method stub
43         System.out.println("FilterDemo1的destroy方法被调用");
44     }
45 
46 }

澳门1495娱乐 4

  • 布局filter,在web.xml文件中在下面就段代码

澳门1495娱乐 5

1 <filter>
2       <filter-name>filterDemo1</filter-name>
3       <filter-class>com.oracle.filter.FilterDemo1</filter-class>
4   </filter>
5   <filter-mapping>
6       <filter-name>filterDemo1</filter-name>
7       <url-pattern>/*</url-pattern>
8       <!-- /*是对所有的文件进行拦截 -->
9   </filter-mapping>

澳门1495娱乐 6

  • 控制台结果

澳门1495娱乐 7

  • 分开
    析:从点结果可以视,在服务器启动时,就调用了init方法,当访问页面时,该过滤器拦截到要执行doFilter方法,在该方式中,使用
    doFilter方法,当回响应后,继续执行剩下的代码,执行就后用应传为客户端。当关闭服务器时,服务器即调用了destroy方法。

 

(2)Filter链示例

  • 编写FilterDemo1类

澳门1495娱乐 8

 1 package com.oracle.filter;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.Filter;
 6 import javax.servlet.FilterChain;
 7 import javax.servlet.FilterConfig;
 8 import javax.servlet.ServletException;
 9 import javax.servlet.ServletRequest;
10 import javax.servlet.ServletResponse;
11 
12 public class FilterDemo1 implements Filter{
13 
14     /*
15      * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
16      */
17     @Override
18     public void init(FilterConfig filterConfig) throws ServletException {
19         // TODO Auto-generated method stub
20     }
21 
22     @Override
23     public void doFilter(ServletRequest request, ServletResponse response,
24             FilterChain chain) throws IOException, ServletException {
25         // TODO Auto-generated method stub
26         System.out.println("我是FilterDemo1,客户端向Servlet发送的请求被我拦截到了");
27         //对请求放行,进入下一个过滤器FilterDemo2
28         chain.doFilter(request, response);
29         System.out.println("我是FilterDemo1,Servlet向客户端发送的响应被我拦截到了");
30     }
31 
32     @Override
33     public void destroy() {
34         // TODO Auto-generated method stub
35     }
36 
37 }

澳门1495娱乐 9

  •  编写FilterDemo2类

澳门1495娱乐 10

 1 package com.oracle.filter;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.Filter;
 6 import javax.servlet.FilterChain;
 7 import javax.servlet.FilterConfig;
 8 import javax.servlet.ServletException;
 9 import javax.servlet.ServletRequest;
10 import javax.servlet.ServletResponse;
11 
12 public class FilterDemo2 implements Filter{
13 
14     @Override
15     public void init(FilterConfig filterConfig) throws ServletException {
16         // TODO Auto-generated method stub
17         
18     }
19 
20     @Override
21     public void doFilter(ServletRequest request, ServletResponse response,
22             FilterChain chain) throws IOException, ServletException {
23         // TODO Auto-generated method stub
24         System.out.println("我是FilterDemo2,客户端向Servlet发送的请求被我拦截到了");
25         //对请求放行,进入Servlet
26         chain.doFilter(request, response);
27         System.out.println("我是FilterDemo2,Servlet向客户端发送的响应被我拦截到了");
28     }
29 
30     @Override
31     public void destroy() {
32         // TODO Auto-generated method stub
33         
34     }
35 
36 }

澳门1495娱乐 11

  •  配置filter,在web.xml文件被进入下面就段代码

澳门1495娱乐 12

 1 <filter>
 2       <filter-name>filterDemo1</filter-name>
 3       <filter-class>com.oracle.filter.FilterDemo1</filter-class>
 4   </filter>
 5   <filter>
 6       <filter-name>filterDemo2</filter-name>
 7       <filter-class>com.oracle.filter.FilterDemo2</filter-class>
 8   </filter>
 9   
10   <filter-mapping>
11       <filter-name>filterDemo1</filter-name>
12       <url-pattern>/*</url-pattern>
13       <!-- /*是对所有的文件进行拦截 -->
14   </filter-mapping>
15   <filter-mapping>
16       <filter-name>filterDemo2</filter-name>
17       <url-pattern>/*</url-pattern>
18       <!-- /*是对所有的文件进行拦截 -->
19   </filter-mapping>

澳门1495娱乐 13

  •  控制台澳门1495娱乐结果

澳门1495娱乐 14

  • 划分
    析:当有差不多个过滤器对同一个央进行拦截时,根据web.xml文件中<filter-mapping>的布顺序,谁在眼前,先实施谁。当第
    同过滤器拦截成功后,会实施doFilter方法,该措施吃,调用chain.doFilter方法,会拿该要放行给下一个过滤器,依次执行,直到执行
    到结尾一个过滤器,当最后一个过滤器调用chain.doFilter方法时,请求会给放行给Servlet,当Servlet处理回来响应信息时,先回到
    回到最后执行的过滤器,继续执行该过滤器剩下的代码。依次返回,直到回到到第一独过滤器,最后回到给客户端。

 

 (3)禁用所有动态页面的休养存过滤器

  • 编写FilterDemo3类

澳门1495娱乐 15

 1 package com.oracle.filter;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.Filter;
 6 import javax.servlet.FilterChain;
 7 import javax.servlet.FilterConfig;
 8 import javax.servlet.ServletException;
 9 import javax.servlet.ServletRequest;
10 import javax.servlet.ServletResponse;
11 import javax.servlet.http.HttpServletResponse;
12 
13 public class FilterDemo3 implements Filter{
14 
15     @Override
16     public void init(FilterConfig filterConfig) throws ServletException {
17         // TODO Auto-generated method stub
18         
19     }
20 
21     @Override
22     public void doFilter(ServletRequest request, ServletResponse response,
23             FilterChain chain) throws IOException, ServletException {
24         // 在response的头部设置Cache-Control、Pragma和Expires即可取消缓存
25         HttpServletResponse resp = (HttpServletResponse)response;
26         resp.setHeader("Cache-Control", "no-cache");
27         resp.setHeader("Pragma", "no-cache");
28         resp.setDateHeader("Expires", -1);
29         chain.doFilter(request, resp);
30     }
31 
32     @Override
33     public void destroy() {
34         // TODO Auto-generated method stub
35         
36     }
37 
38 }

澳门1495娱乐 16

  •  配置filter,在web.xml文件中在下面就段代码

澳门1495娱乐 17

1 <filter>
2       <filter-name>filterDemo3</filter-name>
3       <filter-class>com.oracle.filter.FilterDemo3</filter-class>
4   </filter>
5   
6   <filter-mapping>
7       <filter-name>filterDemo3</filter-name>
8       <url-pattern>/*</url-pattern>
9   </filter-mapping>

澳门1495娱乐 18

 

(4) 分IP统计网站的拜会次数过滤器

  • 编写FilterDemo4类

澳门1495娱乐 19

 1 package com.oracle.filter;
 2 
 3 import java.io.IOException;
 4 import java.util.HashMap;
 5 import java.util.Map;
 6 
 7 import javax.servlet.Filter;
 8 import javax.servlet.FilterChain;
 9 import javax.servlet.FilterConfig;
10 import javax.servlet.ServletContext;
11 import javax.servlet.ServletException;
12 import javax.servlet.ServletRequest;
13 import javax.servlet.ServletResponse;
14 
15 public class FilterDemo4 implements Filter{
16 
17     private FilterConfig filterConfig;
18     @Override
19     public void init(FilterConfig filterConfig) throws ServletException {
20         // TODO Auto-generated method stub
21 
22         //初始化参数,ipCount用来存放ip及访问次数
23         ServletContext application = filterConfig.getServletContext();
24         Map<String,Integer> ipCount = new HashMap<String,Integer>();
25         application.setAttribute("ipCount",ipCount);
26         this.filterConfig = filterConfig;
27     }
28 
29     
30     @Override
31     public void doFilter(ServletRequest request, ServletResponse response,
32             FilterChain chain) throws IOException, ServletException {
33         // TODO Auto-generated method stub
34         ServletContext application = filterConfig.getServletContext();
35         Map<String,Integer> ipCount = (HashMap<String,Integer>)application.getAttribute("ipCount");
36         String ip = request.getRemoteAddr();
37         Integer count = ipCount.get(ip);
38         if(count != null){
39             //Map中存在该ip
40             count = count + 1;
41         }else{
42             count = 1;
43         }
44         ipCount.put(ip, count);
45         application.setAttribute("ipCount",ipCount);
46         chain.doFilter(request, response);
47     }
48 
49     @Override
50     public void destroy() {
51         // TODO Auto-generated method stub
52 
53     }
54 
55 }

澳门1495娱乐 20

  • 编写index.jsp页面

澳门1495娱乐 21

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 3 <%
 4 String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7 
 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 9 <html>
10   <head>
11     <base href="<%=basePath%>">
12     
13     <title>My JSP 'index.jsp' starting page</title>
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22   </head>
23   
24   <body>
25     <h1>分IP统计网站浏览次数</h1>
26    <table border="1" width="400">
27        <tr>
28            <th>IP地址</th>
29            <th>浏览次数</th>
30        </tr>
31        <c:forEach items="${ipCount}" var="m">
32            <tr>
33                <td>${m.key}</td>
34                <td>${m.value}</td>
35            </tr>
36        </c:forEach>
37    </table>
38   </body>
39 </html>

澳门1495娱乐 22

  • 布filter,在web.xml文件被入下面就段代码

澳门1495娱乐 23

1 <filter>
2       <filter-name>filterDemo4</filter-name>
3       <filter-class>com.oracle.filter.FilterDemo4</filter-class>
4   </filter>
5   
6   <filter-mapping>
7       <filter-name>filterDemo4</filter-name>
8       <url-pattern>/*</url-pattern>
9   </filter-mapping>

澳门1495娱乐 24

  •  网页结果

 澳门1495娱乐 25

 

(5)自动登录

  • 编写AutoLoginFilter类

澳门1495娱乐 26

 1 package com.oracle.filter;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.Filter;
 6 import javax.servlet.FilterChain;
 7 import javax.servlet.FilterConfig;
 8 import javax.servlet.ServletException;
 9 import javax.servlet.ServletRequest;
10 import javax.servlet.ServletResponse;
11 import javax.servlet.http.Cookie;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpSession;
14 
15 import com.oracle.biz.UserInfoBiz;
16 import com.oracle.biz.impl.UserInfoBizImpl;
17 import com.oracle.entity.UserInfo;
18 import com.oracle.util.CookieUtil;
19 
20 public class AutoLoginFilter implements Filter{
21 
22     @Override
23     public void init(FilterConfig filterConfig) throws ServletException {
24         // TODO Auto-generated method stub
25         
26     }
27 
28     @Override
29     public void doFilter(ServletRequest request, ServletResponse response,
30             FilterChain chain) throws IOException, ServletException {
31         // TODO Auto-generated method stub
32         //先判断session中是否存在,存在则放行,不存在则判断cookie中是否存在用户名密码,存在则到数据库中查询是否正确,正确则存入session并放行,不正确则放行
33         HttpServletRequest req = (HttpServletRequest)request;
34         HttpSession session = req.getSession();
35         UserInfo user = (UserInfo)session.getAttribute("user");
36         if(user != null){
37             chain.doFilter(req, response);
38         }else{
39             //session中不存在用户
40             Cookie[] cookies = req.getCookies();
41             Cookie cookie = CookieUtil.findCookie(cookies, "autoLogin");
42             if(cookie!=null){
43                 //在cookie中找到该用户
44                 UserInfoBiz ubiz = new UserInfoBizImpl();
45                 String name = cookie.getValue().split("#oracle#")[0];
46                 String pwd = cookie.getValue().split("#oracle#")[1];
47                 String msg = ubiz.login(name, pwd);
48                 if("登陆成功!".equals(msg)){
49                     user = ubiz.getByName(name);
50                     session.setAttribute("user", user);
51                     chain.doFilter(req, response);
52                 }else{
53                     chain.doFilter(req, response);
54                 }
55             }else{
56                 //没有找到该客户
57                 chain.doFilter(req, response);
58             }
59         }
60     }
61 
62     @Override
63     public void destroy() {
64         // TODO Auto-generated method stub
65         
66     }
67 
68     
69 }

澳门1495娱乐 27

 

  • 编写DoLoginServlet

澳门1495娱乐 28

 1 package com.oracle.servlet;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.ServletException;
 6 import javax.servlet.http.Cookie;
 7 import javax.servlet.http.HttpServlet;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10 import javax.servlet.http.HttpSession;
11 
12 import com.oracle.biz.UserInfoBiz;
13 import com.oracle.biz.impl.UserInfoBizImpl;
14 import com.oracle.entity.UserInfo;
15 
16 public class DoLoginServlet extends HttpServlet {
17 
18     /**
19      * Constructor of the object.
20      */
21     public DoLoginServlet() {
22         super();
23     }
24 
25     /**
26      * Destruction of the servlet. <br>
27      */
28     public void destroy() {
29         super.destroy(); // Just puts "destroy" string in log
30         // Put your code here
31     }
32 
33     /**
34      * The doGet method of the servlet. <br>
35      *
36      * This method is called when a form has its tag value method equals to get.
37      * 
38      * @param request the request send by the client to the server
39      * @param response the response send by the server to the client
40      * @throws ServletException if an error occurred
41      * @throws IOException if an error occurred
42      */
43     public void doGet(HttpServletRequest request, HttpServletResponse response)
44             throws ServletException, IOException {
45 
46         doPost(request,response);
47     }
48 
49     /**
50      * The doPost method of the servlet. <br>
51      *
52      * This method is called when a form has its tag value method equals to post.
53      * 
54      * @param request the request send by the client to the server
55      * @param response the response send by the server to the client
56      * @throws ServletException if an error occurred
57      * @throws IOException if an error occurred
58      */
59     public void doPost(HttpServletRequest request, HttpServletResponse response)
60             throws ServletException, IOException {
61 
62         HttpSession session = request.getSession();
63         request.setCharacterEncoding("UTF-8");
64         response.setCharacterEncoding("UTF-8");
65         String name = request.getParameter("myname");
66         String pwd = request.getParameter("pwd");
67         String autoLogin = request.getParameter("autoLogin");
68         UserInfoBiz ubiz = new UserInfoBizImpl();
69         //ubiz.login(name, pwd):判断用户是否登陆成功,返回一个字符串。成功则返回"登陆成功!",不成功则返回对应的错误提示。
70         String msg = ubiz.login(name, pwd);
71         if("登陆成功!".equals(msg)){
72             UserInfo user = ubiz.getByName(name);
73             session.setAttribute("user", user);
74             if("true".equals(autoLogin)){
75                 //利用cookie记住用户名和密码
76                 Cookie cookie = new Cookie("autoLogin",user.getUserName()+"#oracle#"+user.getPassword());
77                 //设置有效时间
78                 cookie.setMaxAge(60*60*24);
79                 //将cookie回写到浏览器
80                 response.addCookie(cookie);
81             }
82             response.sendRedirect("success.jsp");
83         }else{
84             request.setAttribute("msg", msg);
85             request.getRequestDispatcher("login.jsp").forward(request, response);
86         }
87     }
88 
89     /**
90      * Initialization of the servlet. <br>
91      *
92      * @throws ServletException if an error occurs
93      */
94     public void init() throws ServletException {
95         // Put your code here
96     }
97 
98 }

澳门1495娱乐 29

  •  编写CookieUtil

澳门1495娱乐 30

 1 package com.oracle.util;
 2 
 3 import javax.servlet.http.Cookie;
 4 
 5 
 6 
 7 public class CookieUtil {
 8 
 9     public static Cookie findCookie(Cookie[] cookies,String name){
10         if(cookies==null){
11             return null;
12         }else{
13             for(Cookie cookie:cookies){
14                 if(cookie.getName().equals(name)){
15                     return cookie;
16                 }
17             }
18             return null;
19         }
20     }
21 }

澳门1495娱乐 31

  • 编写login.jsp

澳门1495娱乐 32

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'login.jsp' starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22 
23   </head>
24   
25   <body>
26     <form action="doLogin" method="post">
27         用户名<input name="myname"><br/>
28         密&nbsp;码<input type="password" name="pwd"><br/>
29         <input type="checkBox" name="autoLogin" value="true">自动登录<br/>
30         <input type="submit" value="登陆"> 
31     </form>
32   </body>
33 </html>

澳门1495娱乐 33

  • 编写success.jsp

澳门1495娱乐 34

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 3 <%
 4 String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7 
 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 9 <html>
10   <head>
11     <base href="<%=basePath%>">
12     
13     <title>My JSP 'success.jsp' starting page</title>
14     
15     <meta http-equiv="pragma" content="no-cache">
16     <meta http-equiv="cache-control" content="no-cache">
17     <meta http-equiv="expires" content="0">    
18     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
19     <meta http-equiv="description" content="This is my page">
20     <!--
21     <link rel="stylesheet" type="text/css" href="styles.css">
22     -->
23 
24   </head>
25   
26   <body>
27     <c:if test="${empty user}">
28         <h2>您还未登陆,请去<a href="login.jsp">登陆</a></h2>
29     </c:if>
30     <c:if test="${not empty user}">
31         <h2>欢迎你${user.userName}</h2>
32     </c:if>
33   </body>
34 </html>

澳门1495娱乐 35

  • 布置filter和servlet,在web.xml文件中进入下面的代码

澳门1495娱乐 36

 1 <servlet>
 2     <description>This is the description of my J2EE component</description>
 3     <display-name>This is the display name of my J2EE component</display-name>
 4     <servlet-name>DoLoginServlet</servlet-name>
 5     <servlet-class>com.oracle.servlet.DoLoginServlet</servlet-class>
 6   </servlet>
 7 
 8   <servlet-mapping>
 9     <servlet-name>DoLoginServlet</servlet-name>
10     <url-pattern>/doLogin</url-pattern>
11   </servlet-mapping>    
12   
13   <filter>
14       <filter-name>AutoLoginFilter</filter-name>
15       <filter-class>com.oracle.filter.AutoLoginFilter</filter-class>
16   </filter>
17   <filter-mapping>
18       <filter-name>AutoLoginFilter</filter-name>
19       <url-pattern>/*</url-pattern>
20   </filter-mapping>

澳门1495娱乐 37

 

(6)处理网站的Get和Post请求乱码


  • 路:增强request对象的getParameter方法等艺术。编写一个类MyHttpServletRequest实现
    HttpServletRequestWrapper,重写她的getParameter方法等方法,在这些方式吃针对不同方法提交的数码开展转码。在过滤
    器中,将request强制转换成为MyHttpServletRequest类型的靶子,这样,当还以getParameter等方式时其实调用的凡
    你再度写后底getParametr方法,也就是就处理过之数,这样即便甭还担心乱码的问题了。
  • MyHttpServletRequest类

澳门1495娱乐 38

 1 package com.oracle.bookshop.filter;
 2 
 3 import java.io.UnsupportedEncodingException;
 4 import java.util.Map;
 5 
 6 import javax.servlet.http.HttpServletRequest;
 7 import javax.servlet.http.HttpServletRequestWrapper;
 8 
 9 
10 public class MyHttpServletRequest extends HttpServletRequestWrapper {
11     /*
12      * 该类重写
13      */
14     private HttpServletRequest request;
15 
16     private boolean hasEncode;
17 
18     public MyHttpServletRequest(HttpServletRequest request) {
19         super(request);// super必须写
20         this.request = request;
21     }
22 
23     // 对需要增强方法进行覆盖
24     @Override
25     public Map getParameterMap() {
26         // 先获得请求方式
27         String method = request.getMethod();
28         if (method.equalsIgnoreCase("post")) {
29             // post请求
30             try {
31                 // 处理post乱码
32                 request.setCharacterEncoding("utf-8");
33                 return request.getParameterMap();
34             } catch (UnsupportedEncodingException e) {
35                 e.printStackTrace();
36             }
37         } else if (method.equalsIgnoreCase("get")) {
38             // get请求
39             Map<String, String[]> parameterMap = request.getParameterMap();
40             if (!hasEncode) { // 确保get手动编码逻辑只运行一次
41                 for (String parameterName : parameterMap.keySet()) {
42                     String[] values = parameterMap.get(parameterName);
43                     if (values != null) {
44                         for (int i = 0; i < values.length; i++) {
45                             try {
46                                 // 处理get乱码
47                                 values[i] = new String(values[i]
48                                         .getBytes("ISO-8859-1"), "utf-8");
49                             } catch (UnsupportedEncodingException e) {
50                                 e.printStackTrace();
51                             }
52                         }
53                     }
54                 }
55                 hasEncode = true;
56             }
57             return parameterMap;
58         }
59 
60         return super.getParameterMap();
61     }
62 
63     @Override
64     public String getParameter(String name) {
65         Map<String, String[]> parameterMap = getParameterMap();
66         String[] values = parameterMap.get(name);
67         if (values == null) {
68             return null;
69         }
70         return values[0]; // 取回参数的第一个值
71     }
72 
73     @Override
74     public String[] getParameterValues(String name) {
75         Map<String, String[]> parameterMap = getParameterMap();
76         String[] values = parameterMap.get(name);
77         return values;
78     }
79 
80 }

澳门1495娱乐 39

  • 编写CharacterEnodingFilter

澳门1495娱乐 40

 1 package com.oracle.bookshop.filter;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.Filter;
 6 import javax.servlet.FilterChain;
 7 import javax.servlet.FilterConfig;
 8 import javax.servlet.ServletException;
 9 import javax.servlet.ServletRequest;
10 import javax.servlet.ServletResponse;
11 import javax.servlet.http.HttpServletRequest;
12 
13 public class CharacterEnodingFilter implements Filter{
14 
15     @Override
16     public void destroy() {
17         // TODO Auto-generated method stub
18         
19     }
20 
21     @Override
22     public void doFilter(ServletRequest request, ServletResponse response,
23             FilterChain chain) throws IOException, ServletException {
24         // TODO Auto-generated method stub
25         HttpServletRequest req = (HttpServletRequest)request;
26         MyHttpServletRequest myreq = new MyHttpServletRequest(req);
27         chain.doFilter(myreq, response);
28         
29     }
30 
31     @Override
32     public void init(FilterConfig arg0) throws ServletException {
33         // TODO Auto-generated method stub
34         
35     }
36 
37     
38 }

澳门1495娱乐 41