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>
Put the JSP in
/WEB-INFfolder. It’ll fix 2 things:You need to fix 2 other problems not directly related to the concrete problem:
try-catch.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).