java枚举类型 jsp select,填写HTML< select>在JSP中的下拉列表,其值从Servlet中的数据库中提取...

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 a List from it anyway.

@WebServlet("/passengers")

public class Passengers extends HttpServlet {

@EJB

private PassengerService service;

@Override

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

List passengers = service.list();

request.setAttribute("passengers", passengers);

request.getRequestDispatcher("/WEB-INF/passengers.jsp").forward(request, response);

}

}

Create a JSP file /WEB-INF/passengers.jsp which uses JSTL to iterate over it, printing a new HTML everytime:

...

(this example assumes the Passenger entity to have id and name 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:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值