Bookmark Topic Watch Topic
  • New Topic

getting java bean in Servlet ,from JSP

 
Karthikean Konangi
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
Karthikean Konangi
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Report post to moderator
Thanks a lot Christophe. Now i got it.
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!