Session案例-用户登录场景
package com.loaderman.demo; import java.io.IOException; import java.io.PrintWriter; 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 IndexServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter writer = response.getWriter(); String html = ""; /** * 接收request域对象的数据 */ /* String loginName = (String)request.getAttribute("loginName"); */ /** * 二、在用户主页,判断session不为空且存在指定的属性才视为登录成功!才能访问资源。 * 从session域中获取会话数据 */ //1.得到session对象 HttpSession session = request.getSession(false); if(session==null){ //没有登录成功,跳转到登录页面 response.sendRedirect(request.getContextPath()+"/login.html"); return; } //2.取出会话数据 String loginName = (String)session.getAttribute("loginName"); if(loginName==null){ //没有登录成功,跳转到登录页面 response.sendRedirect(request.getContextPath()+"/login.html"); return; } html = "<html><body>欢迎回来,"+loginName+",<a href='"+request.getContextPath()+"/logout'>安全退出</a></body></html>"; writer.write(html); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package com.loaderman.demo; 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; /** * 处理登录的逻辑 * @author APPle * */ public class LoginServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); //1.接收参数 String userName = request.getParameter("userName"); String userPwd = request.getParameter("userPwd"); //2.判断逻辑 if("admin".equals(userName) && "123456".equals(userPwd)){ //登录成功 /** * 分析: * context域对象:不合适,可能会覆盖数据。 * request域对象: 不合适,整个网站必须得使用转发技术来跳转页面 * session域对象:合适。 */ /* request.setAttribute("loginName", userName); //request.getRequestDispatcher("/IndexServlet").forward(request, response); response.sendRedirect(request.getContextPath()+"/IndexServlet"); */ /** * 一、登录成功后,把用户数据保存session对象中 */ //1.创建session对象 HttpSession session = request.getSession(); //2.把数据保存到session域中 session.setAttribute("loginName", userName); //3.跳转到用户主页 response.sendRedirect(request.getContextPath()+"/index"); }else{ //登录失败 //请求重定向 response.sendRedirect(request.getContextPath()+"/fail.html"); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package com.loaderman.demo; 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 { /** * 三、安全退出: * 删除掉session对象中指定的loginName属性即可! */ //1.得到session对象 HttpSession session = request.getSession(false); if(session!=null){ //2.删除属性 session.removeAttribute("loginName"); } //2.回来登录页面 response.sendRedirect(request.getContextPath()+"/login.html"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
web.xml配置:
<servlet> <!-- servlet的内部名称,自定义。尽量有意义 --> <servlet-name>IndexServlet</servlet-name> <!-- servlet的类全名: 包名+简单类名 --> <servlet-class>com.loaderman.demo.IndexServlet</servlet-class> </servlet> <servlet-mapping> <!-- servlet的内部名称,一定要和上面的内部名称保持一致!! --> <servlet-name>IndexServlet</servlet-name> <!-- servlet的映射路径(访问servlet的名称) --> <url-pattern>/index</url-pattern> </servlet-mapping> <servlet> <!-- servlet的内部名称,自定义。尽量有意义 --> <servlet-name>LoginServlet</servlet-name> <!-- servlet的类全名: 包名+简单类名 --> <servlet-class>com.loaderman.demo.LoginServlet</servlet-class> </servlet> <servlet-mapping> <!-- servlet的内部名称,一定要和上面的内部名称保持一致!! --> <servlet-name>LoginServlet</servlet-name> <!-- servlet的映射路径(访问servlet的名称) --> <url-pattern>/login</url-pattern> </servlet-mapping> <servlet> <!-- servlet的内部名称,自定义。尽量有意义 --> <servlet-name>LogoutServlet</servlet-name> <!-- servlet的类全名: 包名+简单类名 --> <servlet-class>com.loaderman.demo.LogoutServlet</servlet-class> </servlet> <servlet-mapping> <!-- servlet的内部名称,一定要和上面的内部名称保持一致!! --> <servlet-name>LogoutServlet</servlet-name> <!-- servlet的映射路径(访问servlet的名称) --> <url-pattern>/logout</url-pattern> </servlet-mapping>
fail.xml
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>信息提示页面</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> <font color='red' size='3'>亲, 你的用户名或密码输入有误!请重新输入!</font><br/> <a href="/login.html">返回登录页面</a> </body> </html>
login.xml
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>登录页面</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> <form action="/login" method="post"> 用户名:<input type="text" name="userName"/> <br/> 密码:<input type="text" name="userPwd"/> <br/> <input type="submit" value="登录"/> </form> </body> </html>
ok ,运行服务器,访问index,就可以模拟实现用户登录了~~~
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!