(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中进行登录判断
}
}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'codelogin.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>
<font color="red"><%=request.getParameter("errorMessage") == null ? ""
: request.getParameter("errorMessage")%></font>
<font color="red"><%=request.getAttribute("errorMessage") == null ? ""
: request.getAttribute("errorMessage")%></font>
<form action="CodeLogin" method="post">
<table width="350" align="center" border="1" cellspacing="1">
<tr>
<td colspan="2" align="center">
用户登录
</td>
</tr>
<tr>
<td>
用户名
</td>
<td>
<input type="text" name="username">
</td>
</tr>
<tr>
<td>
密码
</td>
<td>
<input type="password" name="password">
</td>
</tr>
<tr>
<td>
验证码
</td>
<td>
<input type="text" name="yan">
<img src="ImageServlet" alt="验证码">
</td>
</tr>
<tr>
<td colspan="2">
<input type="radio" name="save" value="0">
保存密码一天
</td>
</tr>
<tr>
<td colspan="2">
<input type="radio" name="save" value="1">
保存密码一月
</td>
</tr>
<tr>
<td colspan="2">
<input type="radio" name="save" value="2">
保存密码一年
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交">
</td>
</tr>
</table>
</form>
</body>
</html>