aspose file tools*
The moose likes Servlets and the fly likes Populating a HTML select with a servlet - how is it done? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Populating a HTML select with a servlet - how is it done?" Watch "Populating a HTML select with a servlet - how is it done?" New topic
Author

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

David McWilliams
Ranch Hand

Joined: Mar 14, 2009
Posts: 73
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

Joined: Jan 10, 2002
Posts: 61434
    
  67

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
David McWilliams
Ranch Hand

Joined: Mar 14, 2009
Posts: 73
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

Joined: Sep 03, 2005
Posts: 46
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

    Joined: Mar 14, 2009
    Posts: 73
    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

    Joined: Apr 18, 2009
    Posts: 2234

    This code is of my previous project ,which might help you .



    Save India From Corruption - Anna Hazare.
    David McWilliams
    Ranch Hand

    Joined: Mar 14, 2009
    Posts: 73
    Ravi Kiran Va wrote:This code is of my previous project ,which might help you .


    Thanks.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61434
        
      67

    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Populating a HTML select with a servlet - how is it done?