5

I've seen servlets examples, they're something like this:

 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">");
        ...
    }

My question is, instead of the code, can I return an HTML page? I mean, something like this:

 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            PrintWriter out = response.getWriter();

            SHOW(FILE.HTML);

        }

Thanks!!! ;)

informatik01
  • 16,038
  • 10
  • 74
  • 104
dak
  • 199
  • 1
  • 5
  • 18
  • Do you want static html or dynamic html from a jsp? – Sotirios Delimanolis Jun 06 '13 at 14:31
  • 2
    You need to use a `RequestDispatcher` and forward the request. Search around on the site for `servlet forward to jsp` or look on our servlet wiki page. – Sotirios Delimanolis Jun 06 '13 at 15:01
  • It seems that you're new to servlets in general. I recommend to check out our servlets tag wiki page. Put your mouse on top of `[servlets]` tag which you put on the question until a black box shows up and then click therein the *info* link. Other tags (may) have similar wiki pages. – BalusC Jun 06 '13 at 19:28
  • By the way, returning a XHTML file plain vanilla makes no utter sense. It has no value for webbrowsers. Just make it a HTML file. But that's a different subject. – BalusC Jun 06 '13 at 19:29
  • @BalusC I've started with jsf 2 weeks ago, with webServices 2 days ago and yesterday with servlets. You can laugh at me all you want ;) – dak Jun 07 '13 at 11:47
  • I just pointed out a misconception so that you can learn from it. You learn nothing from being laughed at. – BalusC Jun 07 '13 at 12:21

1 Answers1

11

There are a few different ways you could do this:

  1. Forward the servlet to the path where the HTML file is located. Something like:

    RequestDispatcher rd = request.getRequestDispatcher("something.html"); rd.forward(request, response);

  2. Send a redirect to the URL where the HTML is located. Something like:

    response.sendRedirect("something.html");

  3. Read in the contents of the HTML file and then write out the contents of the HTML file to the servlet's PrintWriter.

worpet
  • 3,788
  • 2
  • 31
  • 53