当前位置: 首页 > 编程笔记 >

JSP登录中Session的用法实例详解

吴宏扬
2023-03-14
本文向大家介绍JSP登录中Session的用法实例详解,包括了JSP登录中Session的用法实例详解的使用技巧和注意事项,需要的朋友参考一下

本文实例讲述了JSP登录中Session的用法。分享给大家供大家参考,具体如下:

登录页面

<%@ page language="java" contentType="text/html; charset=utf-8"
  pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
 <div style="float:left;margin-top:100px;margin-left:200px;width:400px;height:300px;background:gray;">
 <form action="IndexServlet" method="post">
 <div style="float:left;width:400px;height:30px;background:gray;margin-top:50px">
  <div style="margin-left:70px;float:left;line-height:30px">账号:</div><input style="disply:block;float:left;width:200px;height:30px;border:none;" type="text" name="user"/>
 </div>
 <div style="float:left;width:400px;height:30px;background:gray;margin-top:50px">
  <div style="margin-left:70px;float:left;line-height:30px">密码:</div><input style="disply:block;float:left;width:200px;height:30px;border:none;" type="text" name="password"/>
 </div>
 <div style="float:left;margin-top:50px;width:400px;height:30px;background:gray;">
  <input style="float:left;width:60px;height:30px;margin-left:170px;border:none;" type="submit" name="ok" value="登录"/>
 </div>
 </form>
 </div>
</body>
</html>

检测账号密码以及设置session的IndexServlet

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 * Servlet implementation class IndexServlet
 */
@WebServlet("/IndexServlet")
public class IndexServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;
    
  /**
   * @see HttpServlet#HttpServlet()
   */
  public IndexServlet() {
    super();
    // TODO Auto-generated constructor stub
  }
 
 /**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 // TODO Auto-generated method stub
 response.getWriter().append("Served at: ").append(request.getContextPath());
 }
 
 /**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 // TODO Auto-generated method stub
 request.setCharacterEncoding("utf-8");
 String user = request.getParameter("user");
 String password = request.getParameter("password");
 
 String path = request.getContextPath();
 HttpSession session=request.getSession();
 
 if ("1".equals(user) && "1".equals(password)) {
  
  session.setAttribute("name", user);
  response.sendRedirect(path + "/success.jsp");
  
 }else{
  response.sendRedirect(path + "/Index.jsp");
 }
 }
 
}

成功登录页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
 String path = request.getContextPath();
%>
<%
 Object name = session.getAttribute("name");
 if(name==null){
 response.sendRedirect(path+"/Index.jsp");
 }
%>
<html>
 <head>
 <title>成功页面</title>
 </head>
 <body>
 恭喜你,骚年,<%=session.getAttribute("name") %>,成功登陆了!
 <a href="out.jsp" rel="external nofollow" >注销</a>
 </body>
</html>

注销功能的jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
 <% 
 String path = request.getContextPath();
 %>
 <%
   session.removeAttribute("name");
   response.sendRedirect(path+"/Index.jsp");
  %>
</body>
</html>

希望本文所述对大家jsp程序设计有所帮助。

 类似资料:
  • 本文向大家介绍JSP+Servlet+JavaBean实现登录网页实例详解,包括了JSP+Servlet+JavaBean实现登录网页实例详解的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了JSP+Servlet+JavaBean实现登录网页的方法。分享给大家供大家参考。具体如下: 这里涉及到四个文件: 1. 登录页面:login.html 2. 登录成功欢迎页面:login_succes

  • 本文向大家介绍jsp cookie+session实现简易自动登录,包括了jsp cookie+session实现简易自动登录的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了jsp cookie+session实现简易自动登录的具体代码,供大家参考,具体内容如下 关闭浏览器只会使存储在客户端浏览器内存中的session cookie失效,不会使服务器端的session对象失效。 如果

  • 本文向大家介绍jsp中Action使用session方法实例分析,包括了jsp中Action使用session方法实例分析的使用技巧和注意事项,需要的朋友参考一下 本文实例分析了jsp中Action使用session方法。分享给大家供大家参考。具体如下: 在Struts2里,如果需要在Action中使用session,可以通过下面两种方式得到   1.通过ActionContext class中的

  • 本文向大家介绍纯JSP实现的简单登录示例,包括了纯JSP实现的简单登录示例的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了纯JSP实现的简单登录的方法。分享给大家供大家参考,具体如下: 文件共有四个web.xml、login.jsp、logout.jsp、welcome.jsp四个文件 测试环境:Tomcat 6.0.x 假设项目名称是LoginSample,我的目录结构是这样的 ...\

  • 本文向大家介绍利用JSP session对象保持住登录状态,包括了利用JSP session对象保持住登录状态的使用技巧和注意事项,需要的朋友参考一下 http协议本身是一种无状态的协议,也就是客户端连续发送的多个请求之间没有联系,下一次请求不关心上一次请求的状态。 而实际运用中却希望服务器能记住客户端请求的状态,比如在网上购物系统中,服务器端应该能够识别并跟踪每个登录到系统中的用户挑选并购买商品

  • 本文向大家介绍jsp登录页面的简单实例 雏形,包括了jsp登录页面的简单实例 雏形的使用技巧和注意事项,需要的朋友参考一下 jsp登录页面的简单实例 雏形 欢迎界面: 尚未注册,直接登录的时候: 进入注册界面: 注册成功,跳转登录界面: 登录时,密码出错:5秒后重新登录                                   账号密码输入正确,进入主页面: 以上就是小编为大家带来的js