I have a database flights_DB
containing a table called Passengers
. Each passenger is uniquely identified by his passport number.
I would like to create a drop-down list containing all the passport numbers in the table Passengers
. How can I achieve this using JSP and Servlets?
Assuming that you've the model and DB part already finished (as per the comments on the question), just create a servlet class and implement the
doGet()
method accordingly. It's relatively simple, just retrieve the list of passengers from the DB, store it in request scope and forward to the JSP which should present it. The below example assumes that you're using EJB/JPA as service/DB layer, but whatever service/DB layer you use, you should ultimately end up getting aList<Passenger>
from it anyway.Create a JSP file
/WEB-INF/passengers.jsp
which uses JSTL<c:forEach>
to iterate over it, printing a new HTML<option>
everytime:(this example assumes the
Passenger
entity to haveid
andname
properties)That should basically be it. Just open the page by invoking the servlet's URL like so http://example.com/contextpath/passengers.
See also: