How can I return a readily available (static) HTML page to the user instead of generating it in the servlet?

André Wolf

To solve your problem, you can either send a "Redirect" back to the client or use a RequestDispatcher and forward your request to another page:

  1. Redirect:
    A redirection is made using the HttpServletResponse object:
    if(condition) {
        response.sendRedirect("page1.html");
    } else {
        response.sendRedirect("page2.html");
    }
    
  2. RequestDispatcher:
    A request dispatcher can be obtained through the ServletContext. It can be used to include another page or to forward to it.
    if(condition) {
        this.getServletContext()
            .getRequestDispatcher("page1.html").forward();
    } else {
        this.getServletContext()
            .getRequestDispatcher("page2.html").forward();
    }
    

Both solutions require, that the pages are available in you document root. If they are located somewhere else on your filesystem, you have to open the file manually and copy their content to the output writer.

If your application server is set up in combination with a normal web server like Apache, you should use solution (1), because the the web server usually serves static files much faster than the application server.

Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact