您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如果尚未手动创建会话对象,如何在jsp页面中自动创建会话?

如果尚未手动创建会话对象,如何在jsp页面中自动创建会话?

除非您使用指令

<%@ page session="false" %>
@H_403_5@

在JSP中,一旦您单击此JSP,就会创建一个会话(除非它显然已经存在)。

我不知道您要实现什么目标,但是在99%的情况下,使用认值(即,在您单击JSP后立即创建会话)是可以接受的解决方案。除非您有充分的理由不希望创建会话,否则您将不在乎。

解决方法

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

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

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

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

我想请专家们告诉我到底发生了什么错误。谢谢!

喜欢与人分享编程技术与工作经验,欢迎加入编程之家官方交流群!
<%@ 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>
<%@ 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>
<%@ 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>

在JSP中,一旦您单击此JSP,就会创建一个会话(除非它显然已经存在)。

我不知道您要实现什么目标,但是在99%的情况下,使用认值(即,在您单击JSP后立即创建会话)是可以接受的解决方案。除非您有充分的理由不希望创建会话,否则您将不在乎。

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

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

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

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

test2.jsp(jsp页面2的代码)

test3.jsp(jsp代码第3页)

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

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

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

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

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

我想请专家们告诉我到底发生了什么错误。谢谢!

在JSP中,一旦您单击此JSP,就会创建一个会话(除非它显然已经存在)。

我不知道您要实现什么目标,但是在99%的情况下,使用认值(即,在您单击JSP后立即创建会话)是可以接受的解决方案。除非您有充分的理由不希望创建会话,否则您将不在乎。

其他 2022/1/1 18:13:57 有660人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶