This example demonstrates how to connect JSP pages to resources. The example is copied from the Bookstore example presented by the Stapler project that provides a way to staple URIs to Java classes to build RESTful Web applications such as Hudson.
A bookstore Web application is presented that is capable of presenting books, CDs and tracks of CDs.
The example consists of four web resources implemented by the following:
com.sun.jersey.samples.bookstore.resources.Bookstore
com.sun.jersey.samples.bookstore.resources.Book
com.sun.jersey.samples.bookstore.resources.CD
com.sun.jersey.samples.bookstore.resources.Track
The mapping of the URI path space is presented in the following table:
URI path | Resource class | HTTP methods | JSP page |
---|---|---|---|
/ | Bookstore | GET | /com/sun/jersey/samples/bookstore/resources/Bookstore/index.jsp |
/count | Bookstore | GET | /com/sun/jersey/samples/bookstore/resources/Bookstore/count.jsp |
/time | Bookstore | GET | /com/sun/jersey/samples/bookstore/resources/Bookstore/time.jsp |
/items/{itemid} | Book, CD | GET | /com/sun/jersey/samples/bookstore/resources/Book/index.jsp, /com/sun/jersey/samples/bookstore/resources/CD/index.jsp |
/items/{itemid}/tracks/{num} | Track | GET | /com/sun/jersey/samples/bookstore/resources/Track/index.jsp |
Bookstore example runs on Glassfish 3.1.2.2 application server (http://glassfish.java.net), Jetty or another Servlet container of your choice (not tested).
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 Jetty as follows:
Build and deploy the project by executing the following command from the project directory
mvn clean package jetty:run
Go to the URL:
http://localhost:8080/bookstore/
mvn test -DargLine="-Djersey.test.containerFactory=com.sun.jersey.test.framework.spi.container.external.ExternalTestContainerFactory -DenableLogging -Djersey.test.port=8080" -Ptest
This example shows how to support polymorphism of resources and JSP pages. Hence it is possible to add another resource, such as a DVD resource with associated JSP pages, which extends Item without having to change the logic of Bookstore or the existing JSP pages.
JSP pages are associated with resource classes. Such JSP pages are resolved by converting the fully qualified class name of the resource class into a path and appending last path segment of the request URI path to that path. For example, when a GET is performed on the URI path "/" then the path to the JSP page is "/com/sun/jersey/samples/bookstore/resources/Bookstore/", and in this case since the last path segment is empty "index.jsp" is appended to the path. Then the request us forwarded to the JSP page at that path. Similarly when a GET is performed on the URI path "/count" then the path to the JSP page is "/com/sun/jersey/samples/bookstore/resources/Bookstore/count.jsp".
The JSP variable "it" is set to the instance of Bookstore so that the index.jsp, or count.jsp, has access to the Bookstore instance as a Java bean.
If a resource class inherits from another resource class then it will automatically inherit the JSPs from the super class.
A JSP page may also include JSPs using the inheritance mechanism, for example the index.jsp page associated with the Book resource class includes a footer.jsp page whoose location is specified by the super class, Item.