users@grizzly.java.net

Re: Grizzly with static content and servlets

From: Ryan Lubke <ryan.lubke_at_oracle.com>
Date: Fri, 15 Jul 2011 15:23:33 -0700

Upon reviewing the code, it appears that as soon as an adapter is added
to the GWS, then the static resource serving functionality is disabled.
This change was a part of GRIZZLY-614 [1]. I need to dig around a bit
as the issue wasn't 100% clear to me.

At any rate, you can work around this by doing this:

--------------------------------------------------

try {
             GrizzlyWebServer gws = new GrizzlyWebServer(8080);
             ServletAdapter sa = new ServletAdapter();
             sa.setHandleStaticResources(true);
             sa.setContextPath("/main");

             sa.setServletInstance(new MyServlet());

             gws.addGrizzlyAdapter(sa, new String[]{"/main"});
             gws.addGrizzlyAdapter(new GrizzlyAdapter() {

                 {
                     setHandleStaticResources(true);
                     addRootFolder("/tmp");
                 }

                 @Override
                 public void service(GrizzlyRequest grizzlyRequest,
GrizzlyResponse grizzlyResponse) throws Exception {
                     // no-op
                 }
             });
             gws.start();
         } catch (IOException ex) {
             System.out.println("ERROR: Problem loading Webserver");
             System.out.println(ex.getMessage());
             System.exit(1);
         }
---------------------------------------------------------

Testing this, you should be able to have both static content and Servlet
content served.




[1] http://java.net/jira/browse/GRIZZLY-614

On 7/15/11 11:10 AM, Hiran Patel wrote:
> *Folks,
>
> I am trying to follow the example here:
> http://grizzly.java.net/nonav/docs/1.9/apidocs/com/sun/grizzly/http/embed/GrizzlyWebServer.html
> using Grizzly 1.9.36 (JDK 5)
>
> *It seems Grizzly wipes out the original static adapter when the
> servlet adapter is added. If I include the following lines of code (
> ServletAdapter ServletAdapter = ...) the static content is no longer
> served. Instead it gives me a "Resource not Found".
>
> GrizzlyWebServer gws = new GrizzlyWebServer(8080, "/var/www");|
>
> try {
> ServletAdapter sa = new ServletAdapter();
> // Fails too: ServletAdapter sa = new ServletAdapter(|"/var/www"|);
> sa.setHandleStaticResources(true);
> // Fails too: sa.addRootFolder("|/var/www"|);
> sa.setContextPath("/main");
>
> Servlet servlet = (Servlet)ClassLoaderUtil.load("com.my.MainServlet");
> sa.setServletInstance(servlet);
>
> System.out.println("Root is:"+sa.getRootFolders());
>
> ws.addGrizzlyAdapter(sa, new String[]{"/main"});
> gws.start();
> } catch (IOException ex) {
> System.out.println("ERROR: Problem loading Webserver");
> System.out.println(ex.getMessage());
> System.exit(1);
> }
>
>
> |I realy don't know how to proceed...
> |
> Thanks in advance,
> Hiran
> |
>