如何在jsp的会话范围内存储请求对象 [英] how to store request object in session scope in jsp

查看:64
本文介绍了如何在jsp的会话范围内存储请求对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在做随机把戏&在jsp页面上进行测试.我想使用Attributesrequest范围对象存储在session范围对象中.在尝试从请求属性(存储在会话属性中)中提取值进行存储后,得到了null.为什么会这样呢? 以下是我的jsp文件:

I was just doing random trick & testing in my jsp pages. I wanted to store request scope object in session scope object using Attributes. After storing when tried to extract the value from request attribute (stored in session attribute), I got null. Why is it so? Following are my jsp files:

index.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>
<%request.setAttribute("request1", "requestValue"); %>
<%session.setAttribute("req1", request); %>
<br>
<a href="jsp2.jsp">link</a>
</body>
</html>

jsp2.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>
<br>
<%HttpServletRequest rrrr=(HttpServletRequest)session.getAttribute("req1"); %><br>
<%=rrrr.getAttribute("request1")%><br>
</body>
</html>

浏览器输出

null

预期产量

requestValue

............................................... .....

.........................................................

在这个问题上,我需要您的指导.

I need your guidance in this problem.

推荐答案

请求对象不应存储在会话中.正如JB Nizet所写,不应在当前请求之外使用它.例如,容器可以决定稍后重用该对象,同时处理其他请求,同时重置其所有属性.

Request object shouldn't be stored in the session. As JB Nizet wrote it shouldn't be used out side of current request. Container may decide for example to reuse that object later on, while handling different request, resetting all its attributes.

您可以使用方法request.getParameter()request.getAttribute()当前请求中获取参数和属性,如果以后需要它们可以将其存储在会话中.您还可以在会话中存储任意对象.例如这样的(片段):

You can get parameters and attributes from current request using methods request.getParameter() and request.getAttribute() and if you need them for later you can store them in session. You can also store your arbitrary objects in session. For example like this (fragment):

String paramForLater = request.getParameter("p1");
// store parameter
session.setAttribute("paramForLater", paramForLater);

// store some data
Person personData = new Person();
session.setAttribute("personData", personData );

// you can retrieve these object later in different jsp like this
Person personData = (Person) session.getAttribute("personData");
String param = (String ) session.getAttribute("paramForLater");

方法request.set/getAttribute()仅在处理当前请求时使用. 例如,您可以在servlet(controller)中设置一些参数,然后在将 same 请求转发到的jsp(视图)中访问它们.像这样的模式:

Methods request.set/getAttribute() are used only, while handling current request. For example you may set some parameters in servlet(controller), then access them in the jsp (view) to which that same request was forwarded. in pattern like this:

// in servlet get your data e.g. from database
MyEntity entity = // code to get entity;
request.setAttribute("entity", entity);
request.getRequestDispatcher("view.jsp").forward(request, response);


// then in jsp you can access that paramter 
<%
MyEntity e = (MyEntity) request.getAttribute("entity");
... // do something in entity
%>

您可能应该使用EL表达式而不是scriplet,但是这个主题需要另外讨论:).

You probably should use EL expression instead of scriplets, but this theme for another discussion :).

这篇关于如何在jsp的会话范围内存储请求对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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