• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JSP & useBean

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Something does not feel right here !!
I declared a bean with the useBean tag and gave it a scope of request.
<jsp:useBean scope="request" id="foo" class="abc.def.foo" />
I then use the bean in my jsp file and set up the values manually like this....

<%
foo.setName = "abc";
...
...
%>
At the end the user is allowed to post the form data using the submit button and in this process another jsp file is called.
<FORM name="frmInvAdj" method="post" action="secondJSP.jsp" >
There too I have used the same bean with the same id and scope
But the object goes out of scope
why ???
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you'll need to up your scope to session for this behaviour.

I have a book that describes 'request' scope as: "Visible within the Servlet or JSP handling the request, as well as any other Servlet or page that is forwarded or included during the processing of that request".

This does not say to me: "... and also any servlet or page that is posted to through an HTTP Post." I think 'forwarded or included' means through a jsp directive or tag.

Also, take a look at the sample under http://localhost:8080/examples/jsp/sessions/carts.html

It uses session scoped beans.
 
Abraham Jacob
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Mike...
I was just wondering what exactly happens during a html form submit during a "post" or "get" ?

I can retrieve other form variables using request.getParameter or request.getAttribute ??
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The values you enter in the form are available through request.getParameter(String name) where 'name' is the name of the form's textbox (for example).

The only things you can get from request.getAttribute(String name) are values you yourself placed in the request by using request.setAttribute(String name, Object value) It should be noticed that anything that is placed in the request as an attribute is only valid for that single request. (ie - it's scope is 'request')
 
Abraham Jacob
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What if I pass a java object using request.setAttribute and use the form submit ? Can that object be reference using request.getAttribue ?
e.g
first.jsp
<FORM name="frm1" method="post" action="second.jsp">
<%
request.setAttribute("foo", fooObj);
%>
<INPUT type="submit" name="btnSubmit" value="Submit">
</FORM>

second.jsp
<%
fooObj = request.getAttribute("foo");
%>
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Scriptlets in the jsps are run b4 you get to the client. This means that the item is only in the request when it is generating the page containing your form. When you submit the newly generated form a new request object is created, and there are no attibutes in the request, however, the form's inputs will be converted to request parameters (as strings).
Servlets allow you to set Objects as attributes in the request and use them in page but once you get to the jsp that request is gone.
The only way to share an object, between jsps, that cannot be stored in the query string of the request (i.e. ) is by ...
1. EASY Using the session obj... "scope=sesson", session.setAttribute("OBJ_NAME");
2. HARDER Saving it as a binary file on the server or saving it to a db as a Blob.

[This message has been edited by Heath Lilley (edited October 17, 2001).]
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Abraham, I wasn't quite as clear as I should have been.

Heath is correct... the request in question is for that instance of the page. When you 'request' the JSP, the code inside <% %> is run, and HTML is returned, and then the request object dies. So when you click on submit... you would *not* be able to retrieve anything you had put into the request attributes, because that was for the 'last' request.
The only time attribute stuffing makes sense is if you have a 'preparing' JSP or servlet that does some kind of 'pre-processing' for you. You would place the results into the request attribute space, and then either include or forward to the 'real' displaying JSP, which would retrieve them from the request attributes... the *same* request, because you have forwarded or included the second JSP. (see the second paragraph of my first post)
 
Abraham Jacob
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes that's it !!!
Thats the part I guess I was confused about , I guess The fact that the portion under
<% %> tags are indead run only after the request...
Which means that ----
<FORM name="frm1" method="post" action="second.jsp">
<%
request.setAttribute("foo", fooObj);
%>
<INPUT type="text" name="fooName" value="abc">
<jsp:forward page="second.jsp" />
</FORM>
--------------------------------
would carry it to secound.jsp as it is in the same request.
Now my question is what about fooName... Will I be able to get the value of fooName using request.getParameter("fooName") in secound.jsp ?

My guess in *NO*, as the value for fooName is generated only as a part of the new request (as in the case of submit).

 
Heath Lilley
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right. When you "forward" you are forwarding the request and response objects that came into your current jsp. And since you aren't submitting the form, the form's parameters won't be in the request.
If you can't use session scope you could try (if it is possible) to breakdown the object you put in the reguest as an attribute so that it's members are hidden fields in the html form, they would then be included in the NEW request as parameters, this will only work with objects that have a valid string representation. Also be careful setting up loops to convert arrays and collections to hidden fields so that you don't exceed the maximum request size.

[This message has been edited by Heath Lilley (edited October 17, 2001).]
 
Abraham Jacob
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I guess thats it...
Thanx all you guys
[This message has been edited by Abraham Jacob (edited October 17, 2001).]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic