A Servlet That Generates HTML

By: aathishankaran Emailed: 1757 times Printed: 2445 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

A Servlet That Generates HTML

         Most servlets generate HTML, not plain text as in the previous example. To build HTML, you need two additional steps:

 1. Tell the browser that you’re sending back HTML, and

2. Modify the println statements to build a legal Web page.

             You accomplish the first step by setting the HTTP Content-Type response header. In general, headers are set by the setHeader method of HttpServletResponse, but setting the content type is such a common task that there is also a special setContentType method just for this purpose. The way to designate HTML is with a type of text/html, so the code would look like this:

         response.setContentType("text/html");

Although HTML is the most common type of document servlets create, it is not unusual to create other document types. For example, in the previous article (Using Servlets to Generate GIF Images) shows how servlets can build and return custom images, specifying a content type of image/gif. As a second example, in (The contentType Attribute) article shows how to generate and return Excel spreadsheets, using a content type of application/vnd.ms-excel. Don’t be concerned if you are not yet familiar with HTTP response headers; they are discussed in next article. Note that if you need to set response headers before actually returning any of the content via the Print-Writer. That’s because an HTTP response consists of the status line, one or more headers, a blank line, and the actual document, in that order.

The headers can appear in any order, and servlets buffer the headers and send them all at once, so it is legal to set the status code (part of the first line returned) even after setting headers. But servlets do not necessarily buffer the document itself, since users might want to see partial results for long pages. In version 2.1 of the servlet specification, the PrintWriter output is not buffered at all, so the first time you use the PrintWriter, it is too late to go back and set headers.

In version 2.2, servlet engines are permitted to partially buffer the output, but the size of the buffer is left unspecified. You can use the get-BufferSize method of HttpServletResponse to determine the size, or use setBufferSize to specify it. In version 2.2 with buffering enabled, you can set headers until the buffer fills up and is actually sent to the client. If you aren’t sure if the buffer has been sent, you can use the isCommitted method to check.

         The second step in writing a servlet that builds an HTML document is to have your println statements output HTML, not plain text. The structure of an HTML document is discussed more in the article (Simple HTML-Building Utilities), but it should be familiar to most readers. This program gives an example servlet, with the result.


JSP Home | All JSP Tutorials | Latest JSP Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by aathishankaran
Web Security Issues
The Web User's Perspective
Server-side plug-Ins
The best way to avoid security vulnerabilities with new server
JavaScript Security
Window Object
Working with Status Bar Messages
Retrieving a Portion of a String
Referencing Windows
Math Object
Frame Object
Document Object
Closing Windows
Built-in Object in Javascript
Textarea Object

More Tutorials in JSP
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: `java.net.BindException: Permission denied <null>:80
JSP Alert Example
JSP CheckBox Example
Uploading an Image to a Database using JSP
Uploading a file to a server using JSP
A JSP page that gets properties from a bean
The page Directive in JSP
The taglib, tag, include, attribute and the variable Directive in JSP
Declarations in JSP
Scriptlets and Expressions in JSP
Tag Libraries in JSP
The Request Object in JSP
The Response Object in JSP
The Out Object in JSP
The Session Object in JSP

More Latest News
Most Viewed Articles (in JSP )
JSP Example to connect to MS SQL database and retrieve records
What are the different scopes in JSP?
JSP Alert Example
Comparison operators in JSP
JSP Program for display Date
JSP CheckBox Example
Sending Email using JSP
Disabling Scriptlets in JSP using web.xml
Uploading an Image to a Database using JSP
Enable/Disable Scripting Elements in JSP
Embedding java codes in jsp sciptlets
Arithmetic Evaluation Using the Expression Language in JSP
Writing your first JSP page
JSP Example to connect to MS SQL database using Tomcat Connection Pool
Uploading a file to a server using JSP
Most Emailed Articles (in JSP)
The taglib, tag, include, attribute and the variable Directive in JSP
What is JSP?
Cookies using JSP or Java Bean
Click to Activate and Use this control
The JSP Program running first Time.
Scriptlets
Server Side Programming
Syntax For JSP Declaratives
Embedding java codes in jsp sciptlets
Tags using in jsp
JSP pages in servlet
Combining Scriptlets with HTML
JSP Directives
Form processing in JSP
Declaring variable in JSP