Servlets & JavaBeans

Hi all

basically im trying to convert a jsp page to a Servlet

the jsp uses the following code to get the Java bean

<jsp:useBean id="formHandler" class="temp.FormBean">
<jsp:setProperty name="formHandler" property="*"/>

I was wondering what the alternative is in a Servlet?
Who is Participating?
searlasConnect With a Mentor Commented:
The setXXX methods are only executed if the bean had to be instantiated.  See:

So, the equivalent code is something like:
temp.FormBean formHandler = (temp.FormBean) session.getAttribute("formHandler");
if (formHandler == null) {
  formHandler = new temp.FormBean();
  session.setAttribute("formHandler", formHandler);

That's the hard-coded equivalent.  To use reflection to do
the same is more difficult, but you could use struts/commons
to do the work for you (as they've already reimplemented
setProperty property="*")

Inside the doPost() method of your servlet
do the following

temp.FormBean formHandler = new temp.FormBean();
temp.FormBean myFormBean = (temp.FormBean)session.getAttrribute("formHandler");

formHandler.set<attribute name 1>( myAttribute1 );
formHandler.set<attribute name 2>( myAttribute2 );
formHandlerset<attribute name 3>( myAttribute3 );

<jsp:useBean id="formHandler" class="temp.FormBean">
Either creates new instance of temp.FormBean or retrieves it from the session

<jsp:setProperty name="formHandler" property="*"/>
Sets all your set attribute methods

so if you had an attribute called
private String name

then you will have a method called
setName( String newName);
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.