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!!! ;)

6
  • Do you want static html or dynamic html from a jsp? Commented Jun 6, 2013 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. Commented Jun 6, 2013 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. Commented Jun 6, 2013 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. Commented Jun 6, 2013 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 ;) Commented Jun 7, 2013 at 11:47

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.