简体   繁体   中英

tomcat 8 jsp how to include / import a jsp page

I am trying to include / import a header and a footer in my project from my servlet, but so far i havent managed to do so.

What i have tried

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext sc = getServletContext();

//      Set request attributes
        request.setAttribute("header", sc.getResource("/includes/nav.jsp").toString());
        request.setAttribute("footer", sc.getResource("/includes/footer.jsp").toString());


//      Request dispatcher
        getServletContext().getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request, response);
    }

Pass it to my index.jsp and than extract the paths

<c:import url="${header}"/>
<c:import url="${footer}"/>

But this does not work and give an internal 500 error.

tomcat 8 jsp javax.servlet.ServletException

I have also tried

sc.getRealPath("/includes/nav.jsp");
sc.getRealPath("/includes/footer.jsp");

But didnt work

What is the correct way to archive this. Is it passing a different value the import statement understand, if so how should i format it? Or should i use another method for archiving the import of the pages?

The file tree

在此处输入图片说明

file:/C:/Use ... etc is not the right way to write it.

would the relative path be "includes/footer.jsp"?

<jsp:include page=request.getServletContext().getRealPath("/")+'includes/footer.jsp' />;

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