Bookmark Topic Watch Topic
  • New Topic

how to display values from database in a dropdown box?  RSS feed

 
maggie karve
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Report post to moderator
i am displaying my dropdown box in a jsp ..i want to display values from database...does anybody know how to do it??
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Report post to moderator
Your question is general, let's try with this, just for a beginning.

You can use a select tag and a cicle to display each option
I suppose you have set in the request or in the session an attribute called listOfValues representing your list of values



 
Devaka Cooray
Marshal
Posts: 5326
575
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Report post to moderator
maggie, please TellTheDetails
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Report post to moderator
  • From the servlet page controller, call a model class to obtain the data.
  • In the model class, obtain the information from the database and close the resultset after copying the data to a List or Map.
  • Use a List if the value and display text are the same, a Map if they are different.
  • Return the collection to the servlet page controller.
  • Set the collection as a request-scoped variable and forward to the JSP.
  • In the JSP, use <c:forEach>, as shown above, to iterate over the collection and create the option elements.
  •  
    maggie karve
    Ranch Hand
    Posts: 187
    • Mark post as helpful
    • send pies
    • Report post to moderator
    hey thanks for the reply..i am not familiar with JSTL tags..
    as far of now i have made a class which will have get methods to access the values from the database...
    it is as follows:

    but i dont know what code should i write to retrieve the ArrayList value "b1".....
    How do i call getBillMonthYear in jsp code?
     
    Nicola Garofalo
    Ranch Hand
    Posts: 308
    • Mark post as helpful
    • send pies
    • Report post to moderator
    Set a request attribute in your servlet, containing the result of your method invokation getBillMonthYear()



    In your jsp iterate over that attribute as shown above.
    Get used to jstl, you'll find it very easy to learn.
     
    maggie karve
    Ranch Hand
    Posts: 187
    • Mark post as helpful
    • send pies
    • Report post to moderator
    actually it is not a servlet...
    it is just a Data class which has a get method...
    and i wrote
    in my jsp to retrieve the value but it is complaining that constructor Up is not Visible...though it is defined in Up.java class...
    what is my mistake???///

    thanks in advance...
     
    Marc Cracco
    Ranch Hand
    Posts: 80
    1
    • Mark post as helpful
    • send pies
    • Report post to moderator
    Sorry ignore below... Is your up class included in the deployment to your web server???
    I thought you were still having problems showing your items in a select tag....


    You need to iterate through your ArrayList...

    instead of:


    try:


    Use the debugger to step through your Up public ArrayList getBillMonthYear() to make sure that the return ArrayList is not empty...
     
    maggie karve
    Ranch Hand
    Posts: 187
    • Mark post as helpful
    • send pies
    • Report post to moderator
    hey thanks for the code actually i had written

    i tried each of the 3 lines individually..
    but still it is unable to recognise the Up class..
    and since it is a class of only get methods i have not declared it in web.xml because it is not a servlet..

    please tell me what else is lacking in my declaration...........


    thanks a lot.........
     
    Marc Cracco
    Ranch Hand
    Posts: 80
    1
    • Mark post as helpful
    • send pies
    • Report post to moderator
    I'm new @ this too but I think that since the JSP is interpreted server side, i.e. tomcat, it has to be part of your project deployment.

    Marc
     
    Nicola Garofalo
    Ranch Hand
    Posts: 308
    • Mark post as helpful
    • send pies
    • Report post to moderator
    The constructor is not visible because you declared it package private.

    But that's not the point.

    Please, take time to think about the guidelines posted above
     
    maggie karve
    Ranch Hand
    Posts: 187
    • Mark post as helpful
    • send pies
    • Report post to moderator
    hey i got the answer...actually i had declared a constructor in my Up.java class and when i removed the constructor I am getting the output..but i have still not understood the logic behind this/........thanks alll........
     
    Nicola Garofalo
    Ranch Hand
    Posts: 308
    • Mark post as helpful
    • send pies
    • Report post to moderator
    Well that's because when you removed the package private constructor the compiler served you with the one you had to write



    but..forgive me to repeat it. That's not the point. Please follow the guidelines posted above, trust me.
     
    maggie karve
    Ranch Hand
    Posts: 187
    • Mark post as helpful
    • send pies
    • Report post to moderator
    ok..i will follow it...thanks a lot....
     
    Megha Singhal
    Ranch Hand
    Posts: 225
    IBM DB2 Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Report post to moderator
    Nicola Garofalo wrote:Set a request attribute in your servlet, containing the result of your method invokation getBillMonthYear()



    In your jsp iterate over that attribute as shown above.
    Get used to jstl, you'll find it very easy to learn.

    can you please tell me what syntax i have to follow to get this in the jsp dropdown , after i have done set attribute in servlet file for the above example


     
    Rob Spoor
    Sheriff
    Posts: 20904
    81
    Chrome Eclipse IDE Java Windows
    • Likes 1
    • Mark post as helpful
    • send pies
    • Report post to moderator
    Megha, you've already been given an example in two of your threads.
     
    Nicola Garofalo
    Ranch Hand
    Posts: 308
    • Likes 1
    • Mark post as helpful
    • send pies
    • Report post to moderator
    Megha Singhal wrote:
    can you please tell me what syntax i have to follow to get this in the jsp dropdown , after i have done set attribute in servlet file for the above example


    Sure, read the second post of this thread.
     
    Megha Singhal
    Ranch Hand
    Posts: 225
    IBM DB2 Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Report post to moderator
    Nicola Garofalo wrote:
    Megha Singhal wrote:
    can you please tell me what syntax i have to follow to get this in the jsp dropdown , after i have done set attribute in servlet file for the above example


    Sure, read the second post of this thread.

    can you tell me syntax without using any jstl or etl because that is not enabled on my college server.
    how to call variable having database from servlet to jsp
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 65833
    134
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Likes 1
    • Mark post as helpful
    • send pies
    • Report post to moderator
    You have already asked this question elsewhere. Please do not post the same question more than once.
     
      Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!