通过url模式调用JSP文件时出现空白页

发布于 2024-12-22 20:36:14 字数 4395 浏览 3 评论 0原文

我正在使用 Netbeans 在 localhost 上设置一个由 MVC 驱动的网站。我已将控制器设置为 servlet,它将客户端请求指向相关的 JSP 页面。

当我尝试直接访问 JSP 文件时,没有任何问题,但是当我尝试通过控制器 servlet 中设置的 url 模式时,我得到空白页面。我知道该指向有效,因为如果我请求一个未在控制器 servlet 中命名的 url 模式,我会收到 404 未找到错误。

以下是 Servlet 模板文件的代码:

 public class ControllerServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    String userPath = request.getServletPath();
    if (userPath.equals("/book")) {   
    }
    String url = "/WEB-INF" + userPath + ".jsp";
    request.getRequestDispatcher(url).forward(request, response);
}


@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

    String userPath = request.getServletPath();
    if (userPath.equals("/profile")) {
    } else if (userPath.equals("/updates")) {
    } else if (userPath.equals("/mybooks")) {
    }
    String url = "/WEB-INF/view" + userPath + ".jsp";
    request.getRequestDispatcher(url).forward(request, response);
}

 }

ControllerServlet 文件在标准 HTTPServlet 代码之上包含一行附加行:

@WebServlet(name = "ControllerServlet", loadOnStartup = 1, urlPatterns = {"/profile", "/mybooks", "/updates", "/book"})

XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>
<context-param>
    <description>The relative path to images of book covers.</description>
    <param-name>coversImagePath</param-name>
    <param-value>img/bookCovers</param-value>
</context-param>
<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>0</load-on-startup>
</servlet>
<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
        <param-name>config</param-name>
        <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
        <param-name>debug</param-name>
        <param-value>2</param-value>
    </init-param>
    <init-param>
        <param-name>detail</param-name>
        <param-value>2</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<jsp-config>
    <jsp-property-group>
        <description>header and footer settings</description>
        <url-pattern>/index.jsp</url-pattern>
        <url-pattern>/WEB-INF/view/*</url-pattern>
        <url-pattern>/book.jsp</url-pattern>
        <include-prelude>/WEB-INF/jspf/header.jspf</include-prelude>
        <include-coda>/WEB-INF/jspf/footer.jspf</include-coda>
    </jsp-property-group>
</jsp-config>
<resource-ref>
    <description>Connects to database for bookshelves application.</description>
    <res-ref-name>jdbc/bookshelves</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
</web-app>

Using Netbeans I'm setting up a MVC-powered website on localhost. I've set up the controller as a servlet which points client requests to the relevant JSP pages.

When I try to access the JSP files directly I have no problems, but when I try via the url patterns set in the controller servlet I get blank pages. I know the pointing is working because if I request a url pattern that hasn't been named in the controller servlet I get a 404 not found error.

Here is the code for the servlet template file:

 public class ControllerServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    String userPath = request.getServletPath();
    if (userPath.equals("/book")) {   
    }
    String url = "/WEB-INF" + userPath + ".jsp";
    request.getRequestDispatcher(url).forward(request, response);
}


@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

    String userPath = request.getServletPath();
    if (userPath.equals("/profile")) {
    } else if (userPath.equals("/updates")) {
    } else if (userPath.equals("/mybooks")) {
    }
    String url = "/WEB-INF/view" + userPath + ".jsp";
    request.getRequestDispatcher(url).forward(request, response);
}

 }

The ControllerServlet file contains an additional line on top of the standard HTTPServlet code:

@WebServlet(name = "ControllerServlet", loadOnStartup = 1, urlPatterns = {"/profile", "/mybooks", "/updates", "/book"})

XML file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>
<context-param>
    <description>The relative path to images of book covers.</description>
    <param-name>coversImagePath</param-name>
    <param-value>img/bookCovers</param-value>
</context-param>
<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>0</load-on-startup>
</servlet>
<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
        <param-name>config</param-name>
        <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
        <param-name>debug</param-name>
        <param-value>2</param-value>
    </init-param>
    <init-param>
        <param-name>detail</param-name>
        <param-value>2</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<jsp-config>
    <jsp-property-group>
        <description>header and footer settings</description>
        <url-pattern>/index.jsp</url-pattern>
        <url-pattern>/WEB-INF/view/*</url-pattern>
        <url-pattern>/book.jsp</url-pattern>
        <include-prelude>/WEB-INF/jspf/header.jspf</include-prelude>
        <include-coda>/WEB-INF/jspf/footer.jspf</include-coda>
    </jsp-property-group>
</jsp-config>
<resource-ref>
    <description>Connects to database for bookshelves application.</description>
    <res-ref-name>jdbc/bookshelves</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
</web-app>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

莫相离 2024-12-29 20:36:14

将 JSP 放入 /WEB-INF 文件夹中。它将解决两件事:

  1. 您不会得到空白页。
  2. 您将无法直接访问 JSP(这是正确且理想的行为!)

您需要解决与具体问题不直接相关的 2 个其他问题:

  1. 除非您知道自己在做什么,否则永远不要抑制异常。删除 try-catch
  2. 永远不要忽略服务器日志。 forward() 确实抛出了异常。

额外的好处是,您不会再得到空白页面,因此会立即以不言自明的异常的方式看到您的错误(仅当您没有通过某些自定义 < 覆盖服务器默认的 HTTP 500 错误页面时) ;error-page> 反过来又不显示任何异常)。

Put the JSP in /WEB-INF folder. It'll fix 2 things:

  1. You won't get a blank page.
  2. You won't be able to access the JSP directly (which is correct and desired behaviour!)

You need to fix 2 other problems not directly related to the concrete problem:

  1. Never suppress exceptions unless you know what you're doing. Remove that try-catch.
  2. Never ignore server logs. The forward() did throw an exception.

Additional benefit is that you won't get a blank page anymore and thus will see your mistake immediately in flavor of a self-explaining exception (only if you didn't override the server default HTTP 500 error page by some custom <error-page> which in turn doesn't show anything of the exception).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文