How to pass form data in jsp to bean that can be accessed in the servlet
Hello all,
I'm trying to convert an AS400 screen to a webpage using jsps and servlets.Initially I have a html page where I can enter a code consisting of alphabets and numbers and when I hit submit, the control is transfered to a servlet which has the code for accessing the DB2 database to check whether that code exists in the database. If the code exists all the data corresponding to that code is transferred to a java bean(srccodedatabean) and control is transfered to a jsp called retrievedmaintfile.jsp which consists of a form containing several fields (among which several fields are select tags) which are populated by accessing the properties of the javabean.
Now when any of the fields in that retrievedmaintfile.jsp are changed , I should update the corresponding fields in the database.For that I have a button named change in that jsp which when clicked goes to a servlet (updatesrcfile.java) which has to have the code for updating the database.I wish to update the fields of the java bean(srccodedatabean.java) in the retrievedmaintfile.jsp with any of the changed values in the retrievedmaintfile.jsp(all the entries in retrievedmaintfile.jsp have corresponding fields in srccodedatabean.java bean).I 'm not knowing how to capture the value that is selected in the select tags is retrievedmaintfile.jsp and I'm not knowing how to update the java bean(srcocdedatabean.java) with the changed values and how do I access the fields of the java bean in updatesrcfile.java servlet to update them in the database.I 'm pasting a part of the code in retrievedmaintfile.jsp and the code in updatesrcfile.java servlet.
This is the code in retrievedmaintfile.jsp where catlogType is the name of a select tag and drpDate is the name of a textbox.
<jsp:useBean id="srccoddata" class="com.xxx.SrcCodeDataBean" scope="session">
<jsp:setProperty name="srccoddata" property="catlogType" param="catlogType"/>
<jsp:setProperty name="srccoddata" property="drpDate" param="drpDate"/>
</jsp:useBean>
This is the code in updatesrcfile.java servlet
if(session!=null)
{
SrcCodeDataBean s=(SrcCodeDataBean)session.getAttribute("srccoddata");
catltype=s.getCatlogType();
System.out.println("The value of catlog type is "+catltype);
drpdt=s.getDrpDate();
drpdt=request.getParameter("drpDate");
System.out.println("The drop date is "+drpdt);
}
As you see I am trying to print out the catlogType or drpDate and I am not getting anything .
I'm a novice programmer who is just starting to code.Hope I made some sense with my long description. I've been trying to resolve this problem from the past one week and couldnot suceed.Any help would be appreciated.
Please respond as soon as possible..
Thanks in advance
Neelima
[2876 byte] By [
Neel_Nava] at [2007-11-27 5:15:33]

# 1
There is shortcut method for harvesting all of the HTML Form's values (in SELECT's, TEXTFIELDs, etc... (i.e.: any <INPUT> tag element) and setting the bean's properties to match these values. It can be done with one line of code, in your JSP:
If the JavaBean you want to update with these values was created with the following statement:
<jsp:useBean id="mybeanname" class="beans.SomeBeanClass" scope="page" />
Then, to harvest all of the FORM's values and set the bean's properties accordingly, you can simply use:
<jsp:setProperty name="mybeanname" property="*" />
(an asterix for the property attribute, signifying ALL properties relevant to the form).
The requirement to use this approach is: in the HTML of your form, you must set the name attribute of each <INPUT> tag you want captured to the exact name of the bean property which corresponds to it. For example,
if one of your bean's properties is :String username;
, then in the HTML of your form, the <INPUT> tag for the textfield where the user enters their username, must have:name="username".
e.g: <INPUTTYPE="text" NAME="username"/>
The value you supply for the NAME= attribute must be an exact match, including the proper case, of the bean property name it corresponds to.
# 2
As I pasted in the code I did that but I am not getting any value when I retrieve the properties that are set in retrievedmaintfile.jsp in a servlet named updatesrcfile.java.After using the setProperty in jsp how do I retrieve those fields of the bean in a servlet?Please let me know.
Thanks
# 3
Please respond if any one has a answer or if anyone can figure out from the paste code where I am going wrong
# 4
That shortcut will work for a JSP, but to set all of the bean's properties from a form, in a servlet, it won't. In a servlet, you have to hard code it. So in your servlet, you would have to call request.getParameter() for each INPUT's name (verbatim spelling), like:
request.getParameter("mybeansnumber");
and use the return value to explicitly set the value of your bean's corresponding property
# 5
Although you could add a method to your bean which takes a single Map as an argument, then reads the Map to set all of the bean's properties based on the key being the property name. I believe there is a HttpRequest method which returns ALL of the request params as a single map, mapping the parameter name to its value.
Make sure in your Form in HTML, you specify the <form> tag with the name of your servlet or JSP,and specify the method as "POST", e.g:
<form method="POST" action="MyJSPName.jsp" >
or
<form method="POST" action="/MyServletURI" >
Also, in the case of a Servlet, you have to make sure that you have registered the servlet in Web.xml, so that the container knows about it. If you simply provide the class filename of the servlet as the action, or as a forward, it won't work: you have to add a series of tags in Web.xml for your webapp to "register" the servlet and its URI. That's only for servlets, not JSPs.
# 6
Hi agentorange07,
Thankyou for the reply. But my doubt is can't we set the properties of the bean with the values entered in the form in the jsp itself using
<jsp:useBean> tag and <jsp:setProperty> tag and retrieve those values in servlet?
Then if the above mentioned idea is not going to work, as you mentioned if I try to retrieve the values of the form(in jsp) from servlet
using request.getParameter("paramname") ,how do I do it for select tags in the form?
And to say I tried doing the request.getParameter for a textbox in the form and I did this in servlet but it was not retrieving any value...
I'm pasting the code for reference...
drpdt=request.getParameter("drpDate");
System.out.println("The drop date is "+drpdt);
where drpDate is the name of the textbox
Please let me know as soon as you can...I really appreciate any help....
Thanks,
Neelima
# 7
I' am sorry I didn't see ur last reply ...I did register the servlet in web.xml
Actually to say I want to set the bean properties with the values entered in the form (which is in a jsp) in the jsp itself and retrieve the values of that bean in a servlet (control navigates to this servlet when submit button in jsp is clicked and it is sucessfully navigating to that servlet)
and then use a sql update statement to update a DB2 database.
Please let me know what i can do for it...
Thanks