X
  • REST
    April 22, 2009

Jersey aplication sharing Grizzly with static content and servlets

I was asked the question some time ago by one of my colleges here at SUN, and the same question was asked again recently at our user mailing list: "I want to run Jersey based application on Grizzly web server, but i want to also run another servlet from there, and possibly
also serve some static content using the very same Grizzly sever instance. How do i do that?"

The answer is as simple as follows. Having imported

import com.sun.grizzly.http.embed.GrizzlyWebServer;
import com.sun.grizzly.http.servlet.ServletAdapter;
import com.sun.jersey.spi.container.servlet.ServletContainer;

You can just use:

        // static content is linked from here
GrizzlyWebServer gws = new GrizzlyWebServer(8080, "/var/www");
// Jersey web resources
ServletAdapter jerseyAdapter = new ServletAdapter();
jerseyAdapter.addInitParameter("com.sun.jersey.config.property.packages",
"com.example");
jerseyAdapter.setContextPath("/jersey");
jerseyAdapter.setServletInstance(new ServletContainer());
// Another non-Jersey servlet
ServletAdapter simpleServletAdapter = new ServletAdapter();
simpleServletAdapter.setContextPath("/simple");
simpleServletAdapter.setServletInstance(new SimpleServlet());
// register all above defined adapters
gws.addGrizzlyAdapter(jerseyAdapter, new String[] {"/jersey"});
gws.addGrizzlyAdapter(simpleServletAdapter, new String[] {"/simple"});
// let Grizzly run
gws.start();

Your Jersey resource classes are placed in com.example package, and will become accessible at http://localhost:8080/jersey base.
Your static content for http://localhost:8080/ is taken from /var/www directory and a SimpleServlet will become available at http://localhost:8080/simple.

The only dependency you will need to include other than Jersey dependencies itself is

        <dependency>
<groupId>com.sun.grizzly</groupId>
<artifactId>grizzly-servlet-webserver</artifactId>
<version>1.9.10</version>
</dependency>

If you are interested in some more advanced scenarios, you can get inspired at Jean-Francois Arcand's blog post

Join the discussion

Comments ( 4 )
  • Florian Saturday, June 6, 2009

    Dear Jakub,

    Thanks for this article. I'm exactly facing the problem that you address here: I would like to serve some jersey webservices, along with a few static pages.

    However I'm unable to get your example up and running:

    GrizzlyWebServer gws = new GrizzlyWebServer(8080, "/var/www");

    gives mir a webserver which serves all content from /var/www. However as sonn as I include the following lines of code ( ServletAdapter jerseyAdapter = ...) the static content is no longer served.

    Instead it gives me a "Resource not Found".

    I realy don't know how to proceed...


  • Ted Hesselroth Friday, June 12, 2009

    Turns out Grizzly wipes out the original static adapter when the servlet adapter is added. It works to add an empty one back.

    GrizzlyWebServer gws = new GrizzlyWebServer(8082, "/home/tdh/source");

    // Jersey web resources

    ServletAdapter jerseyAdapter = new ServletAdapter();

    jerseyAdapter.addInitParameter("com.sun.jersey.config.property.packages", "gov.fnal.xpathsearch.rest");

    jerseyAdapter.setContextPath("/jersey");

    jerseyAdapter.setServletInstance(new ServletContainer());

    // register all above defined adapters

    gws.addGrizzlyAdapter(jerseyAdapter, new String[] {"/"});

    tAdapter, new String[] {"/simple"});

    gws.addGrizzlyAdapter(new GrizzlyAdapter(){

    public void service(GrizzlyRequest request, GrizzlyResponse response){

    }

    }, new String[] {"/"});

    // let Grizzly run

    gws.start();


  • Andy Tuesday, October 27, 2009

    Hi,

    I have little different question.

    I need to add to running Grizzly server instance new Resource. Is this possible?

    Sorry for my English.


  • Michel Buffa Thursday, February 18, 2010

    Hi ! I have some static web pages in a "res" directory. When I try :

    public class TestGrizzly {

    public static void main(String[] args) {

    GrizzlyWebServer ws = new GrizzlyWebServer(999, "res");

    ws.start();

    ...

    it works with localhost:999/home.html, but as soon as I add the jersey adapter or the empty adapter, it does not serve the static html pages anymore :

    For example :

    public class TestGrizzly {

    public static void main(String[] args) {

    File docroot = new File("./res");

    GrizzlyWebServer ws = new GrizzlyWebServer(999, "res");

    // Jersey web resources

    ServletAdapter jerseyAdapter = new ServletAdapter();

    jerseyAdapter.addInitParameter("com.sun.jersey.config.property.packages",

    "bmserv.resources");

    jerseyAdapter.setContextPath("/jersey");

    jerseyAdapter.setServletInstance(new ServletContainer());

    ws.addGrizzlyAdapter(jerseyAdapter, new String[]{"/jersey"});

    ws.addGrizzlyAdapter(new GrizzlyAdapter() {

    public void service(GrizzlyRequest request, GrizzlyResponse response) {

    }

    }, new String[]{"/"});

    try {

    ws.start();

    } catch (IOException ex) {

    System.out.println(ex.getMessage());

    }

    Serves correctly the jersey resources, but no more the /home.html page. Even if I just keep the static empty adapter, the html page is not served.

    Any help appreciated


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha