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>
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.
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.
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.
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
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); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With