09-use session to log on to the user

Source: Internet
Author: User

/*************************************** ******************************* **********/

<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> insert title here </title>
</Head>
<Body>
<Form action = "/day07/loginservlet" method = "Post">
Username: <input type = "text" name = "username"> <br>
Password: <input type = "password" name = "password"> <br>
<Input type = "Submit" value = "login">;
</Form>
</Body>
</Html>

/************************************ Loginservlet * **************************************** ******/

Package session;

Import java. Io. ioexception;
Import java. Io. printwriter;
Import java. util. arraylist;
Import java. util. List;

Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;

Public class loginservlet extends httpservlet {

Protected void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
Response. setcharacterencoding ("UTF-8 ");
Response. setcontenttype ("text/html; charset = UTF-8 ");
Printwriter out = response. getwriter ();
String name = request. getparameter ("username ");
String Pwd = request. getparameter ("password ");

List <user> List = dB. getall ();
For (User: List ){
If (user. getname (). Equals (name) & User. GetPassword (). Equals (PWD )){
// If the logon succeeds, a logon mark is saved to the session.
Request. getsession (). setattribute ("user", user); // The value is the user object.
// Jump to the homepage
Response. sendredirect (request. getcontextpath () + "/index. jsp ");
Return;
}
}


Out. Print ("incorrect user name and password ");

}


Protected void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
Doget (request, response );
}

}
Class dB {
Private Static list = new arraylist ();
Static {
List. Add (New user( "AAA", "123 "));
List. Add (new user ("BBB", "123 "));
List. Add (new user ("CCC", "123 "));
}
Public static list getall (){
Return list;
}
}
/*************************************** * ************************** Index. JSP *************************************** * *************************** 8/

<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8"
Pageencoding = "UTF-8" %>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> insert title here </title>
</Head>
<Body>
Welcome: $ {user. name} <a href = "/day07/login.html"> logon </a> <a href = "/day07/logoutservlet"> log out </a>
</Body>
</Html>

/*************************************** ******************************* ******************/

Package session;

Import java. Io. ioexception;
Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import javax. servlet. http. httpsession;

// Log out of the user
Public class logoutservlet extends httpservlet {

Protected void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
Httpsession session = request. getsession (false );
// First, judge whether the session is empty. If the session is empty, it indicates that no user is logged on. If this parameter is left blank, skip back to the homepage.
If (session = NULL ){
Response. sendredirect ("/day07/index. jsp ");
Return;
}
// If it is not empty, the session will be destroyed and jumped back to the homepage.
Session. removeattribute ("user ");
Response. sendredirect ("/day07/index. jsp ");
}


Protected void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
Doget (request, response );
}

}

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.