Simple Jersey/REST and JSP based Web Application

The aim of this post is to show how to build a lightweight REST based web application on Jersey and JSP. This article discusses how to configure Jersey and build the first test skeleton of the REST based application.

1) Create an empty dynamic web application project using Eclipse.

2) Download Jersey (http://jersey.java.net/) and place the following JARs in the WEB-INF/lib folder: asm-3.3.1.jar, jersey-client-1.12.jar (not needed but is handy for testing), jersey-core-1.12.jar, jersey-json-1.12.jar (needed later for data exchange), jersey-servlet-1.12.jar, jersey-server-1.12.jar.

3) Download a JSTL implementation, like the Apache Standard Taglib (http://tomcat.apache.org/taglibs/standard) and add jstl.jar and standard.jar to our libraries.

We use JSP as templating and dynamic page generation engine and JSTL to access Java beans. These are “old” technologies, but very efficient and well proven. Later on, AJAX calls will be performed by a light and quick REST/JSON exchange.

4) Create a file welcome-file.jsp in the web file root directory (WebContent), Content:

‹% response.sendRedirect(request.getContextPath()+"/index"); %›

5) Create the index.jsp file in the freshly made WEB-INF/jsp folder :

‹%@ page contentType="text/html; charset=UTF-8" language="java" %›

‹%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %›
‹%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %›

‹!DOCTYPE html›
‹html›
    ‹body›
        Value: [‹c:out value="${obj}"/›]
    ‹/body›
‹/html›

We store our JSP file in the WEB-INF folder, which means that they cannot accessed from outside the web application by constructing a URL. Every access has to pass via the Controller (Jersey).

6) Activate and configure Jersey in the web.xml file:

‹?xml version="1.0" encoding="UTF-8"?›
‹web-app xmlns:xsi="http://www.w3 ... version="3.0"›

  ‹display-name›jersey-rest-jsp-frame-1‹/display-name›

  ‹welcome-file-list›
    ‹welcome-file›welcome-file.jsp‹/welcome-file›
  ‹/welcome-file-list›

  ‹filter›
    ‹filter-name›jersey‹/filter-name›
    ‹filter-class›
        com.sun.jersey.spi.container.servlet.ServletContainer
    ‹/filter-class›
    ‹init-param›
       ‹!--  "Viewable" JSP root is placed in the /WEB-INF/jsp directory.
       This means that it's not accessible outside of the web app.
       There's no way to construct a URL that can retrieve it. --›
       ‹param-name›
           com.sun.jersey.config.property.JSPTemplatesBasePath
       ‹/param-name›
       ‹param-value›/WEB-INF/jsp‹/param-value›
    ‹/init-param›
    ‹init-param›
       ‹!-- Declare what file type should be accessible thru Jersey
       without being interpreted as REST call --›
       ‹param-name›
          com.sun.jersey.config.property.WebPageContentRegex
       ‹/param-name›
       ‹param-value›
          (/(image|js|css)/?.*)|(/.*.jsp)|(/WEB-INF/.*.jsp)|
          (/WEB-INF/.*.jspf)|(/.*.html)|(/favicon.ico)|
          (/robots.txt)
       ‹/param-value›
    ‹/init-param›
   ‹/filter›

   ‹filter-mapping›
      ‹filter-name›jersey‹/filter-name›
      ‹url-pattern›/*‹/url-pattern›
   ‹/filter-mapping›

‹/web-app›

By default, we open welcome-file.jsp which redirects to a REST call on /index. The IndexModel class (below) forwards to the index.jsp file.

By setting the JSPTemplatesBasePath parameter to /WEB-INF/jsp, we indicate to Jersey that the “Viewable” root is located in this directory.

The WebPageContentRegex configuration is quite important. As you might have seen, the REST mapping is set on /*, which means that by default, every HTTP request is treated as REST call. The WebPageContentRegex parameter allows us to exclude “other” web content from this rule. We indicate that we want requests to JSP, HTML, ICO and TXT files treated as non-REST request. Not doing so would exclude our-self (the welcome-file.jsp) from the web application.

7) Having Jersey acting as controller/dispatcher, we need to define a model class, in our case to handle the /index REST call:

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import com.sun.jersey.api.view.Viewable;

@Path("/")
@Produces(MediaType.TEXT_HTML)
public class IndexModel {

    @GET
    @Path("index")
    public Viewable index(@Context HttpServletRequest request) {
        request.setAttribute("obj", new String("IT Works"));
        System.out.println("/INDEX called");
        return new Viewable("/index.jsp", null);
    }
}

The class simply forwards to the index.jsp template placing a value in the request scope that is displayed by the page using the JSTL tag ‹c:out›.

The complete source code of this first example can be found on GitHub: https://github.com/imifos/sample-project-library/tree/master/jersey-rest-jsp-frame-1

4 thoughts on “Simple Jersey/REST and JSP based Web Application

  1. Very good example but it didn’t work for me as it is. Instead I used traditional web.xml for Jersey Rest application having instead of and added the required from this example. Its working.
    Thanks :)

  2. I try to follow your example using web.xml

    Jersey

    welcome-file.jsp

    Contact Service
    com.sun.jersey.spi.container.servlet.ServletContainer

    com.sun.jersey.config.property.JSPTemplatesBasePath

    /WEB-INF/jsp

    com.sun.jersey.config.property.WebPageContentRegex

    (/(image|js|css)/?.*)|(/.*.jsp)|(/WEB-INF/.*.jsp)|
    (/WEB-INF/.*.jspf)|(/.*.html)|(/favicon.ico)|
    (/robots.txt)

    1

    Contact Service
    /*


    If I dont use this sintax doesn’t start web service…
    When I put on browser :http://localhost:8080/Jersey/positionService/viewLocation my class got it in debug but when I make return new Viewable(“/index.jsp”, null); I got HTTP Status 404 –

    ——————————————————————————–

    type Status report

    message

    description The requested resource () is not available.

    che you help me

    thanks

Leave a Reply

Your email address will not be published. Required fields are marked *