Session(案例_登陆)

本文介绍了一个使用Session实现的简单用户登录案例。案例通过提交表单验证用户名及密码,并将用户名保存在Session中,以实现用户状态的跟踪。文章详细展示了登陆、主页及安全退出的Servlet实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Session会话登陆案例

需求:
登陆成功后显示用户名,不成功跳转到登陆页面。

方案:
1.页面提交登陆信息
2.获取表单信息,如果登陆成功,将用户名存入session对象中。如果登陆不成功,重定向到失败页面。
3.登陆成功后,在用户主页面,判断session不为null且存在指定的属性才视为登陆成功!才能访问资源。
4.安全退出时,删除session对象中的登陆名username属性即可。

原理:
 
 
登陆页面:
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>login.html</title>
  5. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  6. <meta http-equiv="description" content="this is my page">
  7. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  8. <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
  9. </head>
  10. <body>
  11. <form action="/test/LoginServlet" method="post">
  12. 用户名:<input type="text" name="username"/><br/>
  13. 密码:<input type="password" name="pwd"/><br/>
  14. <input type="submit" value="提交"/>
  15. </form>
  16. </body>
  17. </html>
登陆Servlet:
  1. package com.cn.session;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import javax.servlet.http.HttpSession;
  9. /**
  10. * Author:Liu Zhiyong(QQ:1012421396)
  11. * Version:Version_1
  12. * Date:2016年12月4日13:23:26
  13. * Desc:Session会话登陆案例
  14. */
  15. public class LoginServlet extends HttpServlet {
  16. public void doGet(HttpServletRequest request, HttpServletResponse response)
  17. throws ServletException, IOException {
  18. request.setCharacterEncoding("utf-8");
  19. /*
  20. * 获取提交的参数
  21. */
  22. String username = request.getParameter("username");
  23. String pwd = request.getParameter("pwd");
  24. if("木丁西".equals(username) && "666666".equals(pwd)){//登陆成功
  25. /*
  26. * 将用户名存入session域对象
  27. */
  28. HttpSession session = request.getSession();
  29. session.setAttribute("username", username);
  30. response.sendRedirect(request.getContextPath() + "/IndexServlet");
  31. }else{//登陆失败
  32. response.sendRedirect(request.getContextPath() + "/fail.html");
  33. }
  34. }
  35. public void doPost(HttpServletRequest request, HttpServletResponse response)
  36. throws ServletException, IOException {
  37. this.doGet(request, response);
  38. }
  39. }
主页Servlet:
  1. package com.cn.session;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import javax.servlet.http.HttpSession;
  9. public class IndexServlet extends HttpServlet {
  10. public void doGet(HttpServletRequest request, HttpServletResponse response)
  11. throws ServletException, IOException {
  12. response.setContentType("text/html; charset=utf-8");
  13. //获取session对象
  14. HttpSession session = request.getSession(false);
  15. /**
  16. * 1.在用户主页,判断session不为空且存在指定的属性才视为登陆成功!才能访问资源。
  17. */
  18. //没有登陆成功,跳转到登陆界面
  19. if(session == null){
  20. response.sendRedirect(request.getContextPath() + "/login.html");
  21. return;
  22. }
  23. PrintWriter writer = response.getWriter();
  24. //取出会话数据
  25. String username = (String)session.getAttribute("username");
  26. /**
  27. * 2.不存在指定的属性,没有登陆成功,跳转到登陆界面
  28. */
  29. if(username == null){
  30. response.sendRedirect(request.getContextPath() + "/login.html");
  31. return;
  32. }
  33. String html = "";
  34. html += "<html><body>欢迎回来," + username + ", <a href='/test/LogoutServlet'>安全退出</a></body></html>";
  35. writer.write(html);
  36. }
  37. public void doPost(HttpServletRequest request, HttpServletResponse response)
  38. throws ServletException, IOException {
  39. this.doGet(request, response);
  40. }
  41. }
安全退出Servlet:
  1. package com.cn.session;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import javax.servlet.http.HttpSession;
  9. public class LogoutServlet extends HttpServlet {
  10. public void doGet(HttpServletRequest request, HttpServletResponse response)
  11. throws ServletException, IOException {
  12. response.setContentType("text/html; charset=utf-8");
  13. /**
  14. * 安全退出:
  15. * 删除session对象中的登陆名username的属性即可
  16. */
  17. //获取当前session
  18. HttpSession session = request.getSession(false);
  19. if(session != null){
  20. if(session.getAttribute("username") == null){
  21. response.getWriter().write("<html><body>您还尚未登陆,请先!<a href='" + request.getContextPath() + "/login.html'>登录</a></body></hmtl>");
  22. return;
  23. }
  24. // session.invalidate();//销毁session的对象
  25. session.removeAttribute("username");//移除登陆名username的属性即可!
  26. response.getWriter().write("<html><body>退出成功!<a href='" + request.getContextPath() + "/login.html'>登录</a></body></hmtl>");
  27. }else{
  28. response.getWriter().write("<html><body>您还尚未登陆,请先!<a href='" + request.getContextPath() + "/login.html'>登录</a></body></hmtl>");
  29. }
  30. }
  31. public void doPost(HttpServletRequest request, HttpServletResponse response)
  32. throws ServletException, IOException {
  33. this.doGet(request, response);
  34. }
  35. }
效果:




 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值