Java Web Login Implementation method based on session _java

Source: Internet
Author: User
Tags java web

The example in this article describes the Java web based on session login implementation method. Share to everyone for your reference, specific as follows:

Package cn.com.login;
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 Login extends HttpServlet {private static final long serialversionuid = 1L;
    protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
    Response.setcharacterencoding ("UTF-8");
    Response.setcontenttype ("Text/html;charset=utf-8");
    String username=request.getparameter ("UserName");
    String password=request.getparameter ("password");
    PrintWriter Out=response.getwriter ();
    List<user> List=db.getall ();
      for (User user:list) {if (User.getusername (). Equals (UserName) &&user.getpassword (). Equals (password))
        {request.getsession (). setattribute ("user", user); RespOnse.sendredirect ("/session/index.jsp");
      return; } out.write ("User name or password error!)"
  "); 
    } protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
  Doget (Request,response);
  } class Db {public static list<user> 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<user> GetAll () {return List;
}} package Cn.com.login;
  public class User {private String userName;
  private String password;
    Public User () {super ();
    TODO auto-generated Constructor stub} public User (string userName, string password) {super ();
    This.username = UserName;
  This.password = password;
  Public String GetUserName () {return userName;
  } public void Setusername (String userName) {this.username = UserName; Public String GetPassword () {RETurn password;
  } public void SetPassword (String password) {this.password = password;
}} package Cn.com.login;
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; /** * Servlet Implementation Class LogOut/public class LogOut extends HttpServlet {private static final long seria
  Lversionuid = 1L;
    protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
    HttpSession session=request.getsession (FALSE);
      if (session==null) {response.sendredirect ("/session/index.jsp");
    return;
    } session.removeattribute ("User");
  Response.sendredirect ("/session/index.jsp"); 
    } protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Doget (Request,Response); }} <! DOCTYPE html>  

I hope this article will help you with your Java Web programming.

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.