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
1 Answers
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.
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);
}
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>
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.
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);
}
See also:
-
Thank you @BalusC. I understand the code you've given but this line is not clear to me`List
products = productService.list(); `. Please explain it. I don't see the actual values being sent to the JSP – ken May 29 '11 at 19:18 -
`ProductService` is just your class which uses JDBC code to get data from the DB the usual JDBC way and returns it in flavor of a `List
`. Servlet doesn't send anything to JSP. The servlet just stores it as a request attribute with the name `"products"` and forwards the request to the JSP. The JSP in turn accesses the request attribute by `${products}`. – BalusC May 29 '11 at 19:21 -
Thanks again @BalusC. can you give a sketchy code of that method definition, for example the part that returns because I don't get when you say "returns it in flavor of a List
". I really don't know the use of List class. – ken May 29 '11 at 19:50 -
Uhm, that brings us back to the very basics. I'd suggest to invest time in learning [basic Java](http://download.oracle.com/javase/tutorial/getStarted/index.html) before diving into [Java web development](http://stackoverflow.com/q/1958808). If you also don't know SQL, then learn that first as well. Once you know Java and SQL, then learn [JDBC](http://download.oracle.com/javase/tutorial/jdbc/basics/) how to execute SQL language using Java code. Anyway, you can find here an article with learn-by-example samples: http://balusc.blogspot.com/2008/07/dao-tutorial-data-layer.html – BalusC May 29 '11 at 19:57
-
Yah, I've come across DTOs and DAOs but not bothered to understand what really happens. Let me check out the link and do some more reading. I'll keep you posted @BalusC. – ken May 29 '11 at 20:22
-
It is done. I read the link above DAOs and DTOs above and I created a DAO to access my database and select the values returning a list. The DTO was just a simple java bean with a getter method. I was then able to use the DTO in the JSP and retrieve the list using JSTL forEach and EL and on submission the form action invoked a servlet that inserted to the database. Only thing I didn't do was to use datasource and naming to access the database like the link shows. I used the usual connection means using drivermanager. I'll do more reading on that first. Thank you very much @BalusC. – ken May 30 '11 at 18:57