js|servlet| Tutorial
3.3 servlet for output HTML
Most servlet output HTML instead of plain text as in the example above. There are two additional steps to output HTML: Tell the browser to send the HTML next, and modify the PRINTLN statement to construct a legitimate HTML page.
The first step is done by setting the Content-type (content Type) answer header. Generally, the answer header can be set through the HttpServletResponse SetHeader method, but because setting the content type is a very frequent operation, the Servlet API provides a dedicated method setContentType. Note Setting up the answer header should be done before the content is sent through PrintWriter. Here is an example:
Hellowww. java
Package Hall;
Import java.io.*;
Import javax.servlet.*;
Import javax.servlet.http.*;
public class Hellowww extends HttpServlet {
public void doget (HttpServletRequest request,
HttpServletResponse response)
Throws Servletexception, IOException {
Response.setcontenttype ("text/html");
PrintWriter out = Response.getwriter ();
Out.println ("<! DOCTYPE HTML public \ "-//W3C//DTD HTML 4.0" +
"Transitional//en\" >\n "+
"&NB sp; ""<body>\n" +
""</BODY></HTML>");
}
}
3.4 Several HTML tool functions
Exporting HTML through the PRINTLN statement is not convenient, and the fundamental solution is to use JavaServer Pages (JSP). However, for a standard servlet, there are two parts of the Web page (DOCTYPE and head) that generally do not change, so you can use a tool function to encapsulate the code that generates the content.
While most mainstream browsers ignore DOCTYPE lines, strictly speaking, HTML specifications require DOCTYPE lines, which help the HTML grammar checker check the legality of HTML documents based on the declared HTML version. In many web pages, the Head section contains only <TITLE>. Although many experienced writers include many meta tags and style declarations in the head, only the simplest scenarios are considered here.
The following Java method takes only the page title as a parameter and then outputs the DOCTYPE, head, and Title section of the page. The list is as follows:
Servletutilities.java
Package Hall;
public class Servletutilities {
public static final String DOCTYPE =
"<! DOCTYPE HTML public \-//W3C//DTD HTML 4.0 transitional//en\ ">";
public static string Headwithtitle (string title) {
Return (DOCTYPE + "\ n" + ""<HEAD><TITLE>" + TITLE + "</title >}
The code for other tool functions is described later in this article
}
Hellowww2.java
The following is the HelloWWW2 that overrides the Hellowww class after applying the servletutilities:
Package Hall;
Import java.io.*;
Import javax.servlet.*;
Import javax.servlet.http.*;
public class HelloWWW2 extends HttpServlet {
public void doget (HttpServletRequest request,
HttpServletResponse response)
Throws Servletexception, IOException {
Response.setcontenttype ("text/html");
PrintWriter out = Response.getwriter ();
Out.println (Servletutilities.headwithtitle ("Hello WWW") +
"<body>\n" +
""</BODY></HTML>");
}
}