cookie为用户登陆服务器后,服务器写在客户端的文件。
我们在登录126邮箱时,如果勾选上两周内自动登录,两周内不用输入用户名和密码邮箱会自动登录,这是因为第一次登录时服务器将用户名和密码写入cookie中,并返回给客户端。
下次登录时Http协议会带着用户的cookie去服务器验证,因为服务器已经存储用户的信息,所以会自动登录。
登录代码:
package com.dr.course;
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 LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username=request.getParameter("username");
String password=request.getParameter("password");
String savetime=request.getParameter("saveTime");
if(CheckLogin.login(username, password)){
if(null!=savetime){
int saveTime=Integer.parseInt(savetime);//这里接受的表单值为天来计算的
int seconds=saveTime*24*60*60;
Cookie cookie = new Cookie("user", username+"=="+password);
cookie.setMaxAge(seconds);
response.addCookie(cookie);
}
request.setAttribute("username",username);
request.getRequestDispatcher("/main126.jsp").forward(request,response);
}else{
request.getRequestDispatcher("/failure.jsp").forward(request,response);
}
}
}
验证代码:
package com.dr.course;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class IndexFilter implements Filter {
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
System.out.println("every request pass here haha");
HttpServletRequest request = (HttpServletRequest) arg0;
HttpServletResponse response = (HttpServletResponse) arg1;
Cookie[] cookies = request.getCookies();
String[] cooks = null;
String username = null;
String password = null;
if (cookies != null) {
for (Cookie coo : cookies) {
String aa = coo.getValue();
cooks = aa.split("==");
if (cooks.length == 2) {
username = cooks[0];
password = cooks[1];
}
}
}
System.out.println("cookie username | " + username);
System.out.println("cookie password | " + password);
if (CheckLogin.login(username, password)) {
System.err.println("check successfully cookie data ");
request.getSession().setAttribute("username",username);
request.getRequestDispatcher("/main126.jsp").forward(request, response);
}else{
arg2.doFilter(request,response );
}
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
package com.dr.course;
public class CheckLogin {
public static boolean login(String username, String password) {
if ("admin".equals(username) && "123456".equals(password)) {
return true;
} else {
return false;
}
}
}
web.xml中配置如下:
- <filter>
<filter-name>loginFilter</filter-name>
<filter-class>com.dr.course.IndexFilter</filter-class>
</filter>
- <filter-mapping>
<filter-name>loginFilter</filter-name>
<url-pattern>/main126.jsp</url-pattern>
</filter-mapping>
- <servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.dr.course.LoginServlet</servlet-class>
</servlet>
- <servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login.do</url-pattern>
</servlet-mapping>
将目录加入到Tomcat中,开启Tomcat,打开浏览器输入网址:http://127.0.0.1:8080/AutoLogin/login.do进行测试。