【问题标题】:tomcat 8 jsp how to include / import a jsp pagetomcat 8 jsp如何包含/导入jsp页面
【发布时间】:2015-03-12 19:45:30
【问题描述】:

我正在尝试从我的 servlet 在我的项目中包含/导入页眉和页脚,但到目前为止我还没有设法这样做。

我尝试过的

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);
    }

将它传递给我的 index.jsp,然后提取路径

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

但这不起作用并给出内部 500 错误。

tomcat 8 jsp javax.servlet.ServletException

我也试过了

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

但是没用

存档的正确方法是什么。 它是否传递了导入语句理解的不同值,如果是,我应该如何格式化它? 还是应该使用其他方法来归档页面的导入?

文件树

【问题讨论】:

  • 你能显示你的文件树和完整的堆栈跟踪吗?
  • 我已经添加了文件树,我不确定你对完整堆栈跟踪的意思。

标签: java jsp tomcat servlets


【解决方案1】:

file:/C:/Use ...等不是正确的写法。

相对路径会是“includes/footer.jsp”吗?

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

【讨论】:

  • getRessource 方法的结果返回一个路径文件:/C/...... 如果我将 .war 文件放在我的本地 tomcat 服务器上,它会说 /opt/tomcat... . 我使用“/includes/nav.jsp”的相对路径这适用于 元素,但到目前为止没有 .jsp 页面的运气。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-19
  • 1970-01-01
  • 1970-01-01
  • 2010-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多