Solved

Servlets & JavaBeans

Posted on 2004-04-28
5
304 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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

763 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

9 Experts available now in Live!

Get 1:1 Help Now