1

I use a servlet to take argumentes with the doGet method from the user by clicking on a submit button. After that it sends him to my servlet's webpage. But to present the new webpage of the servlet I end up creating a page like this:

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    response.setContentType("text/html;charset=UTF-8");

    try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet</title>");            
        out.println("</head>");
        out.println("<body>");

        out.println("<h1> Create your webpage here</h1>");

        out.println("</body>");
        out.println("</html>");
    }

Is there a way to open a certain html file inside the Servlet? because this really narrows my ability to create a good looking page with javascript,css etc.

I tried using:

File htmlFile = new File(url);
Desktop.getDesktop().browse(htmlFile.toURI());

but the servlet wont let me because servlets are quirky and capricious. how can i overcome this obstacle?

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
eliodorou
  • 13
  • 1
  • 5

1 Answers1

3

You can put your static html (or dynamic JSPs) under WEB-INF (or WEB-INF/html below), and then use RequestDispatcher.include(ServletRequest, ServletResponse) (or forward). Something like,

RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/html/myfile.html");
rd.include(request, response);
Elliott Frisch
  • 183,598
  • 16
  • 131
  • 226