Jersey JSP View running on Grizzly server

After I read the stackoverflow question on the subj [1], I decided to try it myself, cause theoretically there shouldn’t have been any problem running JSP on Grizzly.

And it was actually true, except configuration part: Jasper classloader, JSP API and implementation versions, JSTL version etcetc.

Finally the Jersey resource (taken from this sample) looks like:

@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);
    }
}

And Grizzly server initialization code looks like this:

public class Main {
    private static final String JERSEY_SERVLET_CONTEXT_PATH = "";
    private static final String JSP_CLASSPATH_ATTRIBUTE =
            "org.apache.catalina.jsp_classpath";

    private static final URI BASE_URI = UriBuilder
            .fromUri("http://127.0.0.1/").port(8080).build();

    protected static HttpServer startServer() throws IOException {
        System.out.println("Starting grizzly...");

        // Create HttpServer and register dummy "not found" HttpHandler
        HttpServer httpServer = GrizzlyServerFactory.createHttpServer(BASE_URI, new HttpHandler() {

            @Override
            public void service(Request rqst, Response rspns) throws Exception {
                rspns.setStatus(404, "Not found");
                rspns.getWriter().write("404: not found");
            }
        });

        WebappContext context = new WebappContext("WebappContext", JERSEY_SERVLET_CONTEXT_PATH);

        // Initialize and register Jersey Servlet
        FilterRegistration registration = context.addFilter("ServletContainer", ServletContainer.class);
        registration.setInitParameter(ServletContainer.RESOURCE_CONFIG_CLASS, 
                ClassNamesResourceConfig.class.getName());
        registration.setInitParameter(ClassNamesResourceConfig.PROPERTY_CLASSNAMES, IndexModel.class.getName());
        registration.setInitParameter("com.sun.jersey.config.property.JSPTemplatesBasePath",
                "/WEB-INF/jsp");
        // configure Jersey to bypass non-Jersey requests (static resources and jsps)
        registration.setInitParameter("com.sun.jersey.config.property.WebPageContentRegex",
                "(/(image|js|css)/?.*)|(/.*\\.jsp)|(/WEB-INF/.*\\.jsp)|"
                + "(/WEB-INF/.*\\.jspf)|(/.*\\.html)|(/favicon\\.ico)|"
                + "(/robots\\.txt)");

        registration.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), "/*");

        // Initialize and register JSP Servlet        
        ServletRegistration jspRegistration = context.addServlet(
                "JSPContainer", JspServlet.class.getName());
        jspRegistration.addMapping("/*");

        // Set classpath for Jasper compiler based on the current classpath
        context.setAttribute(JSP_CLASSPATH_ATTRIBUTE,
                System.getProperty("java.class.path"));

        context.deploy(httpServer);

       return httpServer;
    }

    /**
     * Run standalone server
     */
    public static void main(String[] args) throws IOException {
        HttpServer httpServer = startServer();
        try {
            System.out.println(String.format("Jersey app started with WADL available at "
                    + "%sapplication.wadl\nTry out %stest\nHit enter to stop it...",
                    BASE_URI, BASE_URI));
            System.in.read();
        } finally {
            httpServer.stop();
        }
    }

Maven dependency tree is:

[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ grizzly-jersey-jsp-sample ---
[INFO] org.grizzly.samples:grizzly-jersey-jsp-sample:jar:1.0-SNAPSHOT
[INFO] +- org.glassfish.grizzly:grizzly-http-all:jar:2.3.2:compile
[INFO] |  +- org.glassfish.grizzly:grizzly-http-server-core:jar:2.3.2:compile
[INFO] |  |  +- org.glassfish.grizzly:grizzly-core:jar:2.3.2:compile
[INFO] |  |  |  \- org.glassfish.grizzly:grizzly-portunif:jar:2.3.2:compile
[INFO] |  |  +- org.glassfish.grizzly:grizzly-http-ajp:jar:2.3.2:compile
[INFO] |  |  \- org.glassfish.grizzly:grizzly-http-server-multipart:jar:2.3.2:compile
[INFO] |  +- org.glassfish.grizzly:grizzly-http-servlet:jar:2.3.2:compile
[INFO] |  +- org.glassfish.grizzly:grizzly-comet:jar:2.3.2:compile
[INFO] |  |  \- org.glassfish.grizzly:grizzly-framework:jar:2.3.2:compile
[INFO] |  +- org.glassfish.grizzly:grizzly-websockets:jar:2.3.2:compile
[INFO] |  \- javax.servlet:javax.servlet-api:jar:3.1-b05:compile
[INFO] +- com.sun.jersey:jersey-core:jar:1.17:compile
[INFO] +- com.sun.jersey:jersey-grizzly2:jar:1.17:compile
[INFO] |  +- org.glassfish.grizzly:grizzly-http:jar:2.2.16:compile
[INFO] |  +- org.glassfish.grizzly:grizzly-http-server:jar:2.2.16:compile
[INFO] |  |  \- org.glassfish.grizzly:grizzly-rcm:jar:2.2.16:compile
[INFO] |  \- com.sun.jersey:jersey-server:jar:1.17:compile
[INFO] |     \- asm:asm:jar:3.1:compile
[INFO] +- com.sun.jersey:jersey-servlet:jar:1.17:compile
[INFO] +- org.glassfish.web:javax.servlet.jsp:jar:2.2.5:compile
[INFO] |  \- javax.servlet.jsp:javax.servlet.jsp-api:jar:2.2.1:compile
[INFO] +- org.glassfish.web:javax.el:jar:2.2.1:compile
[INFO] |  \- javax.el:javax.el-api:jar:2.2.1:compile
[INFO] +- org.glassfish.web:javax.servlet.jsp.jstl:jar:1.2.2:compile
[INFO] |  \- javax.servlet.jsp.jstl:jstl-api:jar:1.2:compile
[INFO] |     +- javax.servlet:servlet-api:jar:2.5:compile
[INFO] |     \- javax.servlet.jsp:jsp-api:jar:2.1:compile
[INFO] \- junit:junit:jar:3.8.1:test

The complete sample code is available on github
https://github.com/oleksiys/samples/tree/master/grizzly-jersey-jsp-sample

Advertisements
This entry was posted in Uncategorized and tagged , , , . Bookmark the permalink.

4 Responses to Jersey JSP View running on Grizzly server

  1. Pingback: Using JSP with Grizzly 2.3 | Thoughts[Random.nextInt()]

  2. Sushma says:

    Nice tutorial, how can this be achieved for Jersey 2.0 or higer?

  3. Pingback: Jersey JSP View running on Grizzly server #2: Jersey 2 | MyTec

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s