在servlet中包含html模板

时间:2015-10-12 06:52:21

标签: java html java-ee servlets

我想在我的servlet中包含页眉和页脚模板,就像在PHP控制器中的PHP一样:

public doSomething()
{
    include "header.html";
    //generate doSomething content in HTML and echo it
    include "footer.html";
}

PS:这是一个例子,我不会在PHP中直接这样做;)

通过这种方式,我希望在所有JSP文件(包含)中避免这样:

<jsp:include page="header.html" />
<%-- Display doSomething informations -->
<jsp:include page="footer.html" />

确切地说,我想:

public void doGet( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException {
    //Include the header.html
    this.getServletContext().getRequestDispatcher( "/WEB-INF/Example.jsp" ).forward( request, response );
    //Include the footer.html
}

在这里,我想要包含页脚和标题,以免在JSP中执行此操作。

1 个答案:

答案 0 :(得分:0)

所以RP在评论中回答了这个问题,但是为了给这个问题一个实际的问题我将他复制到这里:

&#34; 一个选项是将它们读入字符串并将它们设置为请求属性并在jsp中使用这些属性或将这些字符串写入响应和调用请求调度程序的包含方法。&#34; - RP

相关问题