-
屌丝成长之路——应用过滤器实现主动登录
添加时间:2013-5-14 点击量:应用过滤器实现主动登录
一、主动登录实现道理。
(1)当用户勾选“记住暗码”时,先登录,然后把账号暗码记录到客户端的Cookie中,下次接见随便率性页面的时辰应用过滤器从客户端的Cookie中获取账号暗码进行登录,最后把Cookie时候刷新一遍。
(2)当用户不勾选“记住暗码”时,先登录,然后把Cookie从客户端中烧毁。
二、主动登录简单案例。
登录的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 com.service.UserService;
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取登录页面的属性
String userName=request.getParameter(userName);
String passWord=request.getParameter(passWord);
String autoLogin=request.getParameter(autoLogin);
//实例化本身定义的登录Service类
UserService loginService=new UserService();
boolean flag=loginService.login(userName, passWord);
if(flag){//登录成功履行该语句块
if(autoLogin!=null&&!autoLogin.equals(null)){//若是勾选了“记录暗码”,把账号暗码存到客户端Cookie中
Cookie c1=new Cookie(userName,userName);
Cookie c2=new Cookie(passWord,passWord);
c1.setMaxAge(6060);
c2.setMaxAge(6060);
response.addCookie(c1);
response.addCookie(c2);
}else{//如何不勾选“记录暗码”则把客户端的Cookie调换掉,并设置生活生计周期为0
Cookie c1=new Cookie(userName,null);
Cookie c2=new Cookie(passWord,null);
c1.setMaxAge(0);
c2.setMaxAge(0);
response.addCookie(c1);
response.addCookie(c2);
}
request.getSession().setAttribute(userName, userName);
request.getRequestDispatcher(index.jsp).forward(request, response);
}else{
response.sendRedirect(login.jsp);
}
}
}
过滤器:package com.filter;
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;
import com.service.UserService;
public class LoginFilter implements Filter {
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
HttpServletRequest request=(HttpServletRequest) arg0;
HttpServletResponse response=(HttpServletResponse) arg1;
String userName=(String) request.getSession().getAttribute(userName);//获取Session的值
String passWord=null;
if(userName==null){//若是未登录,获取客户端Cookie,并登录
Cookie[] cookies=request.getCookies();
if(cookies!=null){//获取客户端Cookie
for(Cookie c:cookies){
if(c.getName().equals(userName)){
userName=c.getValue();
}
if(c.getName().equals(passWord)){
passWord=c.getValue();
}
}
}
if(userName!=null&&passWord!=null){//如何账号暗码获取成功,则进行登录
UserService loginService=new UserService();
boolean flag=loginService.login(userName, passWord);
if(flag){//登录成功刷新客户端Cookie
Cookie c1=new Cookie(userName,userName);
Cookie c2=new Cookie(passWord,passWord);
c1.setMaxAge(6060);
c2.setMaxAge(6060);
response.addCookie(c1);
response.addCookie(c2);
request.getSession().setAttribute(userName, userName);
}
}
}
arg2.doFilter(request,response);
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
设备文件:<?xml version=1.0 encoding=UTF-8?>
<web-app version=2.5
xmlns=http://java.sun.com/xml/ns/javaee
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation=http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd>
<display-name></display-name>
<filter>
<filter-name>login</filter-name>
<filter-class>com.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>login</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
login.jsp页面:<%@ page language=java import=java.util. pageEncoding=UTF-8%>
<!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN>
<html>
<head>
<title>My JSP login.jsp starting page</title>
</head>
<body>
<form action=login method=post>
<table>
<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=checkbox name=autoLogin value=auto>记住暗码</td>
</tr>
<tr>
<td colspan=2 align=center>
<input type=submit name=submit value=登录 />
</td>
</tr>
</table>
</form>
</body>
</html>
index.jsp页面:
<%@ page language=java import=java.util. pageEncoding=UTF-8%>
<!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN>
<html>
<head>
<title>My JSP index.jsp starting page</title>
</head>
<body>
登录成功,迎接¥{sessionScope.userName}的到来
</body>
</html>
我所有的自负皆来自我的自卑,所有的英雄气概都来自于我的软弱。嘴里振振有词是因为心里满是怀疑,深情是因为痛恨自己无情。这世界没有一件事情是虚空而生的,站在光里,背后就会有阴影,这深夜里一片寂静,是因为你还没有听见声音。—— 马良《坦白书》