Simple Servlet Example

This example demonstrates how to develop RESTful web services with a Servlet 3.0 container, if you are working with Jersey GlassFish update center module installed into your existing GlassFish instance. Otherwise, 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

If you are working with Jersey GlassFish update center module installed into your existing GlassFish instance, you will need to follow instructions at the module README file in order to deploy the example.

Otherwise, you can run the example using embedded GlassFish as follows:

run

mvn clean package embedded-glassfish:run

or you can run the example using Jetty as follows:

mvn clean package jetty: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: /SimpleServlet/resources/resource1 Query:
Accept MimeType:
Result Headers: X-Powered-By: Servlet/3.0 JSP/2.2 (GlassFish Server Open Source Edition 3.1 Java/Sun Microsystems Inc./1.6) Server: GlassFish Server Open Source Edition 3.1 Content-Type: text/plain Transfer-Encoding: chunked Date: Tue, 22 Mar 2011 03:32:47 GMT Result: Hello World from resource 1 in servlet: 'com.sun.jersey.samples.servlet.resources.MyApplication', path: '/resources'
Debug Log: updatepage: resultheaders str: X-Powered-By: Servlet/3.0 JSP/2.2 (GlassFish Server Open Source Edition 3.1 Java/Sun Microsystems Inc./1.6) Server: GlassFish Server Open Source Edition 3.1 Content-Type: text/plain Transfer-Encoding: chunked Date: Tue, 22 Mar 2011 03:32:47 GMT
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://localhost:8080/SimpleServlet/resources/resource1

You can exercise ResourceBean2 directly:

http://localhost:8080/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:

You can exercise ResourceBean4 directly:

http://localhost:8080/SimpleServlet/resources/resource4