Example 2

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Example 2.1: A servlet that prints "Hello World.

"
This servlet prints a simple hello world. Note that the book has an interesting
history on the first documented "Hello World!" program.

o Try it
o Try it (using the "hi" alias)
o Try it (using the "/hello.html" alias)
o Try it (using the "/hello/*" alias)
o Try it (using the "*.hello" alias)
o Source
o web.xml Deployment Descriptor

Web.Xml(Deployment Descriptor)

<?xml version="1.0" encoding="ISO-8859-1" ?>

  <!DOCTYPE web-app (View Source for full doctype...)>

- <web-app>

- <servlet>

  <servlet-name>hi</servlet-name>

  <servlet-class>HelloWorld</servlet-class>

  </servlet>

- <servlet-mapping>

  <servlet-name>hi</servlet-name>

  <url-pattern>/hello.html</url-pattern>

  </servlet-mapping>

- <servlet-mapping>

  <servlet-name>hi</servlet-name>

  <url-pattern>*.hello</url-pattern>

  </servlet-mapping>

- <servlet-mapping>

  <servlet-name>hi</servlet-name>

  <url-pattern>/hello/*</url-pattern>

  </servlet-mapping>

  </web-app>
Example 2.3: A servlet that knows to whom it's saying hello, modified to return
quickly in response to HEAD requests
This servlet uses the request's getMethod() method to improve performance
by detecting HEAD requests in the doGet() method so that it can return early.

o Try it
o Source

You might also like