将登陆信息写下Cookie

将登陆信息写入Cookie
SendServlet.java:
package com.chen.test;

import java.io.IOException;
import java.io.PrintWriter;

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 SendServlet extends HttpServlet {

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


	public void processRequest(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		
		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		
		PrintWriter out = response.getWriter();
		
		try {
				String username = request.getParameter("username");
				String password = request.getParameter("password");
				
				
				Cookie[] cookies = request.getCookies();
				String[]  info = null;
				if(cookies != null){
					for(int i =0; i < cookies.length;i++){
						if("userinfo".equals(cookies[i].getName())){
							info = cookies[i].getValue().split(",");
							username = info[0];
							password = info[1];
/*							System.out.println("username="+username);
							System.out.println("password="+password);*/
							break;
						}
					}
				}
				
				
				if((null != username && !"".equals(username)) && (null != password && !"".equals(password))){
					if(username.equals("chen") && password.equals("chen")){
						Cookie userinfo = new Cookie("userinfo", username+","+password);
						userinfo.setMaxAge(60*60*24);
						/*userinfo.setPath("/");
						userinfo.setDomain("127.0.0.1");*/
						response.addCookie(userinfo);
						request.setAttribute("username", username);
						request.getRequestDispatcher("success.jsp").forward(request, response);
					}
				}
				
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.processRequest(request, response);
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.processRequest(request, response);
	}


	public void init() throws ServletException {
	}

}

index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%														
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  	<base href="<%=basePath%>">
  </head>
  
  <body>
  	<form action="SendServlet" method="post">
  		username:<input type="text" name="username"><br/>
  		password:<input type="text" name="password"><br/>
  		<input type="submit" value="submit">	<br/>
  	</form> 
  </body>
</html>

success.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <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>
    welcome <%=request.getAttribute("username") %> !
  </body>
</html>