top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

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

+1 vote
358 views
How can I return a readily available (static) HTML page to the user instead of generating it in the servlet?
posted Jan 5, 2016 by Roshni

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

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:

Redirect:

A redirection is made using the HttpServletResponse object:

if(condition) {
      response.sendRedirect("page1.html");
   } else {
           response.sendRedirect("page2.html");
   }

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

answer Jan 6, 2016 by Karthick.c
...