• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

html form that contains a list of options drawn from db

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 147
Eclipse IDE Tomcat Server Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Tomcat Server Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic