This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JSP and the fly likes html form that contains a list of options drawn from db Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "html form that contains a list of options drawn from db" Watch "html form that contains a list of options drawn from db" New topic
Author

html form that contains a list of options drawn from db

Edmund Castermund
Ranch Hand

Joined: May 09, 2007
Posts: 85
Hi,

I'm creating an html form, and the user will select options from a pulldown list. The content of that list will need to be drawn from the data base.

Is it best to have the form contained in a servlet? And then the action attribute of the form will be another servlet? Since most of the form is static (other than the list contents) I thought it made sense to do this as a .jsp file.
Can I grab the list contents with a scriptlet?
bp
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

View generation should done with a JSP. And only view generation. Any data gathering or processing should be in servlet controllers.

And JSPs should never contain scriptlets. Ever.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Pete Nelson
Ranch Hand

Joined: Aug 30, 2010
Posts: 147

There are a number of ways to pull this off. While ideally you should separate your view to JSP and your control & data gathering to servlet, the Java Standard Tag Libraries provide a quick-and-dirty way to pull the data & display it all within the JSP.



What I find is a more important question with these lists is "how often does this data change?". If you use the quick-and-dirty JSTL sql tags, every page request results in another query of the database, which might be overkill. I have a number of apps that must display a drop-down list of departments, which change infrequently (maybe every six months). In these cases, I will frequently use an application scope List, and then use a Filter to make sure the List is set, and up-to-date. I am attaching source code from one of my filters for your benefit (or at least I would, if I knew how to use attachments on this forum).

OCPJP
In preparing for battle I have always found that plans are useless, but planning is indispensable. -- Dwight D. Eisenhower
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

Pete Nelson wrote:Java Standard Tag Libraries provide a quick-and-dirty way to pull the data & display it all within the JSP.

The emphasis here is on "quick and dirty". Even the JSTL Specification says not to use these tags in anything but test code or where there are no alternatives.
Pete Nelson
Ranch Hand

Joined: Aug 30, 2010
Posts: 147

Here's the code I wanted to attach to my previous reply:



Then, in the jsp, I simply reference the application scoped variables DeptList and UserMap:


Edmund Castermund
Ranch Hand

Joined: May 09, 2007
Posts: 85
Hi,
that last reply was exactly what I was looking for...the logic in an external class and have the jsp call it.
thanks,
bp
 
wood burning stoves
 
subject: html form that contains a list of options drawn from db