Java WEB User Login +cookie Technology

Source: Internet
Author: User

Login.jsp ......... ............... User Login Page

Dologin.jsp ......... ............ Process user login logic, user authentication user login, save user Login status to session

User.jsp ........ ................. Read seesion status, display user information

Login.jsp code Example:

1<%@ page language= "Java"Import= "java.util.*" contenttype= "text/html; Charset=utf-8 "%>2<%3String Path =Request.getcontextpath ();4String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";5%>6 7<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >89Ten<base href= "<%=basePath%>" > One      A<title>Cookie</title> -<meta http-equiv= "Pragma" content= "No-cache" > -<meta http-equiv= "Cache-control" content= "No-cache" > the<meta http-equiv= "Expires" content= "0" > -<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" > -<meta http-equiv= "description" content= "This is my page" > -<!-- +<link rel= "stylesheet" type= "Text/css" href= "Styles.css" > -- + A    at<body> -<% -String username = ""; -String Password = ""; -cookie[] Cookies =request.getcookies (); -        if(Cookies! =NULL&& cookies.length > 0) in        { -             for(Cookie c:cookies) to            { +                if(C.getname (). Equalsignorecase ("username")) -                { theUsername =C.getvalue (); *                } $                if(C.getname (). Equalsignorecase ("Password"))Panax Notoginseng                { -Password =C.getvalue (); the                } +            } A        } the         +%> -<center> $ $ -<form action= "dologin.jsp" method= "post" name= "Loginfrom" > -<table> the<tr> -<td> User name:</td>Wuyi<td><input type= "text" name= "username" value= "<%=username%>"/></td> the</tr> -                        Wu<tr> -<td> Password:</td> About<td><input type= "password" name= "password" value= "<%=password%>"/></td> $</tr> -                        -<tr> -&LT;TD colspan= "2" align= "center" ><input type= "checkbox" checked= "Checked" name= "Isusecookie"/> Remember login information </ Td> A</tr> +                        the<tr> -&LT;TD colspan= "2" align= "center" ><input type= "submit" value= "Login"/></td> $</tr> the                                          the</table> the</form> the</center> -</body> in

Dologin.jsp Code:

1<%@ page language= "Java"Import= "java.util.*" contenttype= "text/html; Charset=utf-8 "%>2<%3String Path =Request.getcontextpath ();4String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";5%>6 7<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >89Ten<base href= "<%=basePath%>" > One      A<title>my JSP ' dologin.jsp ' starting page</title> -      -<meta http-equiv= "Pragma" content= "No-cache" > the<meta http-equiv= "Cache-control" content= "No-cache" > -<meta http-equiv= "Expires" content= "0" > -<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" > -<meta http-equiv= "description" content= "This is my page" > +<!-- -<link rel= "stylesheet" type= "Text/css" href= "Styles.css" > +- A  at -    -<body> -<% -         //first determine whether the user has chosen to remember the login status -string[] Isusecookie = request.getparametervalues ("Isusecookie"); in         if(Isusecookie! =NULL&& isusecookie.length > 0) -         { to             //Save user name and password in cookie object +String username = request.getparameter ("username"); -String Password = request.getparameter ("Password"); theCookie Usernamecookie =NewCookie ("username"), username); *Cookie Passwordcookie =NewCookie ("password"), password); $Usernamecookie.setmaxage (86400);Panax NotoginsengPasswordcookie.setmaxage (86400); - Response.addcookie (Usernamecookie); the Response.addcookie (Passwordcookie); +         } A         Else  the         { +             //saved cookie settings are invalid -cookie[] Cookies =request.getcookies (); $             if(Cookies! =NULL&& cookies.length > 0) $             { -                  for(Cookie c:cookies) -                 { the                     if(C.getname (). Equalsignorecase ("username") | | c.getname (). Equalsignorecase ("Password")) -                     {WuyiC.setmaxage (0);//Set Cookie invalidation theResponse.addcookie (c);//re-Save Cookies -                     } Wu                 } -             } About         } $%> -<a href= "users.jsp" target= "_blank" > View user Information </a> -</body> -
View Code

Users.jsp Code:

1<%@ page language= "Java"Import= "java.util.*" contenttype= "text/html; Charset=utf-8 "%>2<%3String Path =Request.getcontextpath ();4String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";5%>6 7<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >89Ten<base href= "<%=basePath%>" > One      A<title>my JSP ' users.jsp ' starting page</title> -      -<meta http-equiv= "Pragma" content= "No-cache" > the<meta http-equiv= "Cache-control" content= "No-cache" > -<meta http-equiv= "Expires" content= "0" > -<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" > -<meta http-equiv= "description" content= "This is my page" > +<!-- -<link rel= "stylesheet" type= "Text/css" href= "Styles.css" > +- A  at -    -<body> -<% -String username = ""; -String Password = ""; incookie[] Cookies =request.getcookies (); -        if(Cookies! =NULL&& cookies.length > 0) to        { +             for(Cookie c:cookies) -            { the                if(C.getname (). Equalsignorecase ("username")) *                { $Username =C.getvalue ();Panax Notoginseng                } -                if(C.getname (). Equalsignorecase ("Password")) the                { +Password =C.getvalue (); A                } the            } +        } -%> $<center> $ - -User name: <%=username%> the<br> -Password: <%=password%>Wuyi</center> the     -</body> Wu
users.jsp

Java WEB User Login +cookie technology

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.