本文實例講述了Java Web基於Session的登入實作方法。
套件 cn.com.login;導入 java.io.IOException;導入 java.io.PrintWriter;導入 java.util.ArrayList;導入 java.util.List;導入 javax.servlet.ServletException;導入 javax.servlet.http.HttpServlet ;導入javax.servlet.http.HttpServletRequest;導入javax.servlet.http.HttpServletResponse;公用類別Login擴充HttpServlet {私有靜態最終長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("使用者名稱"); Stringpassword=request.getParameter("密碼"); PrintWriter out=response.getWriter(); List<用戶> 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");返回 ; } } out.write("使用者名稱或密碼錯誤!"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); }}class Db{ public static List<User> list=new ArrayList();靜態 { list.add(new User("aaa","123")); list.add(new User("bbb","123")); list.add(new User("ccc","123")); } public static List<User> getAll() { return 列表; }}package cn.com.login;public class User { private String userName;私有字符串密碼;公共用戶(){超級(); // TODO 自動產生的建構子存根 } public User(String userName, String password) { super(); this.userName = 使用者名稱; this.password = 密碼; } public String getUserName() { return userName; } } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { 回傳密碼; } public void setPassword(String password) { this.password = 密碼; }}包 cn.com.login;導入 java.io.IOException;導入 javax.servlet.ServletException;導入 javax.servlet.http.HttpServlet;導入 javax.servlet.http.HttpServletRequest;導入 javax.servlet.http.HttpSerlet.http. import javax.servlet.http.HttpSession;/** * Servlet實作類別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");返回 ; } session.removeAttribute("用戶"); 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="這是我的頁面"> <meta http-equiv="content-type" content="text/html"> <!--<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> </內文></html >希望本文所述對大家Java web程式設計有幫助。