| 1 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 |
<!-- |
| 3 |
|
| 4 |
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 5 |
|
| 6 |
Copyright (c) 2010-2011 Oracle and/or its affiliates. All rights reserved. |
| 7 |
|
| 8 |
The contents of this file are subject to the terms of either the GNU |
| 9 |
General Public License Version 2 only ("GPL") or the Common Development |
| 10 |
and Distribution License("CDDL") (collectively, the "License"). You |
| 11 |
may not use this file except in compliance with the License. You can |
| 12 |
obtain a copy of the License at |
| 13 |
http://glassfish.java.net/public/CDDL+GPL_1_1.html |
| 14 |
or packager/legal/LICENSE.txt. See the License for the specific |
| 15 |
language governing permissions and limitations under the License. |
| 16 |
|
| 17 |
When distributing the software, include this License Header Notice in each |
| 18 |
file and include the License file at packager/legal/LICENSE.txt. |
| 19 |
|
| 20 |
GPL Classpath Exception: |
| 21 |
Oracle designates this particular file as subject to the "Classpath" |
| 22 |
exception as provided by Oracle in the GPL Version 2 section of the License |
| 23 |
file that accompanied this code. |
| 24 |
|
| 25 |
Modifications: |
| 26 |
If applicable, add the following below the License Header, with the fields |
| 27 |
enclosed by brackets [] replaced by your own identifying information: |
| 28 |
"Portions Copyright [year] [name of copyright owner]" |
| 29 |
|
| 30 |
Contributor(s): |
| 31 |
If you wish your version of this file to be governed by only the CDDL or |
| 32 |
only the GPL Version 2, indicate your decision by adding "[Contributor] |
| 33 |
elects to include this software in this distribution under the [CDDL or GPL |
| 34 |
Version 2] license." If you don't indicate a single choice of license, a |
| 35 |
recipient has the option to distribute your version of this file under |
| 36 |
either the CDDL, the GPL Version 2 or to extend the choice of license to |
| 37 |
its licensees as provided above. However, if you add GPL Version 2 code |
| 38 |
and therefore, elected the GPL Version 2 license, then the option applies |
| 39 |
only if the new code is made subject to such option by the copyright |
| 40 |
holder. |
| 41 |
|
| 42 |
--> |
| 43 |
<html><head><title>Simple Servlet Example</title></head> |
| 44 |
|
| 45 |
|
| 46 |
<body> |
| 47 |
<h1>Simple Servlet Example</h1> |
| 48 |
|
| 49 |
<p>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.</p> |
| 50 |
|
| 51 |
<h2>Contents</h2> |
| 52 |
<p>The example consists of five Java classes, one HTML file, and one JPEG image:</p> |
| 53 |
<dl> |
| 54 |
<dt><code>com.sun.jersey.samples.servlet.resources.MasterResourceBean</code></dt> |
| 55 |
<dd>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.</dd> |
| 56 |
<dt><code>com.sun.jersey.samples.servlet.resources.ResourceBean1</code></dt> |
| 57 |
<dd>A resource class that produces the string "Hello World from resource 1" in response to an HTTP GET.</dd> |
| 58 |
<dt><code>com.sun.jersey.samples.servlet.resources.ResourceBean2</code></dt> |
| 59 |
<dd>A resource class that produces the string "Hello World from resource 2" in response to an HTTP GET.</dd> |
| 60 |
<dt><code>com.sun.jersey.samples.servlet.resources.ResourceBean3</code></dt> |
| 61 |
<dd>A resource class that produces different responses based on either the value of a |
| 62 |
"rep" query parameter or the "Accept" header of a HTTP GET invocation. The form contained |
| 63 |
in the index.html file will allow you to specify whether to use the query parameter |
| 64 |
or a MimeType for the request. |
| 65 |
</dd> |
| 66 |
<dt><code>com.sun.jersey.samples.servlet.resources.ResourceBean4</code></dt> |
| 67 |
<dd>A resource class that produces the string "<response>Hello World</response>" in response to an HTTP GET.</dd> |
| 68 |
<dt><code>index.html</code></dt> |
| 69 |
<dd>A static HTML file that is returned by the MasterResourceBean resource.</dd> |
| 70 |
<dt><code>java.jpg</code></dt> |
| 71 |
<dd>A JPEG image that is returned by the ResourceBean3 resource.</dd> |
| 72 |
</dl> |
| 73 |
<p>The mapping of the URI path space is presented in the following table:</p> |
| 74 |
<table border="1"> |
| 75 |
<tr> |
| 76 |
<th>URI path</th> |
| 77 |
<th>Resource class</th> |
| 78 |
<th>HTTP methods</th> |
| 79 |
</tr> |
| 80 |
<tr> |
| 81 |
<td>/start</td> |
| 82 |
<td>MasterResourceBean</td> |
| 83 |
<td>GET</td> |
| 84 |
</tr> |
| 85 |
<tr> |
| 86 |
<td>/resource1</td> |
| 87 |
<td>ResourceBean1</td> |
| 88 |
<td>GET</td> |
| 89 |
</tr> |
| 90 |
<tr> |
| 91 |
<td>/resource2</td> |
| 92 |
<td>ResourceBean2</td> |
| 93 |
<td>GET</td> |
| 94 |
</tr> |
| 95 |
<tr> |
| 96 |
<td>/resource3/{arg1}/{arg2}</td> |
| 97 |
<td>ResourceBean3</td> |
| 98 |
<td>GET</td> |
| 99 |
</tr> |
| 100 |
<tr> |
| 101 |
<td>/resource4</td> |
| 102 |
<td>ResourceBean4</td> |
| 103 |
<td>GET</td> |
| 104 |
</tr> |
| 105 |
</table> |
| 106 |
|
| 107 |
<h2>Running the Example</h2> |
| 108 |
<p>If you are working with Jersey GlassFish update center module installed into your existing GlassFish instance, |
| 109 |
you will need to follow instructions at <a href="../../README.html">the module README file</a> in order to deploy the example. |
| 110 |
<p>Otherwise, you can run the example using embedded GlassFish as follows:</p> |
| 111 |
<p> run |
| 112 |
<blockquote><code>mvn clean package embedded-glassfish:run</code></blockquote> |
| 113 |
<p>or you can run the example using Jetty as follows:</p> |
| 114 |
<blockquote><code>mvn clean package jetty:run</code></blockquote> |
| 115 |
<p>From a web browser, visit:</p> |
| 116 |
<blockquote><code><a href="http://localhost:8080/SimpleServlet/resources/start">http://localhost:8080/SimpleServlet/resources/start</a></code></blockquote> |
| 117 |
<p>Select the resource to test in the form and submit it by clicking the Test Resource |
| 118 |
button. If you select Resource 1 or Resource 2, a Test Details section appears |
| 119 |
on the form showing what the requested URL was, any Query string used, the value |
| 120 |
of the "Accept" header used, the HTTP headers received on the result, and the content |
| 121 |
of the result. |
| 122 |
|
| 123 |
Here is a sample of what you would see if you select Resource 1: |
| 124 |
</p><pre> |
| 125 |
<b>Test Details:</b><br><br> |
| 126 |
<b>Resource:</b> |
| 127 |
Resource 1<br> |
| 128 |
<b>Request:</b> |
| 129 |
/SimpleServlet/resources/resource1 |
| 130 |
<b>Query:</b> |
| 131 |
<br> |
| 132 |
|
| 133 |
<b>Accept MimeType:</b> |
| 134 |
<br> |
| 135 |
<b>Result Headers:</b> |
| 136 |
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 |
| 137 |
<b>Result:</b> |
| 138 |
Hello World from resource 1 in servlet: 'com.sun.jersey.samples.servlet.resources.MyApplication', path: '/resources' |
| 139 |
<br> |
| 140 |
<b>Debug Log:</b> |
| 141 |
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 |
| 142 |
<br> |
| 143 |
</pre> |
| 144 |
If you select Resource 3, a Resource 3 Inputs section of the form appears. |
| 145 |
Param 1 and Param 2 are URI parameters that are used in the |
| 146 |
result of testing the "text/plain" or "application/x-www-form-urlencoded" MimeType. |
| 147 |
The Selection method: lets you choose whether the "rep" query parameter or |
| 148 |
the "Accept" HTTP header should be used to retrieve the result. After filling in the |
| 149 |
Resource 3 Input, click the Test Resource 3 button to see the Test Details. |
| 150 |
<p></p> |
| 151 |
<p>You can exercise ResourceBean1 directly:</p> |
| 152 |
<blockquote><code><a href="http://localhost:8080/SimpleServlet/resources/resource1">http://localhost:8080/SimpleServlet/resources/resource1</a></code></blockquote> |
| 153 |
<p>You can exercise ResourceBean2 directly:</p> |
| 154 |
<blockquote><code><a href="http://localhost:8080/SimpleServlet/resources/resource2">http://localhost:8080/SimpleServlet/resources/resource2</a></code></blockquote> |
| 155 |
<p>From a web browser, you can only test the query parameter portion of ResourceBean3. |
| 156 |
You must provide two URI parameters and the <code>rep</code> query parameter. |
| 157 |
The following URLs can be used: |
| 158 |
</p><ul> |
| 159 |
<li><code><a href="http://localhost:8080/SimpleServlet/resources/resource3/ramesh/rajiv?rep=0">http://localhost:8080/SimpleServlet/resources/resource3/ramesh/rajiv?rep=0</a></code></li> |
| 160 |
<li><code><a href="http://localhost:8080/SimpleServlet/resources/resource3/dave/steve?rep=1">http://localhost:8080/SimpleServlet/resources/resource3/dave/steve?rep=1</a></code></li> |
| 161 |
<li><code><a href="http://localhost:8080/SimpleServlet/resources/resource3/bill/john?rep=2">http://localhost:8080/SimpleServlet/resources/resource3/bill/john?rep=2</a></code></li> |
| 162 |
<li><code><a href="http://localhost:8080/SimpleServlet/resources/resource3/marc/paul?rep=3">http://localhost:8080/SimpleServlet/resources/resource3/marc/paul?rep=3</a></code></li> |
| 163 |
</ul> |
| 164 |
<p></p> |
| 165 |
<p>You can exercise ResourceBean4 directly:</p> |
| 166 |
<blockquote><code><a href="http://localhost:8080/SimpleServlet/resources/resource4">http://localhost:8080/SimpleServlet/resources/resource4</a></code></blockquote> |
| 167 |
</body></html> |