wood burning stoves 2.0*
The moose likes Servlets and the fly likes dropdown from database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "dropdown from database" Watch "dropdown from database" New topic
Author

dropdown from database

swapna jhanjhar
Greenhorn

Joined: Oct 03, 2010
Posts: 8
Though this is a simple requirement, but i dont know why this is not working. I have a jsp page where i need to show the values from database. I'm using servlet which will be referred to on page submit. On page load of the jsp page i'm calling a javascript function which is submitting the form. In servlet connection is created with database and the values are taken in a resultset.


This structure is working fine in other requirements. Please let me know why this code is not working. I'm getting error in the "while"statement inside the dropdown.
swapna jhanjhar
Greenhorn

Joined: Oct 03, 2010
Posts: 8
I need to populate the values in dropdown.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775

swapna jhanjhar wrote:...I'm getting error in the "while"statement inside the dropdown.

What error you get,any exception details? And you should not write scriptlets inside JSPs these days. Looking at your code, you submit the JSP in the "onload" event of the body, which would submit the form recursively.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
swapna jhanjhar
Greenhorn

Joined: Oct 03, 2010
Posts: 8
Vijitha can you show me some other method of getting the same result?
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 2885
    
  13

Why do you need to submit the form at the form load event? Submitting a form is typically done by the user by pressing a submit button, after the form is filled. If it is submitted at the form-load event, the user may probably not even see the form. On the other hand, for the best practice, these scriptles should be eliminated from your JSP. Use EL, JSTL, and standard tags instead of using Java codes in JSPs. For eliminating the while loop, pass an array or list of colors instead of passing a ResultSet object. Then you can use forEach tag of JSTL.

Devaka


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
My Home Page -- Twitter Profile -- JavaRanch FAQ -- How to Ask a Question
swapna jhanjhar
Greenhorn

Joined: Oct 03, 2010
Posts: 8
I need to populate the dropdown when the page loads from DB.
Is there any other way to populate during page load?
swapna jhanjhar
Greenhorn

Joined: Oct 03, 2010
Posts: 8
swapna jhanjhar wrote:I need to populate the dropdown when the page loads from DB.
Is there any other way to populate during page load?
using servlets and with the same structure.
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 2885
    
  13

swapna jhanjhar wrote:Is there any other way to populate during page load?

You don't need to do it through JavaScripts as JavaScripts are running on the client side. The 'dynamic content' of a JSP is generated at the creation of that page at server side. Have a look at the JSP Tutorial, if you are new to JSP.

Devaka
swapna jhanjhar
Greenhorn

Joined: Oct 03, 2010
Posts: 8
Devaka Cooray wrote:
swapna jhanjhar wrote:Is there any other way to populate during page load?

You don't need to do it through JavaScripts as JavaScripts are running on the client side. The 'dynamic content' of a JSP is generated at the creation of that page at server side. Have a look at the JSP Tutorial, if you are new to JSP.

Devaka

Actually I want to create 3 dropdowns which will be populated on page load and i dont want to write the code in jsp, and want to use servlet.
can anyone please suggest the best way to do so?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

Here's the proper approach:
  • Not only should there be no database access code in the JSPs, there shouldn't be any anywhere in the presentation layer. All DB acccess shold be abstracted away by the model layer.
  • DB constructs like results sets get closed as soon as possible and are never passed outside the model layer.
  • JSPs should never be directly accessed. All but the most trival of JSPs should have a page controller that does all the work needed to gather the data needed to display the page.
  • The page controller places the data in scoped variables on the request and forwards to the JSP.
  • The JSP uses JSTL and EL to create its display. No Java code. No scriptlets. And certainly no DB constructs.

  • You might find these two articles helpful:
  • The Secret Life of JSPs
  • The Front Man


  • [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    Michael Angstadt
    Ranch Hand

    Joined: Jun 17, 2009
    Posts: 272

    Like others have said, there should be no database code in your JSP (there shouldn't be scriptlets either). It shouldn't matter to the JSP where the data comes from.


    SCJP 6 || SCWCD 5
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60050
        
      65

    Rather than an arrays (using unreadable indexes [0] and [1]), I'd use a Map.
    Michael Angstadt
    Ranch Hand

    Joined: Jun 17, 2009
    Posts: 272

    Bear Bibeault wrote:Rather than an arrays (using unreadable indexes [0] and [1]), I'd use a Map.


    Yes, I think that using a Map<String, String> instead of a List<String[]> would definitely make the code much more readable and maintainable. But doesn't a Map add unnecessary overhead in this particular situation? For example, when you add an entry to a HashMap, it has to do things like create a hash of the key, put the value in the proper bucket, etc. In the situation above, we don't need any of that. All we need is a way to store is a list of String pairs that we can iterate through. We never have to retrieve individual values by calling Map.get(), making all the hashing that HashMap does a waste.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60050
        
      65

    Michael Angstadt wrote:[For example, when you add an entry to a HashMap, it has to do things like create a hash of the key, put the value in the proper bucket, etc.
    Do you really think any of that is more than noise? Really? Do you think that the infinitesimal time that might (might) be saved is worth creating crappy unreadable code?
    Michael Angstadt
    Ranch Hand

    Joined: Jun 17, 2009
    Posts: 272

    Bear Bibeault wrote:
    Michael Angstadt wrote:[For example, when you add an entry to a HashMap, it has to do things like create a hash of the key, put the value in the proper bucket, etc.
    Do you really think any of that is more than noise? Really? Do you think that the infinitesimal time that might (might) be saved is worth creating crappy unreadable code?

    No, it's just annoying is all.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: dropdown from database
     
    Similar Threads
    Servlet and XML result HTTP 404
    problem in select
    checkbox and select
    JSP two pages
    JSP + Ajax Combo box onChange event Pass Multiple Values