Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return an html document from java servlet? [duplicate]

Tags:

This works to return a string:

import javax.servlet.http.*; @SuppressWarnings("serial") public class MonkeyServlet extends HttpServlet {     public void doGet(HttpServletRequest req, HttpServletResponse resp)         throws IOException {          resp.setContentType("text/plain");         resp.getWriter().println("got this far");      }  } 

But I can't get it to return an html document. This doesn't work:

import javax.servlet.http.*; @SuppressWarnings("serial") public class BlotServlet extends HttpServlet {     public void doGet(HttpServletRequest req, HttpServletResponse resp)         throws IOException {          resp.setContentType("text/html");         resp.getWriter().println("html/mypage.html");      }  } 

Sorry for being noob!

EDIT:

I already have the html in separate documents. So I need to either return the document, or read/parse it somehow, so I'm not just retyping all the html...

EDIT:

I have this in my web.xml

<servlet>      <servlet-name>Monkey</servlet-name>      <servlet-class>com.self.edu.MonkeyServlet</servlet-class>  </servlet>  <servlet-mapping>      <servlet-name>Monkey</servlet-name>      <url-pattern>/monkey</url-pattern>  </servlet-mapping> 

Is there something else I can put in there so it just returns a file, like...

<servlet-mapping>      <servlet-name>Monkey</servlet-name>      <file-to-return>blot.html</file-to-return>  </servlet-mapping> 
like image 656
monkey blot Avatar asked Jun 11 '13 03:06

monkey blot


People also ask

How can you generate HTML code using servlet?

Write Servlet that generates HTML Set the content type of your response, to tell the browser you will be returning plain text HTML. Get an output writer to which you can write HTML, attached to the response object. Write HTML to that writer. Close the writer.

How do we specify that the result of the servlet request should render a html page?

To make the form works with Java servlet, we need to specify the following attributes for the <form> tag: method=”post”: to send the form data as an HTTP POST request to the server. Generally, form submission should be done in HTTP POST method.

What is servlet in html?

A servlet is an extension to a server that enhances the server's functionality. The most common use for a servlet is to extend a web server by providing dynamic web content.


1 Answers

You either print out the HTML from the Servlet itself (deprecated)

PrintWriter out = response.getWriter(); out.println("<html><body>"); out.println("<h1>My HTML Body</h1>"); out.println("</body></html>"); 

or, dispatch to an existing resource (servlet, jsp etc.) (called forwarding to a view) (preferred)

RequestDispatcher view = request.getRequestDispatcher("html/mypage.html"); view.forward(request, response); 

The existing resource that you need your current HTTP request to get forwarded to does not need to be special in any way i.e. it's written just like any other Servlet or JSP; the container handles the forwarding part seamlessly.

Just make sure you provide the correct path to the resource. For example, for a servlet the RequestDispatcher would need the correct URL pattern (as specified in your web.xml)

RequestDispatcher view = request.getRequestDispatcher("/url/pattern/of/servlet"); 

Also, note that the a RequestDispatcher can be retrieved from both ServletRequest and ServletContext with the difference that the former can take a relative path as well.

Reference:
http://docs.oracle.com/javaee/5/api/javax/servlet/RequestDispatcher.html

Sample Code

public class BlotServlet extends HttpServlet {     public void doGet(HttpServletRequest req, HttpServletResponse resp)         throws IOException {         // we do not set content type, headers, cookies etc.         // resp.setContentType("text/html"); // while redirecting as         // it would most likely result in an IllegalStateException          // "/" is relative to the context root (your web-app name)         RequestDispatcher view = req.getRequestDispatcher("/path/to/file.html");         // don't add your web-app name to the path          view.forward(req, resp);         }  } 
like image 135
Ravi K Thapliyal Avatar answered Oct 21 '22 21:10

Ravi K Thapliyal