如果尚未手动创建会话对象,如何在jsp页面中自动创建会话? [英] How the session is automatically created in my jsp page if I have not created session object manually?

查看:63
本文介绍了如果尚未手动创建会话对象,如何在jsp页面中自动创建会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am beginner to jsp & servlet. I am learning session handling in it.

我完成了一个简单的程序,其中包含3个jsp页面,其中一个jsp页面具有指向jsp页面2的超链接. 如果是,则jsp第2页检查是否存在任何现有会话,然后使用分派器将控制分派到jsp第3页.但是,如果会话对象为null,则它将创建新会话并为其设置属性,然后使用分派器将控件分派给jsp页面3.

I have done a simple program which have 3 jsp pages in which one jsp page have hyperlink to jsp page 2. jsp page 2 checks that is there any existing session exists if yes then it dispatches control to jsp page 3 using dispatcher. But if the session object is null then it creats the new session and sets attributes to it and then dispatches control to jsp page 3 using dispatcher.

以下是所有3个jsp页面的代码;

Following is code of all 3 jsp pages;

test1.jsp(jsp第1页的代码)

test1.jsp (Code for jsp page 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页的代码)

test2.jsp (Code for jsp page 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页的代码)

test3.jsp (Code for jsp page 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>

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

In above code when we directly call sequence is as follows

1)调用具有指向test2.jsp的超链接的test1.jsp 2)当我们单击超链接时,它将调用test2.jsp.在test2.jsp file3中,它检查预先存在的会话.如果找到它,则应直接调用test3.jsp,但如果不存在预存在的会话,则应创建新会话并为其设置属性,然后调用test3.jsp,后者将在控制台上打印此属性值.

1)Invoke test1.jsp which have hyper link to test2.jsp 2)When we click hyperlink then it calls test2.jsp. In test2.jsp file3 it checks for preexisting session. If it finds it then it should directly call test3.jsp but if the preexixting session is not exists then it should create new session and set a attribute to it and call test3.jsp which prints this attributes value on console.

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

In my case when I call test1.jsp first time and click on hyper link it calls test2.jsp and founds that session is already exists and directly call test3.jsp. But in actual case the session is neither started on test1.jsp nor on test2.jsp unless it enters the if block in test2.jsp. My query is then how the session is automatically created in my application?

我确定我是在做一些错误的编码还是在错误地理解了这个概念.

I am dam sure either I am doing some wrong coding or I am understanding the concept wrongly.

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

I have also replaced the test2.jsp page with the servlet which does the same task as test2.jsp page dose but still I get the same result.

我想问专家,请告诉我到底是怎么了. 谢谢!

I want to ask experts please tell me what exactly wrong is going on. Thank You!

推荐答案

除非您使用指令

<%@ page session="false" %>

在JSP中,一旦您按下该JSP,就会创建一个会话(显然,除非它已经存在).

in a JSP, a session is created (unless it already exists, obviously) as soon as you hit this JSP.

我不知道您要达到什么目标,但是在99%的情况下,使用默认值(即,在您按下JSP时就创建了一个会话)是可以接受的解决方案.除非您有充分的理由不希望创建会话,否则您将不在乎.

I don't know what you try to achieve, but 99% of the times, using the default (i.e. a session is created as soon as you hit a JSP) is an acceptable solution. Unless you have good reasons to not want a session to be created, you shouldn't care.

这篇关于如果尚未手动创建会话对象,如何在jsp页面中自动创建会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆