Solved

Servlets & JavaBeans

Posted on 2004-04-28
5
319 Views
Last Modified: 2010-04-06
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="*"/>
</jsp:useBean>

I was wondering what the alternative is in a Servlet?
0
Comment
Question by:KrisM82
5 Comments
 
LVL 4

Expert Comment

by:indigostark
ID: 10937890
Inside the doPost() method of your servlet
do the following

//[A]
temp.FormBean formHandler = new temp.FormBean();
or
temp.FormBean myFormBean = (temp.FormBean)session.getAttrribute("formHandler");

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

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

//[B]
<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);
 
0
 
LVL 7

Accepted Solution

by:
searlas earned 45 total points
ID: 10944513
The setXXX methods are only executed if the bean had to be instantiated.  See:
http://java.sun.com/products/jsp/tags/11/syntaxref11.fm14.html

So, the equivalent code is something like:
temp.FormBean formHandler = (temp.FormBean) session.getAttribute("formHandler");
if (formHandler == null) {
  formHandler = new temp.FormBean();
  formHandler.setFoo(request.getParameter("foo"));
  formHandler.setBar(request.getParameter("bar"));
  ...
  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="*")

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now