程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java Web基於Session的登錄完成辦法

Java Web基於Session的登錄完成辦法

編輯:關於JAVA

Java Web基於Session的登錄完成辦法。本站提示廣大學習愛好者:(Java Web基於Session的登錄完成辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是Java Web基於Session的登錄完成辦法正文


本文實例講述了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法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved