In the first part it was described how to run JSP on Grizzly and Jersey 1.x. In this blog we will take the same sample and make it work on Jersey 2.x.
@Path("/")
@Produces(MediaType.TEXT_HTML)
public class IndexModel {
@GET
@Path("index")
public Viewable index(@Context HttpServletRequest request) {
request.setAttribute("obj", "IT Works");
System.out.println("/INDEX called");
return new Viewable("/index.jsp", null);
}
}
The 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...");
ResourceConfig rc = new ResourceConfig()
.register(JspMvcFeature.class)
.register(IndexModel.class);
// Create HttpServer and register dummy "not found" HttpHandler
HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(BASE_URI, rc);
WebappContext context = new WebappContext("WebappContext", JERSEY_SERVLET_CONTEXT_PATH);
// Initialize and register Jersey Servlet
FilterRegistration registration = context.addFilter("ServletContainer",
ServletContainer.class);
registration.setInitParameter("javax.ws.rs.Application",
MyApplication.class.getName());
registration.setInitParameter(JspProperties.TEMPLATES_BASE_PATH,
"/WEB-INF/jsp");
// configure Jersey to bypass non-Jersey requests (static resources and jsps)
registration.setInitParameter(ServletProperties.FILTER_STATIC_CONTENT_REGEX,
"(/(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();
}
}
The actual application looks like this:
public class MyApplication extends ResourceConfig {
public MyApplication() {
// Resources.
register(IndexModel.class);
// MVC.
register(JspMvcFeature.class);
}
}
Maven dependency tree is:
[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ grizzly-jersey2-jsp-sample --- [INFO] org.grizzly.samples:grizzly-jersey2-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] | | \- org.glassfish.grizzly:grizzly-http:jar:2.3.2:compile [INFO] | \- javax.servlet:javax.servlet-api:jar:3.1-b05:compile [INFO] +- org.glassfish.jersey.core:jersey-server:jar:2.3:compile [INFO] | +- org.glassfish.jersey.core:jersey-common:jar:2.3:compile [INFO] | | +- javax.annotation:javax.annotation-api:jar:1.2:compile [INFO] | | \- org.glassfish.hk2:osgi-resource-locator:jar:1.0.1:compile [INFO] | +- org.glassfish.jersey.core:jersey-client:jar:2.3:compile [INFO] | +- javax.ws.rs:javax.ws.rs-api:jar:2.0:compile [INFO] | +- com.google.guava:guava:jar:14.0.1:compile [INFO] | +- org.glassfish.hk2:hk2-api:jar:2.2.0-b14:compile [INFO] | | \- org.glassfish.hk2:hk2-utils:jar:2.2.0-b14:compile [INFO] | +- org.glassfish.hk2.external:javax.inject:jar:2.2.0-b14:compile [INFO] | +- org.glassfish.hk2:hk2-locator:jar:2.2.0-b14:compile [INFO] | | +- org.glassfish.hk2.external:asm-all-repackaged:jar:2.2.0-b14:compile [INFO] | | \- org.glassfish.hk2.external:cglib:jar:2.2.0-b14:compile [INFO] | \- javax.validation:validation-api:jar:1.1.0.Final:compile [INFO] +- org.glassfish.jersey.containers:jersey-container-grizzly2-http:jar:2.3:compile [INFO] | \- org.glassfish.grizzly:grizzly-http-server:jar:2.3.3:compile [INFO] | \- org.glassfish.grizzly:grizzly-rcm:jar:2.3.3:compile [INFO] +- org.glassfish.jersey.containers:jersey-container-grizzly2-servlet:jar:2.3:compile [INFO] | +- javax.servlet:servlet-api:jar:2.4:compile [INFO] | \- org.glassfish.jersey.containers:jersey-container-servlet:jar:2.3:compile [INFO] +- org.glassfish.jersey.ext:jersey-mvc-jsp:jar:2.3:compile [INFO] | +- org.glassfish.jersey.containers:jersey-container-servlet-core:jar:2.3:compile [INFO] | \- org.glassfish.jersey.ext:jersey-mvc:jar:2.3: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.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-jersey2-jsp-sample