J2EE中的Cookie技术

本文提供了一个使用Java Servlet创建和读取HTTP Cookies的示例。通过一个简单的登录表单,如果用户名和密码正确并选中记住我选项,则将用户名和密码保存为Cookies。在后续请求中,可以通过读取这些Cookies来实现自动登录。
package action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CreateCookie extends HttpServlet {

	
	public CreateCookie() {
		super();
	}

	public void destroy() {
		super.destroy(); 
		
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
        String username=request.getParameter("username");
        String userpwd = request.getParameter("pwdname");
       if((username.equalsIgnoreCase("huoleihu"))&& (userpwd.equalsIgnoreCase("huoleihu"))){   
        if(request.getParameter("check")!=null){
        	Cookie cook=new Cookie("mingzi",username);
        	cook.setMaxAge(3600*24);
            response.addCookie(cook);
        	Cookie cook2=new Cookie("mima",userpwd);
        	cook.setMaxAge(3600*24);
            response.addCookie(cook2);
        }
        response.sendRedirect("success.jsp");
	}
       else{
    	   response.sendRedirect("shibai.jsp");
    	   
       }
      
	}

	public void init() throws ServletException {

	}

}

 

package action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ReadCookie extends HttpServlet {

	public ReadCookie() {
		super();
	}

	public void destroy() {
		super.destroy(); 

	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
         Cookie[] cookies=request.getCookies();
         String username=null;
         String userpwd=null;
        if(cookies!=null){
         for(int i=0;i<cookies.length;i++){
        	if(cookies[i].getName().equals("mingzi")){
        		username=cookies[i].getValue();
        	}
        	if(cookies[i].getName().equals("mima")){
        		userpwd=cookies[i].getValue();
        	}
         }
         }
         request.setAttribute("username", username);
         request.getRequestDispatcher("index.jsp").forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	}

	public void init() throws ServletException {

	}

}

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>
Cookie测试
</title>
</head>
<body>
<form action="CreateCookie" method="post" />

<input type="text" name="username" value="${username}" />
<input type="password" name="pwdname"/>
<input type="checkbox" name="check" />
<input type="submit" value="提交" />
 
</form>

</body>
</html>

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'shibai.jsp' starting page</title>

	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  账号密码错误 <br>
  </body>
</html>

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'success.jsp' starting page</title>

	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  恭喜您登陆成功! <br>
  </body>
</html>

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>CreateCookie</servlet-name>
    <servlet-class>action.CreateCookie</servlet-class>
  </servlet>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>ReadCookie</servlet-name>
    <servlet-class>action.ReadCookie</servlet-class>
  </servlet>


  <servlet-mapping>
    <servlet-name>CreateCookie</servlet-name>
    <url-pattern>/CreateCookie</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>ReadCookie</servlet-name>
    <url-pattern>/ReadCookie</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

  

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值