首页 > 代码库 > Session小案例------完成用户登陆
Session小案例------完成用户登陆
Session小案例------完成用户登陆
在项目开发中,用户登陆功能再寻常不过啦,当用户完成用户名和密码校验后,进入主界面,需要在主界面中显示用户的信息,此时用session来记住用户是最为合适不过了。
功能实现如下:
1,完成用户登陆功能
2,在主界面记住用户
3,完成用户注销功能
用户类:
package cn.itcast.login; public class User{ private String username; private String password; public User() { super(); // TODO Auto-generated constructor stub } public User(String username, String password) { super(); 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; } }
登陆:
package cn.itcast.login; import java.io.IOException; 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 { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //request.getAttribute("username"); //从域中取username String username = request.getParameter("username"); String password = request.getParameter("password"); //检测用户名和密码在数据库中是否存在 List<User> list = DB.getAll(); for(User user : list){ if(user.getUsername().equals(username) && user.getPassword().equals(password)){ //从数据库中找到匹配用户,让用户登陆成功 request.getSession().setAttribute("user", user); response.sendRedirect("/day07/index.jsp"); return; } } response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); response.getWriter().write("用户名或密码错误!!"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } class DB{ private static List<User> list = new ArrayList(); static{ list.add(new User("aaa","123")); list.add(new User("bbb","123")); list.add(new User("ccc","123")); list.add(new User("ddd","123")); } public static List getAll(){ return list; } }
注销:
package cn.itcast.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 LogoutServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(false); if(session!=null){ session.removeAttribute("user"); } response.sendRedirect("/day07/index.jsp"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
登陆页面:
<form action="/day07/servlet/LoginServlet" method="post"> 用户名:<input type="text" name="username"><br/> 密码:<input type="password" name="password"><br/> <input type="submit" value=http://www.mamicode.com/"登陆">>
主页面:
<body> 欢迎您: <% User user = (User)session.getAttribute("user"); if(user!=null) out.write(user.getUsername()); %> <a href=http://www.mamicode.com/"/day07/servlet/LogoutServlet">注销>
Session小案例------完成用户登陆
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。