Solved

Servlets & JavaBeans

Posted on 2004-04-28
5
355 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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…

724 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