Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Why getRequestDispatcher method cannot accept full URL?

843842Aug 5 2009 — edited Aug 6 2009
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!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 3 2009
Added on Aug 5 2009
1 comment
630 views