【问题标题】:How do I reference a JSP in a Servlet如何在 Servlet 中引用 JSP
【发布时间】:2013-06-24 01:29:46
【问题描述】:

我有一个 jsp 文件,其中包含我想要用于我的网站的所有 html 和 javascript。

我是否可以创建一个 servlet,然后让 servlet 引用 jsp 文件(而不是将所有 html 放入 servlet)?

也许是这样的:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  //initiate jsp file
}

【问题讨论】:

  • servlet 做了哪些你不想让 JSP 做的事情?
  • 没什么。 servlet 只是 JSP 的包装器

标签: html eclipse jsp servlets eclipse-wtp


【解决方案1】:

您只需要如下:

ServletContext context = getServletContext();
                 RequestDispatcher dispatcher = context.getRequestDispatcher("/thankYou.jsp");
                 dispatcher.forward(request,response);

否则,如果您不需要先实例化 Servlet,您也可以在 web.xml 中将 welcome-file 设置为 jsp 页面。

【讨论】:

  • 这正是我想要的。谢谢!
  • 有一个好的开始!干杯!!
【解决方案2】:

这么简单的方法就可以搞定

RequestDispatcher rd = request.getRequestDispatcher(response
                                    .encodeURL("/file.jsp"));
                    rd.forward(request, response);

在 response.encodeURL() 中,您可以将路径传递给 jsp 文件,或者简单地将 jsp 文件映射到 web.xml 中,然后将映射的链接直接放入 jsp。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-06
    • 2011-05-14
    • 1970-01-01
    • 2015-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多