【发布时间】:2015-10-12 06:52:21
【问题描述】:
我想在我的 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 中包含不这样做的页脚和页眉。
【问题讨论】:
-
你到底想要什么?
-
转发到
Example.jsp后,页眉和页脚html都会包含在这个jsp中。 -
是的,但我问我是否可以在 Servlet 中包含页眉和页脚,而不是 JSP 文件。
-
一种选择是将它们读入字符串并将它们设置为请求属性并在jsp中使用这些属性或将这些字符串写入响应并调用请求调度程序的
include方法。 -
谢谢@RP-,是我想要的。
标签: java html jakarta-ee servlets