<< Chapter < Page Chapter >> Page >

Responding to the request

The headers should include content type and encoding. The content type must be set before the writer is accessed.

This servlet constructs and returns a simple HTML file containing formatted text. Listing 4 shows the use of setContentType method of the response object to set the content type being returned before accessing the writer. Then the getWriter method of the response object is used to get access to the output stream.

Listing 4 - Preparing to return a response.
res.setContentType("text/html"); PrintWriter out = res.getWriter()

You will need to do some research on your own to learn about the different content types that can be returned by a servlet.

The PrintWriter object referred to by out is used to construct output text in Listing 5 .

Construct and return an HTML file

That brings us to the fragment in Listing 5 , which constructs and returns the various elements of an HTML page and then terminates the doGet method.

Listing 5 - Construct and return an HTML file.
out.println("<html>"); out.println("<head><title>Servlet01</title></head>"); out.println("<body>");out.println("<h1 align=\"center\">" +"<font color=\"#FF0000\">"); out.println("Hello Big Red World");out.println("</font></h1>"); out.println("</body></html>"); }//end doGet()}//end class Servlet01

Constructing the HTML code

The code in Listing 5 constructs a series of String objects and passes them as parameters to the println method of the PrintWriter object referred to by out . The content of the String objects is raw HTML code.

A tedious process:

Constructing raw HTML code as a series of Java String objects can be a very tedious and error-prone process, due particularlyto the frequent need to use the backslash escape character for quotation marks inside the strings. (All attribute values in HTML must be surrounded by quotation marks.)

This is one of the arguments for the use of JSP that you learned about in the prerequisite course .. One of the benefits of JSP is that it can greatly reduce the amount of raw HTML code that you mustconstruct to send back a typical HTML web page.

If you have written any raw HTML code, you will probably recognize the construction of the HTML code in Listing 5 (and you may also recognize the HTML code as being vintage 1999 when the servlet program waswritten) .

If you haven't written any HTML, you will need to do a little research on your own to learn about the various parts of an HTML page. At this level, it is fairly simple.

(An HTML element often consists of text content surrounded by opening and closing tags of a particular type. Elements can also be nested inside of otherelements creating a hierarchical tree of elements.)

The content between the opening and closing body tags in Listing 5 represents the real information content of the page. The rest is mostly formatting information.

This body content in Listing 5 says to

  • Set the text to style h1 (which is a maximum size header).
  • Center it on the page.
  • Set the color to hexadecimal FF0000, which is the color value for pure red (in a red, green, blue color system) .

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask