java cookie包_Java cookie的使用

本文详细介绍了Java中Cookie的使用,包括设置、读取和删除Cookie的方法,并通过一个模拟126邮箱登录的小例子展示了Cookie在用户自动登录功能中的应用。通过设置Cookie的生命周期和路径,可以实现用户信息的持久化存储和特定路径下的操作。
摘要由CSDN通过智能技术生成

转自:http://www.blogjava.net/tscfengkui/archive/2011/01/21/343341.html

很感谢,让小白明白其中道理

Java cookie的使用

1.cookie是什么?

cookie是web应用当中非常常用的一种技术,用于储存某些特定的用户信息。

2.cookie的作用?

在用户登陆时将用户的信息存放在cookie中,用户在一定的时间中再次登陆时不需要输入用户名和密码直接跳转到下一个界面。

3.设置cookie

Cookie cookie = new Cookie("key", "value");

cookie.setMaxAge(saveTime*24*60*60);

生存周期默认时间为秒,如果设置为负值的话,则为浏览器进程Cookie(内存中保存),关闭浏览器就失效。

cookie.setPath("/test/test2");

设置Cookie路径,不设置的话为当前路径(对于Servlet来说为request.getContextPath() + web.xml里配置的该Servlet的url-pattern路径部分) 。

response.addCookie(cookie);

4.读取cookie

该方法可以读取当前路径以及“直接父路径”的所有Cookie对象,如果没有任何Cookie的话,则返回null。如果设置了路径使用这个方法也没有值。

Cookie[] cookies = request.getCookies();

5.删除cookie

Cookie cookie = new Cookie("key", null);

cookie.setMaxAge(0);

设置为0为立即删除该Cookie;

cookie.setPath("/test/test2");

删除指定路径上的Cookie,不设置该路径,默认为删除当前路径Cookie;

response.addCookie(cookie);

下面看一个例子,这是模拟126邮箱登陆的小功能。建立一个名为AutoLoginFilter的project,包结构如下:

6478b5793002a89603c0dbfb70b6e1d1.png

project下有三个java文件、两个jsp还有个html,

CheckLogin.java代码如下:

1fa987a29c6482f53d401256f96355eb.pngpublic class CheckLogin { 

d18c02628675d0a2c816449d98bda930.png

97e794c86028c5f5b5461ae5ef440a4c.png     public static boolean login(String username, String password) {   

97e794c86028c5f5b5461ae5ef440a4c.png            if ("admin".equals(username) && "123456".equals(password)) {   

d18c02628675d0a2c816449d98bda930.png                return true;   

97e794c86028c5f5b5461ae5ef440a4c.png            } else {   

d18c02628675d0a2c816449d98bda930.png                return false;   

ecedf933ec37d714bd4c2545da43add2.png            }   

ecedf933ec37d714bd4c2545da43add2.png        }   

d18c02628675d0a2c816449d98bda930.png

8f1ba5b45633e9678d1db480c16cae3f.png

4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.png

IndexFilter.java代码如下:

4f1150b881333f12a311ae9ef34da474.pngpackage com.bx.course;

1fa987a29c6482f53d401256f96355eb.png/**

d18c02628675d0a2c816449d98bda930.png* Filter可以实现对请求的过滤和重定向等,也就是说可以操作request和response,session等对象,listner只能监听到以上对象的属性的修改。

8f1ba5b45633e9678d1db480c16cae3f.png*/ 

4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.pngimport java.io.IOException;   

4f1150b881333f12a311ae9ef34da474.pngimport javax.servlet.Filter;   

4f1150b881333f12a311ae9ef34da474.pngimport javax.servlet.FilterChain;   

4f1150b881333f12a311ae9ef34da474.pngimport javax.servlet.FilterConfig;   

4f1150b881333f12a311ae9ef34da474.pngimport javax.servlet.ServletException;   

4f1150b881333f12a311ae9ef34da474.pngimport javax.servlet.ServletRequest;   

4f1150b881333f12a311ae9ef34da474.pngimport javax.servlet.ServletResponse;   

4f1150b881333f12a311ae9ef34da474.pngimport javax.servlet.http.Cookie;   

4f1150b881333f12a311ae9ef34da474.pngimport javax.servlet.http.HttpServletRequest;   

4f1150b881333f12a311ae9ef34da474.pngimport javax.servlet.http.HttpServletResponse;   

1fa987a29c6482f53d401256f96355eb.pngpublic class IndexFilter implements Filter {   

97e794c86028c5f5b5461ae5ef440a4c.png    public void destroy() {   

d18c02628675d0a2c816449d98bda930.png        // TODO Auto-generated method stub   

ecedf933ec37d714bd4c2545da43add2.png    }   

d18c02628675d0a2c816449d98bda930.png    public void doFilter(ServletRequest arg0, ServletResponse arg1,   

97e794c86028c5f5b5461ae5ef440a4c.png            FilterChain arg2) throws IOException, ServletException { 

d18c02628675d0a2c816449d98bda930.png        System.out.println("every request pass here haha");

d18c02628675d0a2c816449d98bda930.png        HttpServletRequest request = (HttpServletRequest) arg0;   

d18c02628675d0a2c816449d98bda930.png        HttpServletResponse response = (HttpServletResponse) arg1;   

d18c02628675d0a2c816449d98bda930.png        Cookie[] cookies = request.getCookies();  

d18c02628675d0a2c816449d98bda930.png//        Cookie cookie = new Cookie("user",null);

d18c02628675d0a2c816449d98bda930.png//        cookie.setMaxAge(0);

d18c02628675d0a2c816449d98bda930.png//        response.addCookie(cookie);

d18c02628675d0a2c816449d98bda930.png        String[] cooks = null;   

d18c02628675d0a2c816449d98bda930.png        String username = null;   

d18c02628675d0a2c816449d98bda930.png        String password = null; 

97e794c86028c5f5b5461ae5ef440a4c.png        if (cookies != null) {   

97e794c86028c5f5b5461ae5ef440a4c.png            for (Cookie coo : cookies) {   

d18c02628675d0a2c816449d98bda930.png                String aa = coo.getValue(); 

d18c02628675d0a2c816449d98bda930.png                System.out.println("1");

d18c02628675d0a2c816449d98bda930.png                cooks = aa.split("==");   

97e794c86028c5f5b5461ae5ef440a4c.png                if (cooks.length == 2) {

d18c02628675d0a2c816449d98bda930.png                    System.out.println(cooks[0]+cooks[1]);

d18c02628675d0a2c816449d98bda930.png                    username = cooks[0];   

d18c02628675d0a2c816449d98bda930.png                    password = cooks[1];   

ecedf933ec37d714bd4c2545da43add2.png                }   

ecedf933ec37d714bd4c2545da43add2.png            }   

ecedf933ec37d714bd4c2545da43add2.png        } 

d18c02628675d0a2c816449d98bda930.png        System.out.println("cookie username | " + username);

d18c02628675d0a2c816449d98bda930.png        System.out.println("cookie password | " + password);

97e794c86028c5f5b5461ae5ef440a4c.png        if (CheckLogin.login(username, password)) {

d18c02628675d0a2c816449d98bda930.png            System.err.println("check successfully cookie data ");

d18c02628675d0a2c816449d98bda930.png            request.getSession().setAttribute("username",username);   

d18c02628675d0a2c816449d98bda930.png            request.getRequestDispatcher("/main126.jsp").forward(request, response);

97e794c86028c5f5b5461ae5ef440a4c.png        }else{   

d18c02628675d0a2c816449d98bda930.png            arg2.doFilter(request,response );   

ecedf933ec37d714bd4c2545da43add2.png        }   

ecedf933ec37d714bd4c2545da43add2.png    }   

97e794c86028c5f5b5461ae5ef440a4c.png    public void init(FilterConfig arg0) throws ServletException {   

d18c02628675d0a2c816449d98bda930.png        // TODO Auto-generated method stub   

ecedf933ec37d714bd4c2545da43add2.png    }   

8f1ba5b45633e9678d1db480c16cae3f.png

4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.png

LoginServlet.java代码如下:

4f1150b881333f12a311ae9ef34da474.pngpackage com.bx.course;

4f1150b881333f12a311ae9ef34da474.pngimport java.io.IOException;   

4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.pngimport javax.servlet.ServletException;   

4f1150b881333f12a311ae9ef34da474.pngimport javax.servlet.http.Cookie;   

4f1150b881333f12a311ae9ef34da474.pngimport javax.servlet.http.HttpServlet;   

4f1150b881333f12a311ae9ef34da474.pngimport javax.servlet.http.HttpServletRequest;   

4f1150b881333f12a311ae9ef34da474.pngimport javax.servlet.http.HttpServletResponse;   

1fa987a29c6482f53d401256f96355eb.pngpublic class LoginServlet extends HttpServlet {   

d18c02628675d0a2c816449d98bda930.png    public void doGet(HttpServletRequest request, HttpServletResponse response)   

97e794c86028c5f5b5461ae5ef440a4c.png            throws ServletException, IOException {   

d18c02628675d0a2c816449d98bda930.png        this.doPost(request, response);   

ecedf933ec37d714bd4c2545da43add2.png    }   

d18c02628675d0a2c816449d98bda930.png    public void doPost(HttpServletRequest request, HttpServletResponse response)   

97e794c86028c5f5b5461ae5ef440a4c.png            throws ServletException, IOException {   

d18c02628675d0a2c816449d98bda930.png           String username=request.getParameter("username");   

d18c02628675d0a2c816449d98bda930.png           String password=request.getParameter("password");   

d18c02628675d0a2c816449d98bda930.png           String savetime=request.getParameter("saveTime"); 

d18c02628675d0a2c816449d98bda930.png           System.out.println("usrename  "+username+"  password "+password);

97e794c86028c5f5b5461ae5ef440a4c.png           if(CheckLogin.login(username, password)){   

97e794c86028c5f5b5461ae5ef440a4c.png               if(null!=savetime){   

d18c02628675d0a2c816449d98bda930.png                   int saveTime=Integer.parseInt(savetime);//这里接受的表单值为天来计算的   

d18c02628675d0a2c816449d98bda930.png                   int seconds=saveTime*24*60*60;   

d18c02628675d0a2c816449d98bda930.png                   Cookie cookie = new Cookie("user", username+"=="+password); 

d18c02628675d0a2c816449d98bda930.png                   cookie.setMaxAge(seconds);                      

d18c02628675d0a2c816449d98bda930.png                   response.addCookie(cookie);   

ecedf933ec37d714bd4c2545da43add2.png               }   

d18c02628675d0a2c816449d98bda930.png               request.setAttribute("username",username);   

d18c02628675d0a2c816449d98bda930.png               request.getRequestDispatcher("/main126.jsp").forward(request,response);   

97e794c86028c5f5b5461ae5ef440a4c.png           }else{   

d18c02628675d0a2c816449d98bda930.png               request.getRequestDispatcher("/failure.jsp").forward(request,response);   

ecedf933ec37d714bd4c2545da43add2.png           }   

ecedf933ec37d714bd4c2545da43add2.png    }   

8f1ba5b45633e9678d1db480c16cae3f.png}   

4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.png

web.xml配置文件代码如下:

4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.png        loginFilter

4f1150b881333f12a311ae9ef34da474.png        com.bx.course.IndexFilter

4f1150b881333f12a311ae9ef34da474.png    

4f1150b881333f12a311ae9ef34da474.png    

4f1150b881333f12a311ae9ef34da474.png        loginFilter

4f1150b881333f12a311ae9ef34da474.png        /login.html

4f1150b881333f12a311ae9ef34da474.png    

4f1150b881333f12a311ae9ef34da474.png    

4f1150b881333f12a311ae9ef34da474.png        LoginServlet

4f1150b881333f12a311ae9ef34da474.png        com.bx.course.LoginServlet

4f1150b881333f12a311ae9ef34da474.png     

4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.png    

4f1150b881333f12a311ae9ef34da474.png        LoginServlet

4f1150b881333f12a311ae9ef34da474.png        /login.do

4f1150b881333f12a311ae9ef34da474.png    

4f1150b881333f12a311ae9ef34da474.png    

4f1150b881333f12a311ae9ef34da474.png        login.html

4f1150b881333f12a311ae9ef34da474.png     

4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.png    

4f1150b881333f12a311ae9ef34da474.png        main126.jsp

4f1150b881333f12a311ae9ef34da474.png     

4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.png

login.html代码如下:

4f1150b881333f12a311ae9ef34da474.png  

4f1150b881333f12a311ae9ef34da474.png  

  

4f1150b881333f12a311ae9ef34da474.png    

4f1150b881333f12a311ae9ef34da474.png  

  

4f1150b881333f12a311ae9ef34da474.png  

  

4f1150b881333f12a311ae9ef34da474.png    126邮箱登录

4f1150b881333f12a311ae9ef34da474.png           用户名:

4f1150b881333f12a311ae9ef34da474.png           密    码:

4f1150b881333f12a311ae9ef34da474.png     

4f1150b881333f12a311ae9ef34da474.png        一年  

4f1150b881333f12a311ae9ef34da474.png        半年  

4f1150b881333f12a311ae9ef34da474.png        一个月    

4f1150b881333f12a311ae9ef34da474.png        一周  

4f1150b881333f12a311ae9ef34da474.png   

4f1150b881333f12a311ae9ef34da474.png   

4f1150b881333f12a311ae9ef34da474.png  

  

4f1150b881333f12a311ae9ef34da474.png    

4f1150b881333f12a311ae9ef34da474.png  

4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.png

main126.jsp代码如下:

4f1150b881333f12a311ae9ef34da474.png  

4f1150b881333f12a311ae9ef34da474.png  

  

4f1150b881333f12a311ae9ef34da474.png     

测试cookie  

4f1150b881333f12a311ae9ef34da474.png    

4f1150b881333f12a311ae9ef34da474.png  

  

4f1150b881333f12a311ae9ef34da474.png         

登录成功,欢迎${username}的到来 126邮箱

4f1150b881333f12a311ae9ef34da474.png         

测试cookie的功能

4f1150b881333f12a311ae9ef34da474.png    

4f1150b881333f12a311ae9ef34da474.png  

4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.png

failure.jsp代码如下:

4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.png    Login failure 

4f1150b881333f12a311ae9ef34da474.png  

运行效果:

显示如下界面:

47fbd86a44aa2bd9e775bcc0f05d6772.png

输入用户名:admin    密码:123456 选择保存时间,然后点击登陆,就会进入以下界面:

3626a731b89cea9edb4b9585caee9a51.png

就会直接进入登陆成功界面。如果用户名或密码不正确则会进入以下界面:

fa96bd2544aee91fb129fe2d372595b9.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值