【问题标题】:How the session is automatically created in my jsp page if I have not created session object manually?如果我没有手动创建会话对象,如何在我的 jsp 页面中自动创建会话?
【发布时间】:2012-09-05 13:52:18
【问题描述】:

我是 jsp 和 servlet 的初学者。我正在学习其中的会话处理。

我做了一个简单的程序,它有 3 个 jsp 页面,其中一个 jsp 页面有到 jsp 页面 2 的超链接。 jsp page 2 检查是否存在任何现有会话,如果是,则使用调度程序将控制分派到 jsp page 3。但如果会话对象为空,那么它会创建新会话并为其设置属性,然后使用调度程序将控制权调度到 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 中,它检查预先存在的会话。如果找到它,那么它应该直接调用 test3.jsp,但是如果 preexixting 会话不存在,那么它应该创建新会话并为其设置一个属性并调用 test3.jsp,它会在控制台上打印此属性值。

在我的情况下,当我第一次调用 test1.jsp 并单击超链接时,它会调用 test2.jsp 并发现该会话已经存在并直接调用 test3.jsp。但在实际情况下,会话既不会在 test1.jsp 上也不会在 test2.jsp 上启动,除非它进入 test2.jsp 中的 if 块。 那么我的查询是如何在我的应用程序中自动创建会话?

我敢肯定,要么我做错了编码,要么我对概念的理解有误。

我还用 servlet 替换了 test2.jsp 页面,该 servlet 执行与 test2.jsp 页面相同的任务,但仍然得到相同的结果。

我想请教专家,请告诉我到底出了什么问题。 谢谢!

【问题讨论】:

    标签: jsp session servlets


    【解决方案1】:

    除非你使用指令

    <%@ page session="false" %>
    

    在 JSP 中,只要您点击该 JSP,就会创建一个会话(除非它显然已经存在)。

    我不知道你想达到什么目的,但 99% 的情况下,使用默认值(即,只要你点击 JSP 就创建一个会话)是一个可以接受的解决方案。除非您有充分的理由不希望创建会话,否则您不必在意。

    【讨论】:

      猜你喜欢
      • 2013-02-24
      • 2011-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多