Java Servlet and JSP Getting Started Tutorial (4)

Source: Internet
Author: User
Tags html page
Java Servlet and JSP tutorial (4)
3.3 HTML Servlet output
Most servlets output HTML, instead of plain text. There are two additional steps to output HTML: tell the browser that HTML is sent next; modify the println statement to construct a valid HTML page.
Step 1: Set the Content-Type response header. Generally, the response header can be set through the setHeader method of HttpServletResponse. However, because the content type is set frequently, Servlet API provides a dedicated method setContentType. Note that you should set the response header before sending content through PrintWriter. The following 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"> "+
"<HTML>" +
& Nb sp; "<HEAD> <TITLE> Hello WWW </TITLE> </HEAD>" +
"<BODY>" +
"<H1> Hello WWW </H1>" +
"</BODY> </HTML> ");
}
}
3.4 HTML tool functions
It is not convenient to output HTML using println statements. The fundamental solution is to use JavaServer Pages (JSP ). However, for standard servlets, because there are two parts of the Web page (DOCTYPE and HEAD) are generally not changed, you can use tool functions to encapsulate the code that generates the content.
Although most mainstream browsers ignore the DOCTYPE line, but strictly speaking, the HTML specification requires the DOCTYPE line, which helps the HTML syntax checker to check the validity of the HTML document according to the declared HTML version. In many Web pages, the HEAD part only contains <TITLE>. Although many experienced writers will include many META tags and style declarations in the HEAD, here we only consider the simplest case.
The following Java method only accepts the page TITLE as a parameter, and then outputs the DOCTYPE, HEAD, and TITLE sections 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"> ";
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.