Cookies Remember passwords

Source: Internet
Author: User
The Internet browser will have a history, log on to a website will remember the password, these historical records and passwords are actually left in the computer cookie, if you change a computer, there will be no previous records.   This is different from the session, the record of the session is left on the server side, changed the computer, log in or can see the previous record. Client Record: Cookie

Server records: Session

Small example: remember password at login

Basic structure as pictured

Loginaction.java

Package com.action;

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;

@SuppressWarnings ("Serial")
public class Loginaction extends HttpServlet {

public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
}

public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {

String Username=request.getparameter ("username");
String password=request.getparameter ("Userpass");
String flag=request.getparameter ("flag");

String path= "/error.jsp";

if ("admin". Equals (username) && "123". Equals (password)) {
if (Flag!=null&&flag.equals ("select")) {
Cookies c1=new Cookies ("uname", username);
Cookies c2=new Cookies ("UPass", password);

Set effective time, in milliseconds

   c1.setmaxage (60*60*60*24);
   c1.setmaxage (60*60*60*24);
   
   response.addcookie (C1);
   response.addcookie (C2 );
   }else{
    cookie c1=new Cookies ("uname", username);  
     cookie c2=new Cookies ("UPass", password);  
    
     c1.setmaxage (0);
    c1.setmaxage (0);
    
    response.addcookie (C1);
     Response.addcookie (C2);  
   }
   path= "/index.jsp";
  }  &NBSP
  request.getrequestdispatcher (path). Forward (request,response);    }

}
*************************************************************************************************************** ********************************************

Index.xml

<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<title>cookie</title>

<body>

<form action= "login.do" method= "POST" >
User: <input type= "text" name= "username" value= "${cookie.aname.value}" ><br/>
Password: <input type= "password" name= "Userpass" value= "${cookie.apsss.value}" ><br/>
Remember password: <input type= "checkbox" name= "Flag" value= "select" ${cookie.aname.value}!=null? ' Checked ': '/>
<%--? : An expression
If you are not empty, you are chosen, you are not chosen, you do not remember your password.

--%>

<br/>
<input type= "Submit" value= "Login" >
</form>
</body>

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.