使用Netbeans我在localhost上设置了一个基于MVC的网站。我将控制器设置为servlet,将客户端请求指向相关的JSP页面。
当我尝试直接访问JSP文件时,我没有任何问题,但是当我尝试通过控制器servlet中设置的url模式时,我得到了空白页面。我知道指向正在工作,因为如果我请求一个尚未在控制器servlet中命名的url模式,我会收到404 not found错误。
以下是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>
答案 0 :(得分:0)
将JSP放在/WEB-INF
文件夹中。它将修复两件事:
您需要修复与问题无直接关系的其他2个问题:
try-catch
。forward()
确实抛出异常。额外的好处是你不会再得到一个空白页面,因此会立即看到你自己解释异常的错误(只有当你没有通过某些自定义覆盖服务器默认HTTP 500错误页面时) {1}}反过来没有显示异常的任何内容。)