[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Servlets & JavaBeans

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to dynamically set the form action using jQuery.

656 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