how to store request object in session scope in jsp

核能气质少年 提交于 2021-02-05 11:43:23

问题


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>

browser output

null

expected output

requestValue

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

I need your guidance in this problem.


回答1:


You can do this with following code:

HttpSession session = request.getSession();
session.setAttribute("req1", request);

And when you retrieve the request value you have to do this as:

<%= ((HttpServletRequest) rrrr.getAttribute("req1")).getAttribute("request1") %>

After all this: If you want to store an Object in session better way is to store it directly into session, instead of storing a request as attribute in session. See below code:

To store attribute:

HttpSession session = request.getSession();
session.setAttribute("obj1", Object); //  Object is any object that you want to store

and retrieve it as:

HttpSession session = request.getSession(false);
Object o = session.getAttribute("obj1");



回答2:


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.

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");

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
%>

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




回答3:


I got the solution, when I replaced href code with jsp:forward action, it showed perfect output.

for each server request a new HttpServletRequest object is created. Therefore, when i was navigating to jsp2.jsp page using href, it created new HttpServletRequest object for jsp2.jsp.



来源:https://stackoverflow.com/questions/25459515/how-to-store-request-object-in-session-scope-in-jsp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!