aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes bean problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "bean problem" Watch "bean problem" New topic
Author

bean problem

Varun Nayudu
Ranch Hand

Joined: Jun 26, 2006
Posts: 157
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


SCJP 1.5, SCWCD 1.5
Matteo Palmieri
Greenhorn

Joined: Sep 09, 2008
Posts: 28
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 ]

SCJP1.4: 96% SCWCD5: 91%
Sarat Koduri
Ranch Hand

Joined: Jul 08, 2008
Posts: 83
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 ]
Afzal Hossain
Greenhorn

Joined: Jan 02, 2006
Posts: 25
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


Afzal
Varun Nayudu
Ranch Hand

Joined: Jun 26, 2006
Posts: 157
But even after changing the scope from a request to session i am unable to get the attribute......
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jan 02, 2006
Posts: 25
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

Joined: Sep 09, 2008
Posts: 28
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 ]
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2712
    
    5

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


Either there is a big error here or the code is not real .


SCJP, SCWCD.
|Asking Good Questions|
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: bean problem
 
Similar Threads
jsp:usebean | NoClassDefFoundError
Problem with retreving the Attribute in jsp
useBean doubt
can't able to run <jsp:useBean> in jsp page
Simple EL code is not working