I have created a servlet where I do not code the
doGet() or doPost(), only the init() method. This
allows me to get around the fact that the 2.2 servlet
api doesn't have a server startup event.
Then within the init() method I setup my arraylist of
values where each object in the arraylist is of type
LabelValueBean (which I got from the Struts Example).
I then store the arraylist as an application
attribute. This way I can have dropdowns that
populated once and re-used across my entire app.
Below is a sample servlet using this technique:
package sitemanager.shr.app;
import javax.servlet.http.*;
import javax.servlet.*;
import sitemanager.daemon.*;
import java.util.ArrayList;
import sitemanager.util.LabelValueBean;
/**
* Description of the Class
*
*@author Scott F. Walter
*@created February 17, 2001
*/
public class InitApp extends HttpServlet {
public void init(ServletConfig config) throws
ServletException {
System.out.println("Loading dropdowns...");
loadDropdowns(config);
}
public void loadDropdowns(ServletConfig config) {
ServletContext context = config.getServletContext();
ArrayList list = new java.util.ArrayList();
list.add(new LabelValueBean("What is your street
address?","" + 1));
list.add(new LabelValueBean("Where were your
born?","" + 2));
list.add(new LabelValueBean("What is your mother's
maiden name?","" + 3));
list.add(new LabelValueBean("What is your favorite
car?","" + 4));
context.setAttribute("dd_rememberpassword",list);
}
}
--- Martijn Spronk <[EMAIL PROTECTED]> wrote:
> The part of my app I'm working on is a simple form
> submit, take the
> struts-example/subscription for example.
>
> My form is accessed by an
> editRequestTime.do?action=Create call in the
> browser.
> In the form I use the <html:select> and
> <html:option> tags to display
> a selection box that is part of the form.
>
> My problem is, what is the best place to create the
> ArrayList containing the
> elements
> for the option tag?
>
> In the struts example it is hardcoded at the start
> of the JSP, with a
> disclaimer that
> it would normally be populated by the database (and
> I assume not in the
> JSP).
>
> So I figured I'll put it in my editRequestTimeAction
> code. Pass it on to the
> JSP using
> a request.setAttribute(). This works fine is the
> form is accessed through
> the editRequestTime
> call.
> However, the form submits to a new action,
> saveRequest.do. Which means that
> if there is
> an error in the form, saveRequestAction will be
> executed, after which it
> will forward to the form
> again. The form however is not aware of the
> ArrayList since it's not created
> in the saveRequestAction
> but only in the editRequestTimeAction.
>
> It doesn't seem to make sense to have to create the
> ArrayList in both the
> editRequestTimeAction
> AND in the saveRequestTimeAction. Should I put the
> creation code (or at
> least a handle to it)
> in the JSP?? That doesn't seem to help the
> seperation?
>
> Martijn.
=====
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Scott
__________________________________________________
Do You Yahoo!?
Yahoo! Auctions - Buy the things you want at great prices.
http://auctions.yahoo.com/