users@jersey.java.net

Embedded Jetty, with Jersey and JSP

From: Jared O'Connor <JOConnor_at_internode.com.au>
Date: Wed, 27 Oct 2010 16:29:06 +1030

Hi

I'm currently developing a system containing embedded Jetty and a Jersey servlet. I'm using Maven and the project is to be packaged as a JAR. All my JSP files are sitting in the /resources/jsp/ folder (not /webapp/). Everything is working great, with the exception of JSPs.

My server is created with the following code.

ServletHolder holder = new ServletHolder(ServletContainer.class);
holder.setInitParameter("com.sun.jersey.config.property.resourceConfigClass",
        "com.sun.jersey.api.core.PackagesResourceConfig");
holder.setInitParameter("com.sun.jersey.config.property.packages", "my.package.here");

String jspDirectory = Application.class.getClassLoader().getResource("jsp/").toExternalForm();
holder.setInitParameter("com.sun.jersey.config.property.JSPTemplatesBasePath", jspDirectory);

Server server = new Server(8080);
Context context = new Context(server, "/", Context.SESSIONS);
context.addServlet(holder, "/*");
server.start();

I then have various places I would like to pull up a JSP, as below.

@GET
@Produces("text/html")
public Viewable getHtml()
{
        return(new Viewable("/index", model));
}

I've tried playing around with TemplateProcessor, ViewProcessor and a few of the servlet context methods, but I cannot get JSPs to work. Below is the error I receive.

java.io.IOException: The template name, /index, could not be resolved to a fully qualified template name
        at com.sun.jersey.server.impl.template.ViewableMessageBodyWriter.writeTo(ViewableMessageBodyWriter.java:79)
...

If anyone is able to provide some guidance in this matter, it would be greatly appreciated.

Kind regards