自动登录,是为了帮助用户多次使用这个网页时,不用再次输入用户名和密码就可以登录。
是指用户将用户的登录信息,人,保存到本地的文件中Cookie中。
Name,value– 声明时 newCookie(key,value);
Path - 默认值,即为当前保存cookie的这个serlvet所在的路径。
如果Cookie在这样的路径:http://loclhost:8080/project/abc/AServlet
则Cookie的路径为:http://loclhost/project/abc
则说明:
所在在http://loclhost/project/abc目录下的servlet才可以读取这个cookie的值。
如果:
保存Cookie类:http://loclhost:8080/project/a/b/AServlet
则Cookie的默认path为;
对于path这个值可以手工设置:
如果设置为:http://loclhost/project/即到项目名。
则所有这个项目中的所有Serlvet|jsp都可以读取到这个 cookie.
Cookie.setPath(requst.getContextPath());
如果将path设置为 /
即:cookie.setpath(“/”);-http://localhost/
则所有在tomcat中运行的项目都可以读取这个到cookie.
如果path设置为/必须要与domain共同使用才有意义。
Age -默认值-1,在浏览器中存在。 0:删除文件中的cookie和浏览器中的cookie。
Domain -域-
www.sina.com- login
www.news.sina.com
删除时,必须要设置的与之前设置的信息完全一样:
Name
Age = 0(文件和缓存),-1(只删除文件)
Path 一样。
Domain :null
下一次用户再打开这个网页时,应该读取cookie中的信息,实现自动登录。
第一步:开发一个登录页面
<c:choose>
<c:when test="${empty sessionScope.name}">
<form name="x" method="post" action="<c:url value='/LoginServlet'/>">
Name:<input type="text" name="name"/><br/>
auto:
<input type="radio" name="auto" value="-1">不自动登录
<br/>
<input type="radio" name="auto" value="1">1天<br/>
<input type="radio" name="auto" value="7">1周<br/>
<input type="submit"/>
</form>
</c:when>
<c:otherwise>
你已经登录了:${name}<br/>
<a href="<c:url value='/LoginServlet'/>">退出</a>
</c:otherwise>
</c:choose>
第二步:成功保存cookie
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//接收用户姓名
String name = request.getParameter("name");
String auto = request.getParameter("auto");
//将用户信息放到session
request.getSession().setAttribute("name",name);
//判断auto是否是-1
if(!auto.equals("-1")){
int day = Integer.parseInt(auto);//1|7
int seconds = 60*60*24*day;
//声明cookie
Cookie c = new Cookie("autoLogin",name);
c.setMaxAge(seconds);
c.setPath(request.getContextPath());
//保存cookie
response.addCookie(c);
}
}
第三步:要求访问本网点中任何一个页面都应该实现自动登录
写一个过虑器,对所有url=/*进行过虑。在doFilter中读取所有cookie。是否存在名称为autoLogin的名称cookie。
永远都放行。
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
//在这儿读取cookie
HttpServletRequest req = (HttpServletRequest) request;
//获取所的有cookie
Cookie[] cs = req.getCookies();
if(cs!=null){
for(Cookie c:cs){
if(c.getName().equals("autoLogin")){//如果存在自动登录的cookie
String value = c.getValue();//用户名称
//登录成功是指
req.getSession().setAttribute("name", value);
break;
}
}
}
//不管是否自动登录成
chain.doFilter(request, response);
}
第四涉:配置到web.xml中对所有url=/*
<filter>
<filter-name>auto</filter-name>
<filter-class>cn.itcast.filter.AutoFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>auto</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
第五步:开发退出
System.err.println("用户退出");
//删除整个session
request.getSession().invalidate();
Cookie c = new Cookie("autoLogin", "ddd");
c.setMaxAge(0);
c.setPath(request.getContextPath());
response.addCookie(c);
// request.getSession().removeAttribute("name");
response.sendRedirect(request.getContextPath()+"/index.jsp");
第六步:优化代码
由于用户在做手工登录时,也会进入AutoFiilter的doFilter方法,且读取所有Cookie遍历一次。而这次遍历对用户来说是多余。
所以应该将LoginServet这个url在doFiler中不过过虑。
且对退出也不能自动登录。