xCasper
xCasper

Reputation: 123

JSP vs Servlet For Generating Dropdown List

I need to generate a Drop down list using values from a property file. Currently my program is setup to have an HTML page with a hard coded drop down list with a submit button. When the button is clicked the "work" is all done in the servlet itself(and additional java classes). I wanted to do this using javascript or jquery but I cannot seem to find any information on how to do this.

The two solutions I can think of are 1)switching the html to jsp and putting the java code in the jsp file to generate the ddl or 2)use a servlet to write html code with the proper values to a page.

I am just looking for some guidance as to what would be the best way. Maybe there is a 3rd option that is even better?

Upvotes: 0

Views: 240

Answers (2)

Kevin Hooke
Kevin Hooke

Reputation: 2621

Beyond this one dropdown, assuming there is more to this webapp than just this (other forms, fields, additional pages?) then maybe a longer term solution would be to look at using a web framework that would provide support for handling this for you, as well as many other of the typical features you are going to need to build your web app. You could look at Spring MVC, JSF, or investigate something more 'modern web app'-like by using JAX-RS resources serverside and jQuery or some other Javascript library or framework (AngularJS?) on the frontend.

Upvotes: 1

yjyqin
yjyqin

Reputation: 1

of course you can do it with JSP, but I think servlet would be better. As you said the default values should read from a properties file, so you can read it once only in servlet init, and in doGet set up 2 parameters like (level=1,2,3 & name=a,b,c) to generate a proper JSON output as you want.

on browser end it's quite simple to do it with JQuery, like this

$("#drop-list-id").on('changed', function() { $.getJSON('/servlet/level=1').success(function(json) { ... }); });

Upvotes: 0

Related Questions