?
Solved

Servlets & JavaBeans

Posted on 2004-04-28
5
Medium Priority
?
393 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
2 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 180 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

It's sometimes a bit tricky to use date functions in Oracle BPEL. I'll explain quickly how you can add N days to the current date. In a BPEL process this can be useful, and you can adapt it to fit your needs. First of all, let's see how to add 1 …
I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

807 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