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 ( 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 ( 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="" prefix="fmt" %›
‹%@ taglib uri="" prefix="c" %›

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

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"›



       ‹!--  "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. --›
       ‹!-- Declare what file type should be accessible thru Jersey
       without being interpreted as REST call --›




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 com.sun.jersey.api.view.Viewable;

public class IndexModel {

    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:

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



    Contact Service




    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


    description The requested resource () is not available.

    che you help me


Leave a Reply

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