aspose file tools*
The moose likes JSP and the fly likes request attribute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "request attribute" Watch "request attribute" New topic
Author

request attribute

Peter Straw
Ranch Hand

Joined: Jan 08, 2002
Posts: 79
Is it possible to store an attribute in the request in one jsp and get this attribute out at another jsp?
This is the code I have:
test.jsp
----------
<%
request.setAttribute ( "test", "test data" );
out.println ( "<a href=test2.jsp>here</a>" );
%>
test2.jsp
---------
<%
String text = (String)request.getAttribute ( "test" );
out.println ( "this is the text: " + text );
%>

This just gets a null value for the attribute, please can you tell me where I'm going wrong.
Many thanks.
Winston Smith
Ranch Hand

Joined: Jun 06, 2003
Posts: 136
Instead of using a request object, use a session object (this will persist throughout the client's session).
<%
session.setAttribute("validUser", "true");
%>
This creates an attribute called "validUser" and sets its value to "true". To access the attribute in another jsp page, just use:
<%
session.getAttribute("validUser");
%>
Good Luck,
WS


for (int i = today; i < endOfTime; i++) { code(); }
Peter Straw
Ranch Hand

Joined: Jan 08, 2002
Posts: 79
Thanks Winston, it would be good if I only used the request attribute though as the object is only needed as far as the next page - isn't this possible?
john guthrie
Ranch Hand

Joined: Aug 05, 2002
Posts: 124
unless the first jsp is forwarding control to the second in the same request, you're gonna have to use the session. typically, a jsp is writing text that is going back to the client. once done, the request is complete and the request object is removed from the servlet container. so yeah, to persist an attribute across multiple requests it has to go in the session context (and directly removed if that is a requirement).
you might be thinking of an MVC-style architecture, a la struts, where an action class will create some data object and put it in the request,where the jsp at the end of the chain can fetch it and remove it. but as i think i said above, most mvc-style frameworks only have a single jsp, with the rest of the work handled by servlets and other java classes.
Lu Battist
Ranch Hand

Joined: Feb 17, 2003
Posts: 104
John is right. If you forward the request to the 2nd jsp before trying to write anything then it should work, otherwise you need to use a bigger scope. Session is the most popular choice, but application scope could work too depending on how this information is to be use.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Could you not just do this on the first page?
out.println ( "<a href=test2.jsp?test=testdata>here</a>" );
You would need to symbol for the space between testdata. I don't remember what it is right now. %20 or something. ??
Anyway, would that not work?


GenRocket - Experts at Building Test Data
Tiago Fernandez
Ranch Hand

Joined: May 16, 2003
Posts: 167
Hi Peter,
Maybe you can do the followin':
test.jsp
<% request.setAttribute ( "test", "test data" ); %>
test2.jsp
<% String text = (String) request.getParameter ("test"); %>


Tiago Fernandez
http://www.tiago182.spyw.com/
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61654
    
  67

Maybe you can do the followin':

Ummm, no. Request parameters and request attributes aren't even remotely the same thing!
hth,
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Peter Straw
Ranch Hand

Joined: Jan 08, 2002
Posts: 79
The thing is I actually need to send an object, not just "some text" so I guess the getParameter() is not an option.
Sorry - this was a bit unclear
[ July 31, 2003: Message edited by: Peter Straw ]
Tiago Fernandez
Ranch Hand

Joined: May 16, 2003
Posts: 167
What about this?
test.jsp
<%@ page language="java" import="yourPack.yourObject" %>
<% request.setAttribute("yourObject", yourObject); %>
test2.jsp
<%@ page language="java" import="yourPack.yourObject" %>
<jsp:useBean id="yourObject" scope="request" type="yourPack.yourObject"/>
Winston Smith
Ranch Hand

Joined: Jun 06, 2003
Posts: 136
You can also use a bean which encapsulates the object your talking about. That way, you are free to get and set the object regardless of what page your are on (as long as the encapsulating bean has at least session scope).
class BigBean {
private Object yourObject;
public getYourObject() { return yourObject; }
public setYourObject(Object o) { yourObject = o; }
}
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: request attribute