思路

用户网页登陆一次后,10天内打开网页,不需要再登陆。
方法:
把用户第一次输入的用户名密码存在用户浏览器cookies里,
下次用户打开登陆页面的时候,检查cookies,
如果有直接显示登陆成功,
如果没有显示表单让用户填写

实现

第一次登陆

第二次登陆

代码

login

package login;
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 login extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	//设置字符编码
	request.setCharacterEncoding("utf-8");
	response.setContentType("text/html;charset=utf-8");
	//定义向浏览器输出器
	PrintWriter out=response.getWriter();
	//初始化用户名和密码,以便接收浏览器cookies保存的用户名和密码
	String username="";
	String password="";
	//拿到浏览器cookies
	Cookie[] cookies=request.getCookies();
	//遍历并判断用户名与密码名字是否cookies名字与密码的相同,然后得到保存的数据
	for(int i=0;cookies!=null && i<cookies.length;i++) {
		if("username".equals(cookies[i].getName())) {
			username=cookies[i].getValue();
		}
		if("password".equals(cookies[i].getName())) {
			password=cookies[i].getValue();
		}
	}
	//判断cookie中的用户名和密码,如果相同这显示登陆成功,反之填写表单
	if("admin".equals(username) && "password".equals(password)) {
		out.write("登陆成功!");
	}else {
	
	out.write("<html>\r\n" + 
			"<head>\r\n" + 
			"<meta charset='UTF-8'>\r\n" + 
			"<title>USER LOGIN</title>\r\n" + 
			"</head>\r\n" + 
			"<body>\r\n" + 
			"<form action='"+request.getContextPath()+"/doLogin' methoed='post'>\r\n" + 
			"		用户名:<input name='username' type='text'><br>\r\n" + 
			"		密码:<input name='password' type='password'><br>\r\n" + 
			"		<input name='remember' type='checkbox'>十天免登陆<br>\r\n" + 
			"		<input type='submit' value='登陆'>\r\n" + 
			"\r\n" + 
			"</form>\r\n" + 
			"</body>\r\n" + 
			"</html>");
	}
}	
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	doGet(request, response);
}
}

doLogin

package login;
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 doLogin extends HttpServlet {
private static final long serialVersionUID = 1L;           
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	//设置字符编码
	request.setCharacterEncoding("UTF-8");
	response.setContentType("text/html;charset=utf-8");
	//定义向浏览器输出器
	PrintWriter out=response.getWriter();
	//获取Login传过来的表单数据
	String username=request.getParameter("username");
	String password=request.getParameter("password");
	String remember=request.getParameter("remember");
	//如果用户名和密码符合给定,添加cookies到浏览器,并向浏览器输出成功消息,反之输出失败消息
	Cookie cookie1=new Cookie("username",username);
	Cookie cookie2=new Cookie("password",password);
	cookie1.setPath("/");
	cookie2.setPath("/");
	if("admin".equals(username) && "password".equals(password)) {
		if(remember!=null) {
			cookie1.setMaxAge(60 * 60 * 240);
			cookie2.setMaxAge(60 * 60 * 240);
			response.addCookie(cookie1);
			response.addCookie(cookie2);
		}
		out.write("恭喜您登陆成功!");
	}else {
		out.write("对不起,登陆失败!");
		//两秒后刷新
		response.setHeader("refresh","2;url="+request.getContextPath()+"/login");			
	}						
}	
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	doGet(request, response);
}
}

XML

  <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Login</display-name>
<welcome-file-list>
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.htm</welcome-file>
  <welcome-file>index.jsp</welcome-file>
  <welcome-file>default.html</welcome-file>
  <welcome-file>default.htm</welcome-file>
  <welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
  <description></description>
  <display-name>login</display-name>
  <servlet-name>login</servlet-name>
  <servlet-class>login.login</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>login</servlet-name>
  <url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
  <description></description>
  <display-name>doLogin</display-name>
  <servlet-name>doLogin</servlet-name>
  <servlet-class>login.doLogin</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>doLogin</servlet-name>
  <url-pattern>/doLogin</url-pattern>
</servlet-mapping>
</web-app>