# Simple Servlet Example

This example demonstrates how to develop RESTful web services with a Servlet 2.5 container.

## Contents

The example consists of five Java classes, one HTML file, and one JPEG image:

`com.sun.jersey.samples.servlet.resources.MasterResourceBean`
  A resource class that produces a form in response to an HTTP GET. Tthe form is used to exercise the other resources in this sample applicaton.
`com.sun.jersey.samples.servlet.resources.ResourceBean1`
  A resource class that produces the string "Hello World from resource 1" in response to an HTTP GET.
`com.sun.jersey.samples.servlet.resources.ResourceBean2`
  A resource class that produces the string "Hello World from resource 2" in response to an HTTP GET.
`com.sun.jersey.samples.servlet.resources.ResourceBean3`
  A resource class that produces different responses based on either the value of a "rep" query parameter or the "Accept" header of a HTTP GET invocation. The form contained in the index.html file will allow you to specify whether to use the query parameter or a MimeType for the request.
`com.sun.jersey.samples.servlet.resources.ResourceBean4`
  A resource class that produces the string "<response>Hello World</response>" in response to an HTTP GET.
`index.html`
  A static HTML file that is returned by the MasterResourceBean resource.
`java.jpg`
  A JPEG image that is returned by the ResourceBean3 resource.

The mapping of the URI path space is presented in the following table:

URI path | Resource class | HTTP methods
--- | --- | ---
/start | MasterResourceBean | GET
/resource1 | ResourceBean1 | GET
/resource2 | ResourceBean2 | GET
/resource3/{arg1}/{arg2} | ResourceBean3 | GET
/resource4 | ResourceBean4 | GET

## Running the Example

Run the example as follows:

run

> `mvn glassfish:run`

From a web browser, visit:

> `http://localhost:8080/SimpleServlet/resources/start`

Select the resource to test in the form and submit it by clicking the Test Resource button. If you select Resource 1 or Resource 2, a Test Details section appears on the form showing what the requested URL was, any Query string used, the value of the "Accept" header used, the HTTP headers received on the result, and the content of the result. Here is a sample of what you would see if you select Resource 1:

                Test Details:

                Resource:
                Resource 1

                Request:
                /endpoint/resource1
                Query:

                Accept MimeType:

                Result Headers:
                Last-Modified: Tue, 09 Jan 2007 20:59:59 GMT Content-Type: text/plain Transfer-Encoding: chunked
                Result:
                Hello World from resource 1

If you select Resource 3, a Resource 3 Inputs section of the form appears. Param 1 and Param 2 are URI parameters that are used in the result of testing the "text/plain" or "application/x-www-form-urlencoded" MimeType. The Selection method: lets you choose whether the "rep" query parameter or the "Accept" HTTP header should be used to retrieve the result. After filling in the Resource 3 Input, click the Test Resource 3 button to see the Test Details.

You can exercise ResourceBean1 directly:

> `http:<host:port>//SimpleServlet/resources/resource1`

You can exercise ResourceBean2 directly:

> `http:<host:port>//SimpleServlet/resources/resource2`

From a web browser, you can only test the query parameter portion of ResourceBean3. You must provide two URI parameters and the `rep` query parameter. The following URLs can be used:

- `http:<host:port>//SimpleServlet/resources/resource3/ramesh/rajiv?rep=0`
- `http:<host:port>//SimpleServlet/resources/resource3/dave/steve?rep=1`
- `http:<host:port>//SimpleServlet/resources/resource3/bill/john?rep=2`
- `http:<host:port>//SimpleServlet/resources/resource3/marc/paul?rep=3`

You can exercise ResourceBean4 directly:

> `http:<host:port>//SimpleServlet/resources/resource4`