Java Web基於Session的登入實現方法_java

來源:互聯網
上載者:User

本文執行個體講述了Java Web基於Session的登入實現方法。分享給大家供大家參考,具體如下:

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("使用者名稱或者密碼錯誤!");  }  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 serialVersionUID = 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><html> <head> <title>Index.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> <form action="/Session/Login">   使用者名稱:<input type="text" name="userName"/><br/>   密碼:<input type="password" name="password"/><br/>   <input type="submit" value="登入" name="login"/> </form> </body></html>

希望本文所述對大家Java web程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.