dev@grizzly.java.net

Re: Grizzly 2.2 ServletHandler changes

From: Ryan Lubke <ryan.lubke_at_oracle.com>
Date: Wed, 21 Dec 2011 12:32:44 -0800

On 12/21/11 11:47 AM, Andreas Kohn wrote:
> Hi,
>
> congratulations to your new release :)
>
> I tried upgrading from 2.1.7 to 2.2, and found that the following code
> now longer compiles:
>
> HttpServlet servlet = ...;
> ServletHandler handler = new ServletHandler();
> handler.setServletInstance(servlet);
>
>
> 1) ServletHandler#<init>() now needs a grizzly-specific
> ServletConfigImpl instance, which in turn requires a grizzly-specific
> WebappContext.
>
> Is there any specific reason for needing a ServletConfigImpl, rather
> than requiring any instance implementing javax.servlet.ServletConfig?
>
> 2) #setServletInstance() is no longer visible, is there any reason for
> that?
>
>
> Both of these changes were done as part of commit 5e5da223, "Initial cut
> of the http-servlet rework".
>
>
> I've now replaced my code with:
>
> private static class GrizzlyServletHandler extends ServletHandler {
> public GrizzlyServletHandler(Servlet s, WebappContext c) {
> super(new ServletConfigImpl(c) {});
> setServletInstance(s);
> }
> }
>
> ... later ...
> ServletHandler handler = new GrizzlyServletHandler(servlet, new
> WebappContext("my nice name"));
>
>
>
> Is this the "correct" way of creating a servlet handler in grizzly 2.2?
With the API changes, there should be no reason to directly create the
ServletHandler yourself.

The proper way to register a servlet now would be:

       WebappContext ctx = new WebappContext("Test", "/contextPath");

Then call ctx.addServlet(String, Servlet);

         final ServletRegistration reg = ctx.addServlet(name, new
HttpServlet() {

             @Override
             protected void doGet(
                     HttpServletRequest req, HttpServletResponse resp)
                     throws IOException {
                 LOGGER.log(Level.INFO, "{0} received request {1}", new
Object[]{alias, req.getRequestURI()});
                 resp.setStatus(HttpServletResponse.SC_OK);
                 resp.setHeader("Content-Type", header);
                 resp.setHeader("Path-Info", req.getPathInfo());
                 resp.setHeader("Request-Was", req.getRequestURI());
                 resp.setHeader("Servlet-Name", getServletName());
                 resp.getWriter().write(alias);
             }
         });

Then add the path mapping:

         reg.addMapping(alias);

Register the WebappContext with the HttpServer:

          ctx.deploy(httpServerInstance);
          httpServer.start();

You should then be able to access the servlet.


>
> Regards,
> --
> Andreas
>