aspose file tools
The moose likes JSP and the fly likes getting java bean in Servlet ,from JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » JSP
Reply locked New topic
Author

getting java bean in Servlet ,from JSP

Karthikean Konangi
Greenhorn

Joined: Jan 31, 2008
Posts: 22
Hi,,
I am new to JSP. I have developed one simple EMP Info JSP Form. In this I used useBean tag ,and i set all propeties of Java Bean. On Submit, i wrote a servlet. Now how can i get the JavaBean in servlet?
(I am able to get each property using request.getParameter()). But how can i get whole JavaBean in the Servlet?

code as follows:

<FORM name="frm" method="post" action="EmpServlet" >
......

<jsp:useBean id = "empBean" scope="request" class= "beans.EmpBean"/>
<TABLE width="100%" align=left bgColor=#f7f7e7 border=0>
<TR>
<TD bgColor=#cccc99><font size=3><b>Employee No</b></font></TD>
<TD><input type=text size=10 name="empno" ></TD>
<jsp:setProperty name="empBean" property="empno" param="empno"/>
<TD bgColor=#cccc99><font size=3><b>Employee Name</b></font></TD>
<jsp:setProperty name="empBean" property="empname" param="empname"/> .....


please help me.


Regards,
KonangiK
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14672
    
  11

Good old JSP just does not work the way you are thinking. You are using a bean in a request scope, which means that a bean will be taken/created from/into the request when the form is called. The request ends, and your bean is gone too. Bye bye. When you submit the form to your servlet, there's no bean anymore. Even if you'd make a bean in session scope, it would not be automatically populated with your form's input values.

I am able to get each property using request.getParameter()

Yes, that's the way to do it.


[My Blog]
All roads lead to JavaRanch
Karthikean Konangi
Greenhorn

Joined: Jan 31, 2008
Posts: 22
Thanks Christophe ,,

Then what is the use of useBean, setProperty , getProperty tags in JSP? And now, I have number of fields in the JSP form ,and How can i get the data in the Servlet except getParameter()?

Suggest me best way of doing this ??
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14672
    
  11

Then what is the use of useBean, setProperty , getProperty tags in JSP?

If a servlet has set a bean in request scope, you can use useBean and getProperty to show the bean's values. If the bean is in a broader scope, like session or application scope, setProperty can be used to set the bean values at request time.

I have number of fields in the JSP form ,and How can i get the data in the Servlet except getParameter()?

Is there something wrong with it ? ;) Unless you use some kind of framework to do the job for you, or unless you make a utility class to do some cool things with request parameters, then you'll have to stick with getParameter, getParameterMap, getParameterNames, getParameterValues.
Karthikean Konangi
Greenhorn

Joined: Jan 31, 2008
Posts: 22
Thanks a lot Christophe. Now i got it.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: getting java bean in Servlet ,from JSP
 
Similar Threads
EL & Bean Collections
How to get the bean object in servlet from jsp?
JSP form to javabean
Bean setters not invoked when no data entered in form fields
JSP returning old data, database was changed correctly!