Examples*
This code works well:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String url = "/jsp/pageStart.jsp";
/* requestDispatcher is NOT null */
RequestDispatcher requestDispatcher = getServletContext()
.getRequestDispatcher(url);
requestDispatcher.forward(request, response);
}
This code doesn't work, get NullPointerException, requestDispatcher is null:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String url = "http://localhost:8080/mymvc/jsp/pageStart.jsp";
/* requestDispatcher is null */
RequestDispatcher requestDispatcher = getServletContext()
.getRequestDispatcher(url)
requestDispatcher.forward(request, response);
}
Notes*
- JSP path is \src\main\webapp\jsp\pageStart.jsp
- I am sure, I can manually open 'http://localhost:8080/mymvc/jsp/pageStart.jsp', just copy and paste this url into address of new browser window.
- Base url is http://localhost:8080/mymvc/servlet/ControllerServlet
- I use Servlet mapping:
<servlet-mapping>
<servlet-name>ControllerServlet</servlet-name>
<url-pattern>/servlet/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ControllerServlet</servlet-name>
<servlet-class>controller.ControllerServlet</servlet-class>
</servlet>
Questions*
1. Why getRequestDispatcher method cannot accept full URL?
2. Could you please explain to me the reason why ? and please provide me the better resolutions.
Thanks u in advance!