Generating HTML involves as little as four lines of code in your servlets doGet() method. Your method will:
Set the content type of your response, to tell the browser you will be returning plain text HTML
Get an output writer to which you can write HTML, attached to the response object
Write HTML to that writer
Close the writer
The following MouseOver provides an example of this code:
import statements to permit short class names
define the servlet class
define the doGet method and its two parameters
list the exceptions this method might throw
set the content of the response to "text/html"
create an output writer attached to the response
write HTML to the output writer. It will reach the browser eventually
continue writing HTML to the output writer
close the output writer
import statements to permit short class names
HelloWorld Servlet Structure
This servlet will write out this HTML:
<html>
<head>
<title>Hello, World!</title>
</head>
<body>
<h1> Hello, World!</h1>
This output was generated by the module 4 servlet.
</body>
</html>
That HTML looks like this in a browser:
What the Helloworld HTML should look like in a browser
Learn how to combine static and generated HTML in the next lesson.