On 12/21/2011 09:32 PM, Ryan Lubke wrote:
> 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.
Just wanted to add that our motivation was making API similar to how
Servlets are defined within webapplication.
1) It became easier to define several servlets, which share the same
ServletContext (like webapplication)
2) Now Filters are getting associated with ServletContext
(webapplication), not specific ServletHandler.
Please let us know if we've missed some usecase.
Thanks.
WBR,
Alexey.
>
>
>>
>> Regards,
>> --
>> Andreas
>>
>