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="登陆"> </form>
<body> 欢迎您: <% User user = (User)session.getAttribute("user"); if(user!=null) out.write(user.getUsername()); %> <a href="/day07/servlet/LogoutServlet">注销</a> </body>
原文地址:http://blog.csdn.net/u010590318/article/details/40756959