[Original]java Web learning note 29:cookie Demo automatic login

Source: Internet
Author: User
Tags send cookies java web

1. Requirements for automatic login

① do not need to fill in the user name and password information, you can automatically log on to the system

②login.jsp hello.jsp

  

login.jsp

1 <%@ Page Language="Java"ContentType="text/html; Charset=utf-8"2 pageencoding="UTF-8"%>3 <!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd ">4 <HTML>5 <Head>6 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">7 <title>Insert Title here</title>8 </Head>9 <Body>Ten  One  A     <formAction= "index.jsp"> -Name<inputtype= "text"name= "Name"/> -         <inputtype= "Submit"value= "Submit"/> the      -      -     </form> -  + </Body> - </HTML>

hello.jsp

1 <%@ Page Language="Java"ContentType="text/html; Charset=utf-8"2 pageencoding="UTF-8"%>3 <!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd ">4 <HTML>5 <Head>6 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">7 <title>Automatic login Test</title>8 </Head>9 <Body>Ten  One     <% A         //1If you can get to the request parameter name, print out the welcome message. Store the login information in a cookie and set the maximum age of the cookie to 30S -         Stringname=Request.getparameter ("name"); -         if(name! )= NULL &&!name.trim (). Equals ("")){ the              -             //Create and send cookies - Cookie Cookie= NewCookies ("LoginName", name); - Cookie.setmaxage ( -); + Response.addcookie (cookie); -         }Else{ +         //2read user information from a Cookie and print a welcome message if it exists A cookie[] Cookies=request.getcookies (); at             if(cookies! )= NULL &&Cookies.length> 0){ -                  for(Cookie cookie:cookies) { -                     StringCookieName=cookie.getname (); -                     if("LoginName". Equals (CookieName)) { -                         Stringvalue=Cookie.getvalue (); - name=value; in                          -                     } to                 } +             } -              the              *         } $         if(name! )= NULL &&!name.trim (). Equals ("")){Panax Notoginseng Out.print ("Welcome to login" +name); -         }Else{ the         //3If there are no request parameters and no cookies, redirect to login.jsp + Request.getrequestdispatcher ("/app-1/login.jsp"); A         } the          +  -      $      $      -     %> - </Body> the </HTML>

[Original]java Web learning note 29:cookie Demo automatic login

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.