• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

bean problem

 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am unable to set Attribute for an object using bean the code is as follows

in jsp:
<form...action="<to the intended servlet>">
<jsp:useBean id="person" class="foo.Person" scope="request"/>
<jsp:setProperty name="person" property="name" value="Fred"/>
</form>
--------------------------------------
here the class Person has :
public class Person{
String name;
public String getName(){return name;}
public void setName(String n){name = n;}
}
--------------------------------------
the servlet that should get the Attribute from jsp for the person

servlet class:

Person = (Person)request.getAttribute("Person");
--------------------------------------
please tell me what is wrong in this code
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Varun,

I think you expect to find the "person" attribute in the request scope of the servlet to which the form is submitted, but actually this attribute is not found.
This is correct because the bean was set in the scope of a request whose life-cycle has ended. By the time you get the form page from the server, the request object has gone and the bean with it.
When you submit the form to the servlet a new request object comes into life so there is no bean in it.
In order to get what you expect, you should have to change the scope of the bean from "request" to "session".

Matteo
[ October 02, 2008: Message edited by: Matteo Palmieri ]
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

When you submit the form to the servlet a new request object comes into life..



Matteo .. is this the case always.. ?? i don't think so.. because.. if we set an attribute in the request scope of the Form and we submit it to the servlet.. i am sure we can retrive it back. isn't it..??
[ October 03, 2008: Message edited by: Sarat Koduri ]
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,
If you want to send information from html or jsp form to servlet in that case the data goes to servlet as request parameters and from request parameter you have to grab the data and instantiate the Person bean because there is no direct data binding. I have never seen any example of sending data as an attribute to servlet from JSP. I am still searching.. I think it is possible to do in some framework.
thanks
Afzal
 
Varun Nayudu
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But even after changing the scope from a request to session i am unable to get the attribute......
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Varun Nayudu:
--------------------------------------
the servlet that should get the Attribute from jsp for the person

servlet class:

Person = (Person)request.getAttribute("Person");
--------------------------------------



simple ...

String name=new foo.Person().getName();
 
Afzal Hossain
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Varun Nayudu , If i specify scope as session in useBean action it is working. I think you have forgot to use HttpSession from servlet.
 
Matteo Palmieri
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I'm actually preparing for the SCWCD too, so I may be wrong...
First, the form page has to be trimmed a little to make it usable:
<form action="<path to the servlet you are trying to reach>">
<jsp:useBean id="person" class="test.scwcd.Person" scope="request"/>
<jsp:setProperty name="person" property="name" value="Fred"/>
<input type="submit" name="Submit"/>
</form>

Bun even now, this form actually does submit "nothing", meaning that in the form there isn't any <input> field except for the submit button I added. The jsp:useBean and jsp:setProperty tags have been used within an html <form> tag but that doesn't make any different if they were placed outside of it, because the bean is created and its property set just before the form is returned to the client (jsp code always gets evaluated on server side), and if you look into the html form on the browser you will see that there isn't any info about the bean. The request scoped bean also has already gone out of scope because the http transaction with the server is over by the time the form is displayed on the browser. If you now submit the form a new transaction begins and a new request object will be created, but this time the form has no parameters in it.

I hope this better clarify my point :-)

Originally posted by Sarat Koduri:


Matteo .. is this the case always.. ?? i don't think so.. because.. if we set an attribute in the request scope of the Form and we submit it to the servlet.. i am sure we can retrive it back. isn't it..??

[ October 03, 2008: Message edited by: Sarat Koduri ]

 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Varun Nayudu
Person = (Person)request.getAttribute("Person");



Either there is a big error here or the code is not real .
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic