Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

html form that contains a list of options drawn from db

 
Edmund Castermund
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64982
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pete Nelson
Ranch Hand
Posts: 147
Debian Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64982
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 147
Debian Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic