How to Display data from database with servlet/jsp

29,788

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.

Share:
29,788
Bon_chan
Author by

Bon_chan

Updated on June 02, 2020

Comments

  • Bon_chan
    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
      Bozho almost 13 years
      show the form the submits to the servlet.
    • RMT
      RMT almost 13 years
      make sure that the the web.xml file is configured correctly
    • Bon_chan
      Bon_chan almost 13 years
      I added the submit form. I tried to display the list using JSTL.
    • Bon_chan
      Bon_chan almost 13 years
      I get the table, but there are no data inside. And my web.xml file is correctly configured.
    • RMT
      RMT almost 13 years
      You form tried to submit form to DisplayCar but your servlet is called SDisplayCar typo or error?
    • Bon_chan
      Bon_chan almost 13 years
      No I put it that way in my web.xml <servlet-class>serv.SDisplayCar</servlet-class> <url-pattern>/DisplayCar</url-pattern>
    • RMT
      RMT almost 13 years
      Is there an error message when u try to display or?
    • Bon_chan
      Bon_chan almost 13 years
      No the page does display but no data appears.
    • RMT
      RMT almost 13 years
      does it ever hit the server? put a break point or system.out.println
    • RMT
      RMT almost 13 years
      if it does, print the size of the List make sure theres data
    • Bon_chan
      Bon_chan almost 13 years
      it 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
      Bon_chan almost 13 years
      It 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
    Bon_chan almost 13 years
    the doPost method is correctly invoked since it is written method="post" in the form right??
  • Bon_chan
    Bon_chan almost 13 years
    Ah 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
    RMT almost 13 years
    why are you translating, it would be better to see the original code.
  • BalusC
    BalusC almost 13 years
    Better yet, use only English in code all the time. Also in the comments.
  • AKIWEB
    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.