4

I've been through a few tutorials for java servlets and they all show how to display a web page generating in the java code using a servlet. How can I display an existing html page using a servlet?

I figure I need to do something with HttpServletRequest.getRequestDispatcher but not sure exactly what?

user650309
  • 2,142
  • 6
  • 25
  • 45
  • 3
    Usually if you have an static web page you just serve it directly, without any servlet intervention. Just do not include the URL to the page in the URLs mapped to a servlet, and it will work as any static resource (in the same way that your server manages static resources like images, pdf documents, etc). – SJuan76 Oct 08 '13 at 12:02
  • 1
    Anything not in WEB-INF/ will be served as static resource (special treatment for *.jsp applies) – Gyro Gearless Oct 08 '13 at 12:03

1 Answers1

3

you can do this in two ways:

  1. Request dispatcher

    ServletContext context= getServletContext(); RequestDispatcher rd= context.getRequestDispatcher("/somePage.html"); rd.forward(request, response);

  2. Response sendRedirect()

    response.sendRedirect("/someUrl.html");

See the difference between the two methods here: RequestDispatcher.forward() vs HttpServletResponse.sendRedirect()

MaVRoSCy
  • 16,907
  • 14
  • 76
  • 116