• 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

Retrieving data from database to Combo box in html page

 
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 67746
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    I yam what I yam and that's all that I yam - the great philosopher Popeye. Tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic