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");
                
getServletContext().getRequestDispatcher("/view/index.jsp").forward(req,
resp);
        }

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.setContextPath("/");
                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();
                handlerList.addHandler(servletContext);
                handlerList.addHandler(webAppContext);
                server.setHandler(handlerList);
                
                // Start the server!
                server.start();

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

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

  http://faculty.ycp.edu/~dhovemey/WebApp.zip

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

  http://localhost:8080/index

Any insights would be greatly appreciated.

Thanks,
Dave
_______________________________________________
jetty-users mailing list
[email protected]
https://dev.eclipse.org/mailman/listinfo/jetty-users

Reply via email to