本文实例讲述了Java Web基于Session的登录实现方法。分享给大家提供给大家参考,具体如下:
包 cn.com.login;导入 java.io.IOException;导入 java.io.PrintWriter;导入 java.util.ArrayList;导入 java.util.List;导入 javax.servlet.ServletException;导入 javax.servlet.http.HttpServlet ;导入javax.servlet.http.HttpServletRequest;导入javax.servlet.http.HttpServletResponse;公共类Login扩展HttpServlet {私有静态最终长serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("UTF-8"); } response.setContentType("text/html;charset=UTF-8"); String userName=request.getParameter("用户名"); Stringpassword=request.getParameter("密码"); PrintWriter out=response.getWriter(); List<用户> list=Db.getAll(); for(User user:list) { if(user.getUserName().equals(userName)&&user.getPassword().equals(password)) { request.getSession().setAttribute("user", user); } response.sendRedirect("/Session/index.jsp");返回 ; } } out.write("用户名或者密码错误!"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); }}class Db{ public static List<User> list=new ArrayList();静态 { list.add(new User("aaa","123")); list.add(new User("bbb","123")); list.add(new User("ccc","123")); } public static List<User> getAll() { return 列表; }}package cn.com.login;public class User { private String userName;私有字符串密码;公共用户(){超级(); // TODO 自动生成的构造函数存根 } public User(String userName, String password) { super(); this.userName = 用户名; this.password = 密码; } public String getUserName() { return userName; } } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { 返回密码; } public void setPassword(String password) { this.password = 密码; }}包 cn.com.login;导入 java.io.IOException;导入 javax.servlet.ServletException;导入 javax.servlet.http.HttpServlet;导入 javax.servlet.http.HttpServletRequest;导入 javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;/** * Servlet实现类LogOut */public class LogOut extends HttpServlet { private static final long serialVersionUID = 1升; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session=request.getSession(false); } if(session==null) { response.sendRedirect("/Session/index.jsp");返回 ; } session.removeAttribute("用户"); response.sendRedirect("/Session/index.jsp"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); }}<!DOCTYPE html><html> <head> <title>Index.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description " content="这是我的页面"> <meta http-equiv="content-type" content="text/html"> <!--<link rel="stylesheet" type="text /css” href="./styles.css">--> </head> <body> <form action="/Session/Login"> 用户名:<input type="text" name="userName"/><br /> 密码:<input type="password" name="password"/><br/> <input type="submit" value="登录" name="login"/> </form> </body>< /html>希望本文所述对大家Java web程序设计有所帮助。