如果我没有手动创建会话对象,如何在我的jsp页面中自动创建会话?

时间:2012-09-12 14:01:04

标签: jsp session servlets

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

我做了一个简单的程序,它有3个jsp页面,其中一个jsp页面有超链接到jsp第2页。 如果是,则jsp第2页检查是否存在任何现有会话,然后使用调度程序将控制权发送到jsp第3页。但是如果会话对象为null,则它创建新会话并为其设置属性,然后使用调度程序将控制权分派给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>

在上面的代码中我们直接调用序列如下

1)调用test1.jsp,它具有到test2.jsp的超链接 2)当我们点击超链接时,它会调用test2.jsp。在test2.jsp file3中,它检查预先存在的会话。如果它找到它然后它应该直接调用test3.jsp但是如果预先混合会话不存在那么它应该创建新的会话并为它设置一个属性并调用test3.jsp,它在控制台上打印这个属性值。

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

我很确定要么我正在做错误的编码,要么我错误地理解了这个概念。

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

我想请问专家,请告诉我究竟是怎么回事。 谢谢!

1 个答案:

答案 0 :(得分:9)

除非您使用指令

<%@ page session="false" %>

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

我不知道你试图实现什么,但99%的时候,使用默认值(即一旦你点击JSP就创建了一个会话)是一个可接受的解决方案。除非你有充分的理由不想创建一个会话,否则你不应该在乎。