从mysql数据库检索下拉列表并将其插入到jsp中的数据库 [英] retrieve dropdown list from mysql database and insert to database in jsp
问题描述
这件事真的让我感到困扰.如何从MySQL数据库获取下拉列表,然后将其提交到JSP中的另一个表.我只知道如何使用html创建静态下拉列表,但是如何使它动态化.我正在考虑一种链接到servlet的形式,该servlet连接到数据库并从数据库表中获取字符串数组,然后将其发送到JSP以填充选项,并且在提交选项时,它将发送到然后将servlet插入数据库.有人请给我一些可以做到这一点的示例代码.最具体地说,我需要标记中使用的JSP代码和从servlet发送的代码.我已经与Google核实过,但没有明确的答案.希望我在这里得到答案
This thing is really bothering me. How can I get my dropdown list from MySQL database and then submit it to another table in JSP. I only know how to create a static dropdown with html and but how can I make it dynamic. I am thinking of a form that links to a servlet and the servlet connects to the database and fetches an array of strings from a database table and then sends it to the JSP to populate the options and when an option is submitted, it send to a servlet which then inserts to the database. someone please give me some sample code that can do this. Most specifically I need the code of the JSP used in the tag and the code for sending from the servlet. I've really checked with google but there is no clear answer. Hope I get an answer here
推荐答案
您几乎完全正确.要从数据库中获取下拉列表值,您应该首先调用执行预处理工作的servlet,然后让servlet显示带有下拉列表的JSP.
You've it almost right. To get the dropdown values from a database you should first call the servlet which does the preprocessing job and then let the servlet display the JSP with the dropdown.
由于正常的HTTP请求(单击链接,书签或在地址栏中输入URL)会按定义触发GET请求,因此您想在doGet()
方法中执行此操作.这是一个以List<Product>
风格获得下拉值的示例.
Since a normal HTTP request (clicking a link, a bookmark or entering the URL in address bar) fires per definition a GET request, you'd like to do the job in the doGet()
method. Here's an example which gets the dropdown values in flavor of a List<Product>
.
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Product> products = productService.list();
request.setAttribute("products", products); // It'll be available as ${products}.
request.getRequestDispatcher("/WEB-INF/products.jsp").forward(request, response);
}
在/WEB-INF/products.jsp
中,您可以将其显示如下:
In /WEB-INF/products.jsp
you can display it as follows:
<form action="order" method="post">
<select name="productId">
<c:forEach items="${products}" var="product">
<option value="${product.id}">${product.name}</option>
</c:forEach>
</select>
<input type="submit" value="Order" />
</form>
将此servlet映射到/products
的URL模式上,并通过 http://example.com/进行调用上下文/产品.它将从数据库加载产品,将其存储在请求范围内,再转发给JSP进行展示.
Map this servlet on an URL pattern of /products
and invoke it by http://example.com/context/products. It'll load the products from the DB, store it in the request scope, forward to the JSP to let it present it.
提交表单时,将调用servlet的doPost()
方法,该方法映射到/order
的URL模式上.提交HTML表单时,每个输入元素的name=value
对将可用作HTTP请求参数.因此,您可以获得以下产品ID:
When you submit the form, then the doPost()
method of a servlet which is mapped on an URL pattern of /order
will be invoked. When you submit a HTML form, then the name=value
pair of every input element will be available as a HTTP request parameter. So you can get the product ID as follows:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String productId = request.getParameter("productId");
// ... do your job here to insert the data.
request.getRequestDispatcher("/WEB-INF/someresult.jsp").forward(request, response);
}
另请参见:
- 我们的JSP Wiki页面
- 我们的Servlets Wiki页面
- Our JSP wiki page
- Our Servlets wiki page
See also:
这篇关于从mysql数据库检索下拉列表并将其插入到jsp中的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!