| 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-2013 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> |
| 44 |
|
| 45 |
<head> |
| 46 |
<title>Bookstore Example</title> |
| 47 |
<style type="text/css"><!-- |
| 48 |
.cliSample { |
| 49 |
background-color: lightgray |
| 50 |
} |
| 51 |
|
| 52 |
--></style> |
| 53 |
</head> |
| 54 |
|
| 55 |
<body> |
| 56 |
<h1>Bookstore Example</h1> |
| 57 |
<p>This example demonstrates how to connect JSP pages to resources. The example is copied from |
| 58 |
the <a href="http://stapler.kohsuke.org/getting-started.html">Bookstore</a> example presented |
| 59 |
by the <a href="http://stapler.kohsuke.org/">Stapler</a> project that provides a way to staple |
| 60 |
URIs to Java classes to build RESTful Web applications such as <a href="http://java.net/projects/hudson/">Hudson</a>. |
| 61 |
</p> |
| 62 |
<p>A bookstore Web application is presented that is capable of presenting books, CDs and tracks of CDs. |
| 63 |
|
| 64 |
</p> |
| 65 |
|
| 66 |
<h2>Contents</h2> |
| 67 |
<p>The example consists of four web resources implemented by the following: |
| 68 |
</p><dl> |
| 69 |
<dt><code>com.sun.jersey.samples.bookstore.resources.Bookstore</code></dt> |
| 70 |
<dd>The bookstore resource that returns a list of items, either CDs or Books. |
| 71 |
The resource dynamically references a Book or CD resource using the |
| 72 |
getItem method that is annotated with Path. |
| 73 |
The Book and CD resource both inherit from the Item class and thus the |
| 74 |
resources can be managed polymorphically. |
| 75 |
</dd> |
| 76 |
<dt><code>com.sun.jersey.samples.bookstore.resources.Book</code></dt> |
| 77 |
<dd>The book resource that has a title and an author. |
| 78 |
</dd> |
| 79 |
<dt><code>com.sun.jersey.samples.bookstore.resources.CD</code></dt> |
| 80 |
<dd>The CD resource that has a title, author and a list of tracks. |
| 81 |
The resource dynamically references the Track resource using the |
| 82 |
getTrack method that is annotated with Path. |
| 83 |
</dd> |
| 84 |
<dt><code>com.sun.jersey.samples.bookstore.resources.Track</code></dt> |
| 85 |
<dd>The Tracks resource that has a name and the length of the track. |
| 86 |
</dd> |
| 87 |
</dl> |
| 88 |
<p>The mapping of the URI path space is presented in the following table:</p> |
| 89 |
<table border="1"> |
| 90 |
<tr> |
| 91 |
<th>URI path</th> |
| 92 |
<th>Resource class</th> |
| 93 |
<th>HTTP methods</th> |
| 94 |
<th>JSP page</th> |
| 95 |
</tr> |
| 96 |
|
| 97 |
<tr> |
| 98 |
<td>/</td> |
| 99 |
<td>Bookstore</td> |
| 100 |
<td>GET</td> |
| 101 |
<td>/com/sun/jersey/samples/bookstore/resources/Bookstore/index.jsp</td> |
| 102 |
</tr> |
| 103 |
<tr> |
| 104 |
|
| 105 |
<tr> |
| 106 |
<td>/count</td> |
| 107 |
<td>Bookstore</td> |
| 108 |
<td>GET</td> |
| 109 |
<td>/com/sun/jersey/samples/bookstore/resources/Bookstore/count.jsp</td> |
| 110 |
</tr> |
| 111 |
|
| 112 |
<tr> |
| 113 |
<td>/time</td> |
| 114 |
<td>Bookstore</td> |
| 115 |
<td>GET</td> |
| 116 |
<td>/com/sun/jersey/samples/bookstore/resources/Bookstore/time.jsp</td> |
| 117 |
</tr> |
| 118 |
|
| 119 |
<tr> |
| 120 |
<td>/items/{itemid}</td> |
| 121 |
<td>Book, CD</td> |
| 122 |
<td>GET</td> |
| 123 |
<td>/com/sun/jersey/samples/bookstore/resources/Book/index.jsp,<br> |
| 124 |
/com/sun/jersey/samples/bookstore/resources/CD/index.jsp </td> |
| 125 |
</tr> |
| 126 |
|
| 127 |
<tr> |
| 128 |
<td>/items/{itemid}/tracks/{num}</td> |
| 129 |
<td>Track</td> |
| 130 |
<td>GET</td> |
| 131 |
<td>/com/sun/jersey/samples/bookstore/resources/Track/index.jsp</td> |
| 132 |
</tr> |
| 133 |
|
| 134 |
</table> |
| 135 |
|
| 136 |
<h2>Running the Example</h2> |
| 137 |
|
| 138 |
|
| 139 |
<p>Bookstore example runs on Glassfish 3.1.2.2 application server (<a href="http://glassfish.java.net/">http://glassfish.java.net</a>), |
| 140 |
Jetty or another Servlet container of your choice (not tested). |
| 141 |
|
| 142 |
<p>If you are working with Jersey GlassFish update center module installed into your existing GlassFish instance, |
| 143 |
you will need to follow instructions at <a href="../../README.html">the module README file</a> in order to deploy the example. |
| 144 |
<p>Otherwise, you can run the example using Jetty as follows:</p> |
| 145 |
|
| 146 |
<p>Build and deploy the project by executing the following command from the project directory</p> |
| 147 |
<blockquote><code>mvn clean package jetty:run</code></blockquote> |
| 148 |
|
| 149 |
<p>Go to the URL:<p> |
| 150 |
<blockquote><a href="http://localhost:8080/bookstore/">http://localhost:8080/bookstore/</a></blockquote> |
| 151 |
|
| 152 |
<h4>Test Client Running</h4> |
| 153 |
|
| 154 |
<pre class="cliSample"> |
| 155 |
mvn test -DargLine="-Djersey.test.containerFactory=com.sun.jersey.test.framework.spi.container.external.ExternalTestContainerFactory -DenableLogging -Djersey.test.port=8080" -Ptest |
| 156 |
</pre> |
| 157 |
|
| 158 |
<h2>How it works</h2> |
| 159 |
<p>This example shows how to support polymorphism of resources and JSP pages. |
| 160 |
Hence it is possible to add another resource, such as a DVD resource with associated JSP pages, |
| 161 |
which extends Item without having to change the logic of Bookstore or the existing JSP pages.</p> |
| 162 |
|
| 163 |
<p>JSP pages are associated with resource classes. Such JSP pages are resolved |
| 164 |
by converting the fully qualified class name of the resource class into a path |
| 165 |
and appending last path segment of the request URI path to that path. |
| 166 |
|
| 167 |
For example, when a GET is performed on the URI path "/" then the path |
| 168 |
to the JSP page is "/com/sun/jersey/samples/bookstore/resources/Bookstore/", |
| 169 |
and in this case since the last path segment is empty "index.jsp" is |
| 170 |
appended to the path. Then the request us forwarded to the JSP page |
| 171 |
at that path. |
| 172 |
|
| 173 |
Similarly when a GET is performed on the URI path "/count" then the |
| 174 |
path to the JSP page is "/com/sun/jersey/samples/bookstore/resources/Bookstore/count.jsp". |
| 175 |
</p> |
| 176 |
<p>The JSP variable "it" is set to the instance of Bookstore so that the index.jsp, or count.jsp, |
| 177 |
has access to the Bookstore instance as a Java bean. |
| 178 |
</p> |
| 179 |
<p>If a resource class inherits from another resource class then it will automatically |
| 180 |
inherit the JSPs from the super class.</p> |
| 181 |
<p>A JSP page may also include JSPs using the inheritance mechanism, |
| 182 |
for example the index.jsp page associated with the Book resource class includes a |
| 183 |
footer.jsp page whoose location is specified by the super class, Item. |
| 184 |
</p> |
| 185 |
</body></html> |