用cookie和session模拟自动登录的效果

(1)Servlet

package com.servlet;

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;
import javax.servlet.http.HttpSession;

public class CodeLogin extends HttpServlet {

	//Servlet会调用get方法
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		//取到cookie中的用户名和密码
		Cookie[] cookies = request.getCookies();

		String username = null;
		String password = null;

		HttpSession session = request.getSession(false);

		if (null != cookies && cookies.length > 0) {

			for (Cookie c : cookies) {

				// 如果有cookie就直接进入成功页面
				if (c.getName().equals("username")) {

					// session.setAttribute("isLogin","true");//如果存在cookie则说明用户登录成功
					username = c.getValue();

				}

				if (c.getName().equals("password")) {

					password = c.getValue();
				}
			}
			
			//将cookie中的用户名和密码和正确的用户名和密码进行比较
			if (isLogin(username, password)) {

				session.setAttribute("isLogin", "true");

				response.sendRedirect("sucess.jsp");//正确时,进入成功页面

				return;
			}
			request.getRequestDispatcher("codelogin.jsp").forward(request,
					response);//用户名和密码不一致时,进入登陆页面
		}
	}

	//判断是否登录
	public boolean isLogin(String username, String password) {

		if ("admin".equals(username) && "123".equals(password)) {
			return true;
		}
		return false;
	}

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

		// 设置编码格式
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");

		// false表示当前如果没有、session就不创建新的session
		HttpSession session = request.getSession(false);
		String code = (String) session.getAttribute("code");

		// 获得用户输入的参数
		String yan = request.getParameter("yan");
		String username = request.getParameter("username");
		String password = request.getParameter("password");

		if (code == null) {

			response
					.sendRedirect("codelogin.jsp?errorMessage=The checknumber is not correct");

			return;
		}

		if (yan.equals(code)) {

			// 验证码正确后移除session
			session.removeAttribute("code");

			if (isLogin(username, password)) {

				// 记录用户是否登录
				session.setAttribute("isLogin", "true");

				String save = request.getParameter("save");// 是否记住密码

				// 写cookie(将用户的姓名和密码写到cookie里)
				Cookie cookieUsername = new Cookie("username", username);
				Cookie cookiePassword = new Cookie("password", password);

				if (null != save) {

					if (save.equals("0")) {

						// 设置存活的时间
						cookieUsername.setMaxAge(60 * 60 * 24);// 保存一天
						cookiePassword.setMaxAge(60 * 60 * 24);

					} else if (save.equals("1")) {

						cookieUsername.setMaxAge(60 * 60 * 24 * 30);// 保存一月
						cookiePassword.setMaxAge(60 * 60 * 24 * 30);

					} else if (save.equals("2")) {

						cookieUsername.setMaxAge(60 * 60 * 24 * 365);// 保存一年
						cookiePassword.setMaxAge(60 * 60 * 24 * 365);
					}

				}

				// 设置路径
				cookieUsername.setPath("/");
				cookiePassword.setPath("/");

				response.addCookie(cookieUsername);
				response.addCookie(cookiePassword);

				response.sendRedirect("sucess.jsp");

			} else {

				request.setAttribute("errorMessage",
						"The username or password is not correct");
				request.getRequestDispatcher("codelogin.jsp").forward(request,
						response);

			}
		}
	}
}

 

 

(2)JSP页面

 

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

	//读取Cookies中的值
	Cookie[] cookies = request.getCookies();
	
	String username=null;
	String password=null;

	if (null != cookies && cookies.length > 0) {

		for (Cookie c : cookies) {
		
			//如果有cookie就直接进入成功页面
			if (c.getName().equals("username")) {

				//session.setAttribute("isLogin","true");//如果存在cookie则说明用户登录成功
				username=c.getValue();
				
			}
			
			if(c.getName().equals("password")){
			
				password=c.getValue();
			}
		}
		
		if(null!=username&&null!=password){
			
			response.sendRedirect("CodeLogin");//直接进入Servlet中进行登录判断
		}
	}
%>



	
		

		My JSP 'codelogin.jsp' starting page

		
		
		
		
		
		

	

	
		
<%=request.getParameter("errorMessage") == null ? "" : request.getParameter("errorMessage")%> <%=request.getAttribute("errorMessage") == null ? "" : request.getAttribute("errorMessage")%>
用户登录
用户名
密码
验证码 验证码
保存密码一天
保存密码一月
保存密码一年

 

你可能感兴趣的:(Servlet,JSP)