简体   繁体   中英

How to serve existing web page using a java servlet

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?

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM