Automatic logon is to help users to use this page multiple times, without having to enter the username and password again to log in.
Automatic login refers to a user who saves the user's login information to a cookie in a local file.
Name,value-New Cookie (Key,value) when declared;
path-the default value, which is the path to the Serlvet where the cookie is currently saved.
If the cookie is on such a path: Http://loclhost:8080/project/abc/AServlet
The path to the cookie is: HTTP://LOCLHOST/PROJECT/ABC
The description:
The servlet in the HTTP://LOCLHOST/PROJECT/ABC directory can read the value of this cookie.
If:
Save Cookie class: Http://loclhost:8080/project/a/b/AServlet
The default path for cookies is :http://loclhost/project/a/b
First step: Develop a login page
<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" > not automatically login <br/> <input type= "Radio"
name= "Auto" Value= "1" >1 day <br/>
<input type= "Radio" name= "Auto" value= "7" >1 week <br/>
<input type= " Submit "/>
</form>
</c:when>
<c:otherwise>
You have logged in:${name}<br/>
<a href= "<c:url value= '/loginservlet '/> ' > Exit </a>
</c:otherwise>
</c: Choose>
Step Two: Save cookies successfully
public void DoPost (HttpServletRequest request, httpservletresponse response)
throws Servletexception, IOException {
//Receive user name
String name = Request.getparameter (' name ');
String auto = request.getparameter ("Auto");
Place user information in session
Request.getsession (). setattribute ("name", name);
Determine if Auto is -1
if (!auto.equals ("-1")) {
int day = integer.parseint (auto);//1|7
int seconds = 60*60*24*day ;
Declares cookie cookie
c = new Cookie ("Autologin", name);
C.setmaxage (seconds);
C.setpath (Request.getcontextpath ());
Save Cookie
Response.addcookie (c);
}
}
Step three: Require access to any page in this network should be implemented automatically login
Write a filter that filters out all the url=/*. Read all cookies in the Dofilter. There is a name cookie with the name Autologin.
Always release.
public void Dofilter (ServletRequest request, servletresponse response,
Filterchain chain) throws IOException, servletexception {
//Read cookies here
httpservletrequest req = (httpservletrequest) request;
Gets the Cookie
cookie[] cs = req.getcookies ();
if (cs!=null) {
for (cookie C:cs) {
if (C.getname (). Equals ("Autologin")) {//If there is a cookie String value for automatic login
= C.getvalue ()///user name
//Login success refers
to Req.getsession (). setattribute ("name", value);
break;
}
}} Whether or not automatically log into
chain.dofilter (request, response);
Part IV: Configure to Web.xml for all 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 >
Fifth step: Development exit
System.err.println ("User quits");
Deletes the entire 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");
Sixth step: Optimizing the Code
As the user does a manual login, it also enters the Autofiilter Dofilter method and reads all cookies once. This traversal is redundant for users.
So it should be loginservet this URL in the Dofiler.
And you cannot automatically log on to exit.
Supplementary Knowledge Points:
Verify that the user is logged in
Package cn.hongxin.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.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import javax.servlet.http.HttpSession;
public class Loginfilter implements filter{public void init (Filterconfig filterconfig) throws Servletexception {} public void Dofilter (ServletRequest request, servletresponse response, Filterchain chain) throws IOException, Se
Rvletexception {//will request strong turn into htt ...
HttpServletRequest req = (httpservletrequest) request;
Get session HttpSession SS = Req.getsession ();
Gets the user if (Ss.getattribute ("user") ==null) {System.err.println ("You are not logged in") from the session;
Req.getsession (). setattribute ("msg", "Please login First"); Redirect to login HttpseRvletresponse resp = (httpservletresponse) response; Resp.sendredirect (Req.getcontextpath () + "/index.jsp"); [W2]}
else{//Release Chain.dofilter (request, response);
}} public void Destroy () {}}
Configure to Web.xml and jsps/*:
<filter>
<filter-name>login</filter-name>
<filter-class> cn.itcast.filter.loginfilter</filter-class>
</filter>
<filter-mapping>
< filter-name>login</filter-name>
<url-pattern>/jsps/*</url-pattern>
< Url-pattern>/views/*</url-pattern>
</filter-mapping>
The above is the entire content of this article, I hope to help you learn.