我是 jsp 和 servlet 的初学者。我正在学习其中的 session 处理。
我做了一个简单的程序,它有 3 个 jsp 页面,其中一个 jsp 页面有到 jsp 页面 2 的超链接。 jsp page 2 检查是否存在任何现有 session ,如果存在,则使用调度程序将控制分派(dispatch)到 jsp page 3。但如果 session 对象为空,那么它会创建新 session 并为其设置属性,然后使用调度程序将控制权调度到 jsp 页面 3。
以下是所有3个jsp页面的代码;
test1.jsp(jsp页面1的代码)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="test2.jsp"> start here</a>
</body>
</html>
test2.jsp(jsp页面2的代码)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
HttpSession ses=request.getSession(false);
if(ses==null){
System.out.println("Session is null creating new session ");
%>
Session is null creating new session.
<%
//Usersession objusersession=new Usersession();
ses=request.getSession(false);
request.setAttribute("a", "This");
ses.setAttribute("name", "sandip");
System.out.println("Session created and attributes are set now dispatching");
%>
Session created and attributes are set now dispatching
<%
response.sendRedirect("test3.jsp");
//dispatch.forward(request, response);
}else{
System.out.println("Session is old then dispatching");
%>
Session is old then dispatching
<%
response.sendRedirect("test3.jsp");
//RequestDispatcher dispatch=request.getRequestDispatcher("test3.jsp");
//dispatch.forward(request, response);
}
%>
<a href="test.jsp"> Click here</a>
</body>
</html>
test3.jsp(jsp页面3的代码)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
HttpSession ses=request.getSession();
if(!session.isNew()){
//Usersession objusersession=new Usersession();
//ses.setAttribute("name", "sandip");
//request.getAttribute("a");
//System.out.println(request.getAttribute("a"));
System.out.println(ses.getAttribute("name"));
%>
printed the session attribute value on console.
<%
}else{
response.sendRedirect("test1.jsp");
}
%>
</body>
</html>
上面代码中我们直接调用sequence的时候如下
1) 调用 test1.jsp,其中包含指向 test2.jsp 的超链接 2)当我们点击超链接时,它会调用test2.jsp。在 test2.jsp 文件 3 中,它检查预先存在的 session 。如果找到它,那么它应该直接调用 test3.jsp,但是如果 preexixting session 不存在,那么它应该创建新 session 并为其设置一个属性并调用 test3.jsp,它会在控制台上打印此属性值。
就我而言,当我第一次调用 test1.jsp 并单击超链接时,它会调用 test2.jsp 并发现该 session 已经存在并直接调用 test3.jsp。但在实际情况下, session 既不会在 test1.jsp 上也不会在 test2.jsp 上启动,除非它进入 test2.jsp 中的 if block 。 那么我的查询是如何在我的应用程序中自动创建 session ?
我敢肯定,要么我做错了编码,要么我错误地理解了这个概念。
我还用 servlet 替换了 test2.jsp 页面,该 servlet 执行与 test2.jsp 页面相同的任务,但仍然得到相同的结果。
我想请教专家,请告诉我到底出了什么问题。 谢谢!
最佳答案
除非你使用指令
<%@ page session="false" %>
在 JSP 中,只要您点击该 JSP,就会创建一个 session (除非它显然已经存在)。
我不知道你想达到什么目的,但 99% 的情况下,使用默认值(即,只要你点击 JSP 就创建一个 session )是一种可接受的解决方案。除非您有充分的理由不希望创建 session ,否则您不必在意。
关于jsp - 如果我没有手动创建 session 对象,如何在我的 jsp 页面中自动创建 session ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12389947/