Introduction to Java Servlet and JSP Tutorials (4)

Source: Internet
Author: User
Tags contains final functions header html page include require return
js|servlet| | Getting Started Tutorial Java servlet and JSP Tutorials (4)

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 "+
"&AMP;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>");
}
}java Servlet and JSP Tutorials (4)

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 "+
"&AMP;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>");
}
}java Servlet and JSP Tutorials (4)

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 "+
"&AMP;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>");
}
}java Servlet and JSP Tutorials (4)

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 "+
"&AMP;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>");
}
}

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.