This week's giveaway is in the Spring forum.
We're giving away four copies of Microservices Testing (Live Project) and have Chris Love & Andres Sacco on-line!
See this thread for details.
Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

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

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67650
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Ranch Hand
    Posts: 2234
    Eclipse IDE Firefox Browser Redhat
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Ravi Kiran Va wrote:This code is of my previous project ,which might help you .



    Thanks.
     
    Bear Bibeault
    Sheriff
    Posts: 67650
    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
    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.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic