欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Session

程序员文章站 2022-05-15 10:57:37
...

Session

session:会话,是棵树段访问服务器服务器给客户端响应,直到客户端断开服务器为止的活动为止,被称为会话
会话的好处在于让我们更加方便的取到用户的信息

会话的创建

admin.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'admin.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    This is my JSP page. 
    <%=(String)session.getAttribute("a") %>
    <%=(String)session.getAttribute("b") %>
    <br>
  </body>
</html>

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
<form action="sessionServlet" method="post">
		姓名: <input type="text" name="name"><br> 密码: <input
			type="text" name="password"><br> <br> <input
			type="submit" value="注册"><br>
	</form>       
 <br>
  </body>
</html>

tiaozhuan.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'denglu.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  denglu.jsp
 <%=request.getParameter("name")%>
 <%=(String)session.getAttribute("a") %>
 <%=(String)session.getAttribute("b") %>
 <a href="student/admin.jsp">进入admin</a>
 
 <br>
  </body>
</html>

sessionServlet
package session;

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 sessionServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
	
	doPost(request, response);
}


public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	String name=request.getParameter("name");
	String password=request.getParameter("password");
	
	//先在数据库查询到信息并登录成功
	
	//===================session======================
	HttpSession session=request.getSession();
	session.setAttribute("a", name);
	session.setAttribute("b", password);
	
	request.getRequestDispatcher("student/tiaozhuan.jsp").forward(request, response);
}

}

Session的消失

session的消失有两种方式,一种是Tomcat自定义的时间,另一种是关闭浏览器,我们也可以用session.setMaxInactiveInterval(30);方法来改变session消失时的时间
另外,session.setMaxInactiveInterval(30);的时间单位为秒,tomcat的设置是30分钟,用此方法设置session的消失时间的时候,如果失职的时间比30分钟长,那么session的消失时间仍旧为30分钟,两个值取时间短的,Tomcat的默认消失时间在web.xml文件下,如下:

    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>

Session立即消失

session中有方法session.invalidate();方法,可以让session立即消失

写一个退出的servelt

LoginServlet

package session;

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 loginOffServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/**
		 * session立即消失
		 */
		HttpSession session = request.getSession();
		session.invalidate();// 立即退出,session立即消失

		request.getRequestDispatcher("index.jsp").forward(request, response);
	}

}

在jsp文件写<a href="loginOffServlet">退出</a>,在页面中点击退出session立即消失,并返回登录页面

页面访问权限控制

在admin页面加入代码,当用户的session失效时提示他重新登录

<%
Object obj=session.getAttribute("name");
if(obj!=null){
request.setAttribute("tishi", "请登录");
request.getRequestDispatcher("../student/index.jsp").forward(request, response);
}
%>

然后跳转回登录页面,但是退出登录不要让提示信息显示出来

<%String tishi=(String)request.getAttribute("tishi");
  if(tishi!=null){%>
  <%=tishi %>
 <% } %>

另外,request.getRequestDispatcher("…/student/index.jsp").forward(request, response);方法带参跳转,但是地址栏不变,叫做服务器内部跳转
response.sendRedirect("");方法不带参跳转,但是地址栏会变化,叫做浏览器跳转

相关标签: J2EE