用户的登录和注销是最常见的Web应用案例,当一个应用的客户登录了以后,其他所有的会话都得知道这个用户已经登录还很有可能得提取用户的昵称予以显示等等,所以,只有把登录成功的用户的信息放入到Session中才能够办到使所有的Servlet都能访问到用户的登录状态,下面把这个案例放上来。
第一是登录的界面,是HTML的一个表单,非常的简单
[html] view plaincopy
1. \"http://www.w3.org/TR/html4/loose.dtd\"> 2. 3.
4. 5.
8.
13. 14.[html] view plain copy
1. 3.org/TR/html4/loose.dtd\"> 2. 3.
4. 5.
8.
13.14.
第二是处理登录信息的LoginServlet
[java] view plaincopy
1. package com.bird.login; 2.
3. import java.io.IOException; 4. import java.io.PrintWriter; 5. import java.util.ArrayList; 6. import java.util.List; 7.
8. import javax.servlet.ServletException; 9. import javax.servlet.http.HttpServlet; 10. import javax.servlet.http.HttpServletRequest; 11. import javax.servlet.http.HttpServletResponse; 12.
13. public class LoginServlet extends HttpServlet { 14. 15. /**
16. * @category 使用Session处理用户登陆 17. * @author Bird 18. */
19. private static final long serialVersionUID = 1L; 20.
21. public void doGet(HttpServletRequest request, HttpServletResponse response) 22. throws ServletException, IOException { 23. response.setCharacterEncoding(\"UTF-8\");
24. response.setContentType(\"text/html;charset=UTF-8\"); 25. PrintWriter out = response.getWriter(); 26.
27. String username = request.getParameter(\"username\"); 28. String password = request.getParameter(\"password\"); 29.
30. List 32. if(u.getUsername().equals(username) && u.getPassword().equals(password)){ 33. request.getSession().setAttribute(\"user\", u);//登录成功,将用户数据放入到Session 中 34. response.sendRedirect(\"/Web/index.jsp\"); 35. return;//进行重定向,并且下面的代码不再执行 36. } 37. } 38. 39. out.write(\"您的应户名或密码错误\"); 40. } 41. 42. public void doPost(HttpServletRequest request, HttpServletResponse response) 43. throws ServletException, IOException { 44. doGet(request,response); 45. } 46. 47. } 48. 49. class Db{ 50. public static List 52. list.add(new User(\"aaa\",\"123\")); 53. list.add(new User(\"bbb\",\"123\")); 54. list.add(new User(\"ccc\",\"123\")); 55. } 56. 57. public static List [java] view plain copy 1. package com.bird.login; 2. 3. import java.io.IOException; 4. import java.io.PrintWriter; 5. import java.util.ArrayList; 6. import java.util.List; 7. 8. import javax.servlet.ServletException; 9. import javax.servlet.http.HttpServlet; 10. import javax.servlet.http.HttpServletRequest; 11. import javax.servlet.http.HttpServletResponse; 12. 13. public class LoginServlet extends HttpServlet { 14. 15. /** 16. * @category 使用Session处理用户登陆 17. * @author Bird 18. */ 19. private static final long serialVersionUID = 1L; 20. 21. public void doGet(HttpServletRequest request, HttpServletResponse respon se) 22. throws ServletException, IOException { 23. response.setCharacterEncoding(\"UTF-8\"); 24. response.setContentType(\"text/html;charset=UTF-8\"); 25. PrintWriter out = response.getWriter(); 26. 27. String username = request.getParameter(\"username\"); 28. String password = request.getParameter(\"password\"); 29. 30. List 32. if(u.getUsername().equals(username) && u.getPassword().equals(pa ssword)){ 33. request.getSession().setAttribute(\"user\", u);//登录成功,将用户 数据放入到Session中 34. response.sendRedirect(\"/Web/index.jsp\"); 35. return;//进行重定向,并且下面的代码不再执行 36. } 37. } 38. 39. out.write(\"您的应户名或密码错误\"); 40. } 41. 42. public void doPost(HttpServletRequest request, HttpServletResponse respo nse) 43. throws ServletException, IOException { 44. doGet(request,response); 45. } 46. 47. } 48. 49. class Db{ 50. public static List 52. list.add(new User(\"aaa\",\"123\")); 53. list.add(new User(\"bbb\",\"123\")); 54. list.add(new User(\"ccc\",\"123\")); 55. } 56. 57. public static List 60. } 第三,用户的信息封装在User对象中 [java] view plaincopy 1. package com.bird.login; 2. 3. /** 4. * @category 封装用户的数据的对象 5. * @author Bird 6. * 7. */ 8. public class User { 9. private String username; 10. private String password; 11. 12. 13. public User() { 14. 15. } 16. public User(String username, String password) { 17. 18. this.username = username; 19. this.password = password; 20. } 21. public String getUsername() { 22. return username; 23. } 24. public void setUsername(String username) { 25. this.username = username; 26. } 27. public String getPassword() { 28. return password; 29. } 30. public void setPassword(String password) { 31. this.password = password; 32. } 33. } [java] view plain copy 1. package com.bird.login; 2. 3. /** 4. * @category 封装用户的数据的对象 5. * @author Bird 6. * 7. */ 8. public class User { 9. private String username; 10. private String password; 11. 12. 13. public User() { 14. 15. } 16. public User(String username, String password) { 17. 18. this.username = username; 19. this.password = password; 20. } 21. public String getUsername() { 22. return username; 23. } 24. public void setUsername(String username) { 25. this.username = username; 26. } 27. public String getPassword() { 28. return password; 29. } 30. public void setPassword(String password) { 31. this.password = password; 32. } 33. } 第四是处理用户注销的Servlet [java] view plaincopy 1. package com.bird.login; 2. 3. import java.io.IOException; 4. 5. import javax.servlet.ServletException; 6. import javax.servlet.http.HttpServlet; 7. import javax.servlet.http.HttpServletRequest; 8. import javax.servlet.http.HttpServletResponse; 9. import javax.servlet.http.HttpSession; 10. 11. public class LoginOutServlet extends HttpServlet { 12. 13. /** 14. * @category 退出登录的Servlet,注销 15. * @author Bird 16. */ 17. private static final long serialVersionUID = 1L; 18. 19. public void doGet(HttpServletRequest request, HttpServletResponse response) 20. throws ServletException, IOException { 21. HttpSession session = request.getSession(false);//防止创建Session 22. if(session == null){ 23. response.sendRedirect(\"/Web/index.jsp\"); 24. return; 25. } 26. 27. session.removeAttribute(\"user\"); 28. response.sendRedirect(\"/Web/index.jsp\"); 29. } 30. 31. public void doPost(HttpServletRequest request, HttpServletResponse response) 32. throws ServletException, IOException { 33. 34. } 35. 36. } [java] view plain copy 1. package com.bird.login; 2. 3. import java.io.IOException; 4. 5. import javax.servlet.ServletException; 6. import javax.servlet.http.HttpServlet; 7. import javax.servlet.http.HttpServletRequest; 8. import javax.servlet.http.HttpServletResponse; 9. import javax.servlet.http.HttpSession; 10. 11. public class LoginOutServlet extends HttpServlet { 12. 13. /** 14. * @category 退出登录的Servlet,注销 15. * @author Bird 16. */ 17. private static final long serialVersionUID = 1L; 18. 19. public void doGet(HttpServletRequest request, HttpServletResponse respon se) 20. throws ServletException, IOException { 21. HttpSession session = request.getSession(false);//防止创建Session 22. if(session == null){ 23. response.sendRedirect(\"/Web/index.jsp\"); 24. return; 25. } 26. 27. session.removeAttribute(\"user\"); 28. response.sendRedirect(\"/Web/index.jsp\"); 29. } 30. 31. public void doPost(HttpServletRequest request, HttpServletResponse respo nse) 32. throws ServletException, IOException { 33. 34. } 35. 36. } 第五是最终的显示界面 [html] view plaincopy 1. <%@ page language=\"java\" import=\"java.util.*\" pageEncoding=\"UTF-8\"%> 2. 3. 4. 5. 6. 9. 14. 欢迎您,${user.username} 15. 退出登录 16. 17. [html] view plain copy 1. <%@ page language=\"java\" import=\"java.util.*\" pageEncoding=\"UTF-8\"%> 2. 3. 4. 5. 6. 9. 14. 欢迎您,${user.username} 15. 退出登录 16. 17. 效果如下 登录成功后 注销后
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- huatuo9.cn 版权所有 赣ICP备2023008801号-1
违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务