首页 > 开发 > Java > 正文

Java Web基于Session的登录实现方法

2024-07-13 09:55:49
字体:
来源:转载
供稿:网友

这篇文章主要介绍了Java Web基于Session的登录实现方法,涉及Java针对session的操作及表单提交与验证技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了Java Web基于Session的登录实现方法。分享给大家供大家参考,具体如下:

 

 
  1. package cn.com.login; 
  2. import java.io.IOException; 
  3. import java.io.PrintWriter; 
  4. import java.util.ArrayList; 
  5. import java.util.List; 
  6. import javax.servlet.ServletException; 
  7. import javax.servlet.http.HttpServlet; 
  8. import javax.servlet.http.HttpServletRequest; 
  9. import javax.servlet.http.HttpServletResponse; 
  10. public class Login extends HttpServlet { 
  11. private static final long serialVersionUID = 1L; 
  12. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
  13. response.setCharacterEncoding("UTF-8"); 
  14. response.setContentType("text/html;charset=UTF-8"); 
  15. String userName=request.getParameter("userName"); 
  16. String password=request.getParameter("password"); 
  17. PrintWriter out=response.getWriter(); 
  18. List<User> list=Db.getAll(); 
  19. for(User user:list) 
  20. if(user.getUserName().equals(userName)&&user.getPassword().equals(password)) 
  21. request.getSession().setAttribute("user", user); 
  22. response.sendRedirect("/Session/index.jsp"); 
  23. return ; 
  24. out.write("用户名或者密码错误!"); 
  25. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
  26. doGet(request,response); 
  27. class Db 
  28. public static List<User> list=new ArrayList(); 
  29. static 
  30. list.add(new User("aaa","123")); 
  31. list.add(new User("bbb","123")); 
  32. list.add(new User("ccc","123")); 
  33. public static List<User> getAll() 
  34. return list; 
  35. package cn.com.login; 
  36. public class User { 
  37. private String userName; 
  38. private String password; 
  39. public User() { 
  40. super(); 
  41. // TODO Auto-generated constructor stub 
  42. public User(String userName, String password) { 
  43. super(); 
  44. this.userName = userName; 
  45. this.password = password; 
  46. public String getUserName() { 
  47. return userName; 
  48. public void setUserName(String userName) { 
  49. this.userName = userName; 
  50. public String getPassword() { 
  51. return password; 
  52. public void setPassword(String password) { 
  53. this.password = password; 
  54. package cn.com.login; 
  55. import java.io.IOException; 
  56. import javax.servlet.ServletException; 
  57. import javax.servlet.http.HttpServlet; 
  58. import javax.servlet.http.HttpServletRequest; 
  59. import javax.servlet.http.HttpServletResponse; 
  60. import javax.servlet.http.HttpSession; 
  61. /** 
  62. * Servlet implementation class LogOut 
  63. */ 
  64. public class LogOut extends HttpServlet { 
  65. private static final long serialVersionUID = 1L; 
  66. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
  67. HttpSession session=request.getSession(false); 
  68. if(session==null
  69. response.sendRedirect("/Session/index.jsp"); 
  70. return ; 
  71. session.removeAttribute("user"); 
  72. response.sendRedirect("/Session/index.jsp"); 
  73. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
  74. doGet(request,response); 
  75. <!DOCTYPE html> 
  76. <html> 
  77. <head> 
  78. <title>Index.html</title> 
  79. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"
  80. <meta http-equiv="description" content="this is my page"
  81. <meta http-equiv="content-type" content="text/html; charset=UTF-8"
  82. <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> 
  83. </head> 
  84. <body> 
  85. <form action="/Session/Login"
  86. 用户名:<input type="text" name="userName"/><br/> 
  87. 密码:<input type="password" name="password"/><br/> 
  88. <input type="submit" value="登录" name="login"/> 
  89. </form> 
  90. </body> 
  91. </html> 

希望本文所述对大家Java web程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表