• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

request attribute

 
Peter Straw
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Peter Straw
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Tiago Fernandez
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter,
Maybe you can do the followin':
test.jsp
<% request.setAttribute ( "test", "test data" ); %>
test2.jsp
<% String text = (String) request.getParameter ("test"); %>
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65123
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you can do the followin':

Ummm, no. Request parameters and request attributes aren't even remotely the same thing!
hth,
bear
 
Peter Straw
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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; }
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic