• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Retrieving data from database to Combo box in html page

 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64958
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34651
365
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    Kunal Lakhani
    Ranch Hand
    Posts: 622
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic