• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Populating a HTML select with a servlet - how is it done?

 
David McWilliams
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am writing a html page that contains a dropdown 'select option' list of countries. The HTML page is displayed via a servlet.

Does anyone have any idea how this is done? I have looked for tutorials but none fit my problem.

Basically, my servlet is loaded, goes off to a database and returns a list of country names, then displays a pre-written HTML page. I want to add this list to the HTML page. I don't want the database code to reside in the HTML page.

Any hints are welcome.

Thanks,
David
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "displays a pre-written HTML page"? Is the servlet forwarding to an HTML page? Redirecting?

In either case, you have no opportunity to inject dynamic information into HTML. That's what JSP is for.

Is it not possible to convert the HTML page into a JSP for some reason?
 
David McWilliams
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a .src file that I have created. In my servlet, I created a string variable containing a Select list. I set this string into a hashtable and did a PrintWriter.println() with the hashtable inside. I then read this variable in the .src file and it displays the Select statement.

Thanks for the reply..
 
Rajkishore Pujari
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the role of the .src file?.

Here is how you can do it

In the servlet,
  • read the countries collection from database and store in collection variable.
  • add the collection variable in the request as an attribute.
  • forward the request to a JSP

  • In the JSP
  • use JSTL tag c:foreach to loop through the collection
  • and in the loop for each iteration create an OPTION HTML element with code and value.


  •  
    David McWilliams
    Ranch Hand
    Posts: 77
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for you reply.

    The .src file is doing the same work as the JSP file. Instead of needing to lop through the variable in a JSP file, I have forwarded a prepopulated Select to the .src file as a kind of widget.
     
    Ravi Kiran Va
    Ranch Hand
    Posts: 2234
    Eclipse IDE Firefox Browser Redhat
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This code is of my previous project ,which might help you .

     
    David McWilliams
    Ranch Hand
    Posts: 77
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ravi Kiran Va wrote:This code is of my previous project ,which might help you .


    Thanks.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 64959
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It amazes how people, in 2010, 8 years after they've been discredited, still think that it's appropriate to use scriptlets in JSP code. Please do not follow such examples.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic