Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jetty-users] problem using RequestDispatcher to forward from servlet to jsp

Hi everyone,

Apologies if this is a FAQ.

I am trying to use embedded Jetty (7.4.0 hightide release) for a web application
where I plan to use both servlets and JSP pages.  The idea is that I will use
MVC, with jsps as the views.  So, being able to forward a request from a servlet
to a view is an important requirement.

My problem is that I can't successfully forward from a servlet to a
jsp - the result
displayed in the browser is always an empty page.

If I enter the URL of the jsp (http://localhost:8080/view/index.jsp)
directly in the browser it displays correctly.

Here's my servlet's doGet() method:

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		System.out.println("In the Index servlet");

One interesting thing I have noticed is that if I use any absolute path
in the call to getRequestDispatcher(), even referring to a nonexistent
jsp, I still see the empty result page.  However. if I change this to a
relative path (e.g., "view/index.jsp") then the RequestDispatcher returned
is a null value (and doGet() then throws a NullPointerException).

Here's the main() method that launches the server:

	public static void main(String[] args) throws Exception {
		final String VIEW_PKG = "org/example/webapp/view";
		// Create Server
		Server server = new Server(8080);
		// Servlets
		ServletContextHandler servletContext =
			new ServletContextHandler(ServletContextHandler.SESSIONS);
		servletContext.addServlet(new ServletHolder(new Index()), "/index");

		// Serve JSPs via a WebAppContext
		URL viewPkgUrl = Main.class.getClassLoader().getResource(VIEW_PKG);
		String viewPkgUrlStr = viewPkgUrl.toExternalForm();
		WebAppContext webAppContext = new WebAppContext(viewPkgUrlStr, "/view");
		// create a HandlerList
		HandlerList handlerList = new HandlerList();
		// Start the server!

		// Wait until quit command is received
		System.out.println("Type 'quit' to shut down the server");
		Scanner keyboard = new Scanner(;
		while (true) {
			String line = keyboard.nextLine();
			if (line == null || line.equals("quit")) break;
		System.out.println("Server has shut down");

Here is the complete application (4.8MB, since it includes all of the
Jetty jar files):

The Main class's main() method launches the application.
You can see the empty page by viewing the URL


Any insights would be greatly appreciated.


Back to the top