Question

I'm just puzzled, when I use the <jsp:include file="include/data.jsp" /> in indexq.jsp my data didn't show up, but when I use <%@ include file="include/data.jsp" %> it works as expected. I'm not sure if its a scope or expression language issue. I also included the code below:

TaxiController.java

public class TaxiController extends HttpServlet {

    // codes...

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

                // codes...
        req.setAttribute("taxi_list", taxiDao.getAll());
        req.getRequestDispatcher("/indexq.jsp").forward(req, resp);

    }
}

indexq.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <script src="js/jquery-1.10.1.min.js" ></script>
        <title>Taxi List</title>
    </head>
    <body>
        <%@ include file="include/form.jsp" %>
        <br />
        <jsp:include page="include/data.jsp"  />   
        <%-- <%@ include file="include/data.jsp" %> --%>  
    </body>
</html>

include/data.jsp

<table>
    <thead>
        <tr><th colspan="5">Data</th></tr>
        <tr>
            <th>Date</th>
            <th>Taxi Name</th>
            <th>Plate number</th>
            <th>Amount</th>
        </tr>
    </thead>
    <tbody>
        <c:forEach var="taxi" items="${taxi_list }" >
        <tr>
            <td>${taxi.date } </td>
            <td>${taxi.taxiName }</td>
            <td>${taxi.plateNum }</td>
            <td>${taxi.amount }</td>
        </tr>
        </c:forEach>
    </tbody>
</table>

Thanks!

Était-ce utile?

La solution

Basically the <%@ include file="include/form.jsp" %> use the same context/request and for the <jsp:include file="include/data.jsp" /> it uses a separete request.

So in your case it didn't work because you set the value as an attribute of the request

Below are some detailed info about it extracted from the link: http://www.objectpartners.com/2011/04/14/jsp-to-include-or-jspinclude/

The <jsp:include page=”"/> tag behaves differently in that the result of rendering the specified page is injected into the containing JSP at the point of the tag. This is done by essentially submitting the requested page to the same container, as a separate rendering request, and taking the results, not the content of the file. This request is done in its own context, meaning it doesn’t use the same page information as the page that contains the tag. This can be handy, especially if the included content may have conflicting variables

The <%@include file=”" %> tag will inject the contents of the named file into the JSP containing the tag, as if it were copied and pasted. This is done before the content of the included file is parsed, instead parsing it while the containing JSP is parsed. This is most akin to a C #include directive, where during pre-processing the included file is “pasted” into place before the file is compiled. After the content is included, it is evaluated, all in the same context, and therefore with the same accesses and constraints the included code would have if the contents were simply typed in place.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top