利用Session完成使用者的登入和登出
阿新 • • 發佈:2018-12-25
使用者的登入和登出是最常見的Web應用案例,當一個應用的客戶登入了以後,其他所有的會話都得知道這個使用者已經登入還很有可能得提取使用者的暱稱予以顯示等等,所以,只有把登入成功的使用者的資訊放入到Session中才能夠辦到使所有的Servlet都能訪問到使用者的登入狀態,下面把這個案例放上來。
第一是登入的介面,是HTML的一個表單,非常的簡單
[html] view plaincopyprint?- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">
- <title>登陸介面</title>
- </head>
- <body>
- <formaction="/Web/servlet/LoginServlet"method="post">
- 帳號:<inputtype="text"name="username"><br>
- 密碼:<inputtype="password"name="password">
- <inputtype="submit"value="登入">
- </form>
- </body>
- </html>
<!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>登陸介面</title> </head> <body> <form action="/Web/servlet/LoginServlet" method="post"> 帳號:<input type="text" name="username"><br> 密碼:<input type="password" name="password"><br> <input type="submit" value="登入"> </form> </body> </html>
第二是處理登入資訊的LoginServlet [java] view plaincopyprint?
- package com.bird.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;
- publicclass LoginServlet extends HttpServlet {
- /**
- * @category 使用Session處理使用者登陸
- * @author Bird
- */
- privatestaticfinallong serialVersionUID = 1L;
- publicvoid doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setCharacterEncoding("UTF-8");
- response.setContentType("text/html;charset=UTF-8");
- PrintWriter out = response.getWriter();
- String username = request.getParameter("username");
- String password = request.getParameter("password");
- List<User> list = Db.getAll();
- for(User u: list){
- if(u.getUsername().equals(username) && u.getPassword().equals(password)){
- request.getSession().setAttribute("user", u);//登入成功,將使用者資料放入到Session中
- response.sendRedirect("/Web/index.jsp");
- return;//進行重定向,並且下面的程式碼不再執行
- }
- }
- out.write("您的應戶名或密碼錯誤");
- }
- publicvoid doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doGet(request,response);
- }
- }
- class Db{
- publicstatic List<User> list = new ArrayList<User>();
- static{
- list.add(new User("aaa","123"));
- list.add(new User("bbb","123"));
- list.add(new User("ccc","123"));
- }
- publicstatic List<User> getAll(){
- return list;
- }
- }
package com.bird.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 LoginServlet extends HttpServlet {
/**
* @category 使用Session處理使用者登陸
* @author Bird
*/
private static final long serialVersionUID = 1L;
public 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 username = request.getParameter("username");
String password = request.getParameter("password");
List<User> list = Db.getAll();
for(User u: list){
if(u.getUsername().equals(username) && u.getPassword().equals(password)){
request.getSession().setAttribute("user", u);//登入成功,將使用者資料放入到Session中
response.sendRedirect("/Web/index.jsp");
return;//進行重定向,並且下面的程式碼不再執行
}
}
out.write("您的應戶名或密碼錯誤");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
class Db{
public static List<User> list = new ArrayList<User>();
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;
}
}
第三,使用者的資訊封裝在User物件中 [java] view plaincopyprint?
- package com.bird.login;
- /**
- * @category 封裝使用者的資料的物件
- * @author Bird
- *
- */
- publicclass User {
- private String username;
- private String password;
- public User() {
- }
- public User(String username, String password) {
- this.username = username;
- this.password = password;
- }
- public String getUsername() {
- return username;
- }
- publicvoid setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- publicvoid setPassword(String password) {
- this.password = password;
- }
- }
package com.bird.login;
/**
* @category 封裝使用者的資料的物件
* @author Bird
*
*/
public class User {
private String username;
private String password;
public User() {
}
public User(String username, String password) {
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;
}
}
第四是處理使用者登出的Servlet [java] view plaincopyprint?
- package com.bird.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;
- publicclass LoginOutServlet extends HttpServlet {
- /**
- * @category 退出登入的Servlet,登出
- * @author Bird
- */
- privatestaticfinallong serialVersionUID = 1L;
- publicvoid doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- HttpSession session = request.getSession(false);//防止建立Session
- if(session == null){
- response.sendRedirect("/Web/index.jsp");
- return;
- }
- session.removeAttribute("user");
- response.sendRedirect("/Web/index.jsp");
- }
- publicvoid doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- }
- }
package com.bird.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;
public class LoginOutServlet extends HttpServlet {
/**
* @category 退出登入的Servlet,登出
* @author Bird
*/
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession(false);//防止建立Session
if(session == null){
response.sendRedirect("/Web/index.jsp");
return;
}
session.removeAttribute("user");
response.sendRedirect("/Web/index.jsp");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
第五是最終的顯示介面 [html] view plaincopyprint?
- <%@ page language="java"import="java.util.*"pageEncoding="UTF-8"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>My JSP 'index.jsp' starting page</title>
- </head>
- <body>
- 歡迎您,${user.username} <br>
- <ahref="/Web/servlet/LoginOutServlet">退出登入</a>
- </body>
- </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
歡迎您,${user.username} <br>
<a href="/Web/servlet/LoginOutServlet">退出登入</a>
</body>
</html>
效果如下
登入成功後
登出後