首页 > 代码库 > 利用session完成用户登陆
利用session完成用户登陆
1 package cn.itcast.cookie; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 import java.util.List; 6 7 import javax.servlet.ServletException; 8 import javax.servlet.http.Cookie; 9 import javax.servlet.http.HttpServlet;10 import javax.servlet.http.HttpServletRequest;11 import javax.servlet.http.HttpServletResponse;12 import javax.servlet.http.HttpSession;13 14 import cn.itcast.Db;15 import cn.itcast.Db1;16 import cn.itcast.User;17 18 public class SessionDemo extends HttpServlet {19 20 public void doGet(HttpServletRequest request, HttpServletResponse response)21 throws ServletException, IOException {22 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<User> list = Db1.getAll();31 for (User user : list) {32 if(user.getUsername().equals(username)&&user.getPassword().equals(password)){33 request.getSession().setAttribute("user", user);//登录成功,向session存入一个登录标记34 response.sendRedirect("/ServletDemo/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 45 }46 47 }
1 package cn.itcast.cookie; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse;10 import javax.servlet.http.HttpSession;11 12 //完成任务注销13 public class SessionDemo2 extends HttpServlet {14 15 16 public void doGet(HttpServletRequest request, HttpServletResponse response)17 throws ServletException, IOException {18 19 response.setCharacterEncoding("UTF-8");20 response.setContentType("text/html;charset=UTF-8");21 PrintWriter out = response.getWriter();22 23 HttpSession session = request.getSession();24 if(session==null){25 response.sendRedirect("/ServletDemo/index.jsp");26 return;27 }28 29 session.removeAttribute("user");30 response.sendRedirect("/ServletDemo/index.jsp");31 32 }33 34 35 public void doPost(HttpServletRequest request, HttpServletResponse response)36 throws ServletException, IOException {37 38 39 }40 41 }
1 package cn.itcast; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 public class Db1 { 7 8 public static List list = new ArrayList(); 9 static{10 list.add(new User("aaa","123"));11 list.add(new User("bbb","123"));12 list.add(new User("ccc","123"));13 list.add(new User("ddd","123"));14 }15 16 public static List getAll(){17 return list;18 }19 20 }
1 package cn.itcast; 2 3 public class User { 4 private String username; 5 6 public User() { 7 super(); 8 } 9 10 public User(String username, String password) {11 super();12 this.username = username;13 this.password = password;14 }15 16 private String password;17 18 public String getUsername() {19 return username;20 }21 22 public void setUsername(String username) {23 this.username = username;24 }25 26 public String getPassword() {27 return password;28 }29 30 public void setPassword(String password) {31 this.password = password;32 }33 34 }
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head>10 <base href="<%=basePath%>">11 12 <title>My JSP ‘index.jsp‘ starting page</title>13 <meta http-equiv="pragma" content="no-cache">14 <meta http-equiv="cache-control" content="no-cache">15 <meta http-equiv="expires" content="0"> 16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">17 <meta http-equiv="description" content="This is my page">18 <!--19 <link rel="stylesheet" type="text/css" href="http://www.mamicode.com/styles.css">20 -->21 </head>22 23 <body>24 欢迎您:${user.username }<a href="/ServletDemo/register.html">登录</a> <a href="/ServletDemo/servlet/SessionDemo2">退出登录</a>25 </body>26 </html>
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 2 <html> 3 <head> 4 <title>register.html</title> 5 6 </head> 7 8 <style> 9 </style>10 11 <body>12 <form action="/ServletDemo/servlet/SessionDemo" method="get">13 用户名:14 <input type="text" name="username">15 <br />16 密码:17 <input type="password" name="password">18 <br />19 <input type="submit" value="注册" />20 </form>21 </body>22 </html>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。