Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Populating a drop down

 
Sudhir Pawar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to populate select list with whole 1 column of table using jsp+servlet at onload event in jsp.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64824
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you wait until the onload event? Simply create the element directly as HTML in the page.
 
Sean Rowlandson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Why would you wait until the onload event? Simply create the element directly as HTML in the page.

This is a fair point however it depends on whether the contents of the table are known upon creation or whether the table is dynamically.

Can you explain your scenario a bit clearer and include details such as how and when the table you want the values from is populated?

Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64824
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean Rowlandson wrote:
Bear Bibeault wrote:Why would you wait until the onload event? Simply create the element directly as HTML in the page.

This is a fair point however it depends on whether the contents of the table are known upon creation or whether the table is dynamically.

It is highly unlikely that a database table depends upon a page loading. If so, then it's likely the data shouldn't be in the DB at all. The scenario makes no sense.

And yes, we need to hear from the OP regarding the scenario. I think it's likely that he's just overcomplicating what should be a simple and straightforward operation.
 
Sean Rowlandson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
It is highly unlikely that a database table depends upon a page loading. If so, then it's likely the data shouldn't be in the DB at all. The scenario makes no sense..


Apologies, I was thinking of a html table however the post is unclear as to what is actually happening / what the desired outcome is.
 
Sudhir Pawar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
creation of select list is easy to create in html/jsp but from servlet how to populate it when page get loaded everytime???
 
Albareto McKenzie
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If page gets fully loaded then you don't have a problem, just generate the list in the Servlet again, if you only want to change the dropdown on certain events then you have to use AJAX
 
Sudhir Pawar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no AJAX. I want simple code for retrieving data using servlet from db and show it in select list in jsp when jsp page is opening(onload event).
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64824
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My way:
  • The servlet calls responsible the model class, which returns the data.
  • The servlet creates a Map where the key of each entry is the info that will become the value attribute of the options, and the value of the map entry is the display text, and places it in request scope.
  • The JSP uses the JSTL <c:forEach> tag to iterate through the Map and generate the option elements.

  • Easy as pie.
     
    Albareto McKenzie
    Ranch Hand
    Posts: 299
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sudhir Pawar wrote:no AJAX. I want simple code for retrieving data using servlet from db and show it in select list in jsp when jsp page is opening(onload event).


    Page is opening != onload event

    Page is opening means that is not fully open yet, onload event is thrown when the html content is ready, why would you want to load all your html and after that make a request to get the values to populate the dropdown and not using AJAX? And more important than why, how would you do that without entering an endless loop?
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 64824
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes. See my previous post on how to so this in the JSP. The onload event is of no interest for this purpose.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic