How to Display data from database with servlet/jsp
Solution 1
In your JSP you misspelt list you wrote
${liste}
its ${list}
Solution 2
First ensure that your container ships with JSTL builtin or when the container doesn't ship with it (such as Tomcat), that you've installed the proper version of JSTL. Check our JSTL wiki page for details. Don't forget to doublecheck the web.xml
version!
Then, when you want to use JSTL core taglib, ensure that it's been declared in top of JSP as per the JSTL taglib documentation:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
This way all <c:xxx>
tags will run.
Bon_chan
Updated on June 02, 2020Comments
-
Bon_chan almost 4 years
I am trying to display a list of cars from my jsp. But i don't understand why nothing appears at the runtime :
Servlet code:
public class SDisplayCar extends HttpServlet{ private static final long serialVersionUID = 1L; private Gestion gestion = Gestion.getInstance(); protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int category = Integer.parseInt(request.getParameter("category")); int place = Integer.parseInt(request.getParameter("place")); String startingDate = request.getParameter("dstart"); String endingDate = request.getParameter("dend"); Date start = gestion.getDate(startingDate); Date end = gestion.getDate(endingDate); List<Vehicle> list = gestion.getVehiclesAvailable(category,place,start,end); HttpSession session=request.getSession(); session.setAttribute("list", list); request.getRequestDispatcher("listeOfVehicle.jsp").forward(request,response); } }
JSP code:
<table border="1"> <thead> <tr> <th>Category</th> <th>Brand</th> <th>Model</th> <th>Places</th> </tr> </thead> <tbody> <c:forEach var="vehicle" items="${list}"> <tr> <td><c:out value="${vehicle.category}" /></td> <td><c:out value="${vehicle.brand}" /></td> <td><c:out value="${vehicle.model}" /></td> <td><c:out value="${vehicle.places}" /></td> </tr> </c:forEach> </tbody> </table>
Am I missing something. I don't get it. When I run the servlet it should display the list of vehicles in the jsp page.
I am using hibernate (which works fine and access the database perfectly) and using the MVC model.
Here is my form:
<body> <Form action="DisplayCar" method="post"> <TABLE BORDER=0> <TR> <TD>Category</TD> <TD> <SELECT name="category"> <OPTION VALUE="1">1</OPTION> <OPTION VALUE="2">2</OPTION> <OPTION VALUE="3">3</OPTION> <OPTION VALUE="4">4</OPTION> <OPTION selected VALUE="5">5</OPTION> </SELECT> </TD> </TR> <TR> <TD>Date</TD> <TD> <P>Starting date: <input type="text" name="dstart" /> <P>End date <input type="text" name="dend" /> </TD> </TR> <TR> <TD>Place</TD> <TD> <SELECT name="place"> <OPTION VALUE="4">4</OPTION> <OPTION VALUE="5">5</OPTION> </SELECT> </TD> </TR> <TR> <TD COLSPAN=2> <INPUT type="submit" value="Send"> </TD> </TR> </TABLE> </Form> </body> </html>
-
Bozho almost 13 yearsshow the form the submits to the servlet.
-
RMT almost 13 yearsmake sure that the the web.xml file is configured correctly
-
Bon_chan almost 13 yearsI added the submit form. I tried to display the list using JSTL.
-
Bon_chan almost 13 yearsI get the table, but there are no data inside. And my web.xml file is correctly configured.
-
RMT almost 13 yearsYou form tried to submit form to DisplayCar but your servlet is called SDisplayCar typo or error?
-
Bon_chan almost 13 yearsNo I put it that way in my web.xml <servlet-class>serv.SDisplayCar</servlet-class> <url-pattern>/DisplayCar</url-pattern>
-
RMT almost 13 yearsIs there an error message when u try to display or?
-
Bon_chan almost 13 yearsNo the page does display but no data appears.
-
RMT almost 13 yearsdoes it ever hit the server? put a break point or system.out.println
-
RMT almost 13 yearsif it does, print the size of the List make sure theres data
-
Bon_chan almost 13 yearsit does hit the server. I believe the the problem is between the servlet and the jsp file. But when i read my code I don't see any issue. I just can't figure out why data is displayed on the jsp.
-
Bon_chan almost 13 yearsIt works!!!! I put the System.out.println everywhere to check where the issue precisely was. And it was coming from getVehiclesAvailable in the servlet. It was returning no list at all, because it startingDate and endingDate were null. I was using the wrong date format. I should have checked it earlier, but didn't think it could come from that. Anyway Thanks you so much everyone for your time and comments.
-
-
Bon_chan almost 13 yearsthe doPost method is correctly invoked since it is written method="post" in the form right??
-
Bon_chan almost 13 yearsAh no I just realized I made a mistake when translating it in english (because my program uses French-named variables). So I meant list and not liste.
-
RMT almost 13 yearswhy are you translating, it would be better to see the original code.
-
BalusC almost 13 yearsBetter yet, use only English in code all the time. Also in the comments.
-
AKIWEB about 10 years@RMT I also have similar question here. Can you help me out there if possible? Any help will be appreciated on that.. Thanks for the help.