aspose file tools*
The moose likes JSP and the fly likes Retrieving data from database to Combo box in html page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Retrieving data from database to Combo box in html page" Watch "Retrieving data from database to Combo box in html page" New topic
Author

Retrieving data from database to Combo box in html page

Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 618
I am having a column named 'AREA' in a table named Editors. I am now making a html page for Staffs. In that page, i am having a field named 'AREA OF WORKING'. I have to retrieve values from AREA column of EDITORS table, and have to give the same areas to staffs in a combo box. So, that the staff can choose his area. So, i can't hardcode the values in HTML page.

So, how to proceed with the above mentioned problem?


kunal
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60082
    
  65

First, be sure to use correct terminology. Read HtmlHasNoComboBox

Second, fetch the list of items and close all DB resources. Store the values in a List or Map for passing to the JSP. In the JSP use the JSTL and EL to create the select element. Under no circumstances should there be any Java code in the JSP.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 618
Thanks Bear.
As per your instructions i have coded 2 jsps. But, still, my requirement is not fulfilled. Something is wrong with my code.


What is wrong with this code?





StaffApprov.jsp


The second code, StaffApprov.jsp is just displaying a next button.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29287
    
140

Those weren't Bear's instructions. Bear said not to put Java code in a JSP.

Things I see that are wrong:
1) JDBC code in a JSP is incredibly hard to debug
2) The indentation is wrong making it hard to read the code
3) You have a connection leak - the connection needs to be closed in a finally block
4) You have no error handling.

And finally the error you are hitting. Picture the flow of what happens:
request #1
  • JSP 1 executes the Java code and stores a list in the request
  • JSP 1 renders the HTML


  • User clicks go - we now start a new request

    request #2
  • JSP 2 looks in an empty session for a request attribute


  • In other words, the structure is fundamentally broken.


    [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
    Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
    Kunal Lakhani
    Ranch Hand

    Joined: Jun 05, 2010
    Posts: 618
    ok. Then, i am going for servlets instead of JSP 1. And session.setAttribute() instead of request.setAttribute().

    Is my JSP 2 correct? I am confused regarding use <select> in JSTL
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Retrieving data from database to Combo box in html page
     
    Similar Threads
    combo boxes and iterator
    Display data from combobox into Table
    Using Combo Boxes in JSP & Servlet
    table is not redrawn
    MultiComponent JTable