简体   繁体   中英

How to Display data from database with servlet/jsp

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>

In your JSP you misspelt list you wrote

${liste} its ${list}

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.

Are you sure the doPost method is getting invoked? Please check that first.

The servlet is setting the cars list to session attribute with name "list". Where as the jsp is accessing with name "liste".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM