org.glassfish.grizzly.servlet
Class ServletHandler

java.lang.Object
  extended by org.glassfish.grizzly.http.server.HttpHandler
      extended by org.glassfish.grizzly.servlet.ServletHandler

public class ServletHandler
extends HttpHandler

HttpHandler class that can initiate a FilterChain and execute its Filter and its Servlet Configuring a HttpServer to use this HttpHandler implementation, adds the ability of servicing Servlet as well as static resources. This class can be used to programatically configure a Servlet, Filters, listeners, init parameters, context-param, etc. a application usually defined using the web.xml. See addInitParameter(String, String) addContextParameter(String, String) setProperty(String, Object), addServletListener(String), etc. As an example:


 HttpServer hs = HttpServer.createSimpleServer("/var/www");
 try {
     ServletHandler sa = new ServletHandler();
     sa.setServlet(new MyServlet());

     // Set the display-name property
     sa.setProperty("display-name", "myServlet");

     // All Servlet listener types are added via the
     // addServletListener() method regardless of their type...
     sa.addServletListener(MyHttpSessionListener.class.getName());
     sa.addServletListener(MyOtherHttpSessionListener.class.getName());
     sa.addServletListener(FooServletContextListener.class.getName());
     sa.assServletListener(BarServletCtxAttListener.class.getName());

     // adding context initialization parameter...
     sa.addContextParameter("databaseURI","jdbc://");

     // adding servlet initialization parameter...
     sa.addInitParameter("password","hello");

     // adding servlet path information...
     sa.setServletPath("/MyServletPath");
     sa.setContextPath("/myApp");

     // register the handler with the server...
     hs.getServerConfiguration().addHttpHandler(sa, "/MyServletPath");

     // start the server so the handler is able to serve requests...
     hs.start();
 } catch (IOException ex){
     // Something when wrong.
 }
 

Author:
Jeanfrancois Arcand

Field Summary
protected  java.lang.ClassLoader classLoader
           
static int INCREMENT
           
protected  boolean initialize
          Initialize the ServletContext
static java.lang.String LOAD_ON_STARTUP
           
protected  java.util.Map<java.lang.String,java.lang.Object> properties
          Holder for our configured properties.
protected  javax.servlet.Servlet servletInstance
           
 
Constructor Summary
  ServletHandler()
           
  ServletHandler(javax.servlet.Servlet servlet)
          Create a ServletAdapter which support the specific Servlet
  ServletHandler(javax.servlet.Servlet servlet, ServletContextImpl servletContext)
           
  ServletHandler(java.lang.String servletName)
           
protected ServletHandler(java.lang.String servletName, ServletContextImpl servletCtx, java.util.Map<java.lang.String,java.lang.String> contextParameters, java.util.Map<java.lang.String,java.lang.String> servletInitParameters, boolean initialize)
          Convenience constructor.
protected ServletHandler(java.lang.String servletName, ServletContextImpl servletCtx, java.util.Map<java.lang.String,java.lang.String> contextParameters, java.util.Map<java.lang.String,java.lang.String> servletInitParameters, java.util.List<java.lang.String> listeners)
          Convenience constructor.
protected ServletHandler(java.lang.String servletName, ServletContextImpl servletCtx, java.util.Map<java.lang.String,java.lang.String> contextParameters, java.util.Map<java.lang.String,java.lang.String> servletInitParameters, java.util.List<java.lang.String> listeners, boolean initialize)
          Convenience constructor.
 
Method Summary
 void addContextParameter(java.lang.String name, java.lang.String value)
          Add a new servlet context parameter for this servlet.
protected  void addFilter(FilterConfigImpl filterConfig)
          Add a filter to the set of filters that will be executed in this chain.
 void addFilter(javax.servlet.Filter filter, java.lang.String filterName, java.util.Map initParameters)
          Add a Filter to the FilterChain.
 void addInitParameter(java.lang.String name, java.lang.String value)
          Add a new servlet initialization parameter for this servlet.
 void addServletListener(java.lang.String listenerName)
          Add Servlet listeners that implement EventListener
protected  void configureClassLoader(java.lang.String applicationPath)
          Create a URLClassLoader which has the capability of loading classes jar under an exploded war application.
protected  void configureServletEnv()
          Configure the ServletContextImpl and ServletConfigImpl
 boolean containsInitParameter(java.lang.String name)
          if the servlet initialization parameter in present for this servlet.
 void customizeErrorPage(Response response, java.lang.String message, int errorCode)
          Customize the error page returned to the client.
 void destroy()
          Destroy this Servlet and its associated ServletContextListener
protected  void doServletService(Request request, Response response)
           
 java.lang.ClassLoader getClassLoader()
           
protected  java.util.Map<java.lang.String,java.lang.String> getContextParameters()
           
 java.lang.String getContextPath()
          Returns the portion of the request URI that indicates the context of the request.
 java.lang.String getInitParameter(java.lang.String name)
          get a servlet initialization parameter for this servlet.
protected  java.util.List<java.lang.String> getListeners()
           
 java.lang.String getName()
           
 java.lang.Object getProperty(java.lang.String name)
          Return a configured property.
protected  ServletContextImpl getServletCtx()
           
 javax.servlet.Servlet getServletInstance()
          Return the Servlet instance used by this ServletHandler
 java.lang.String getServletPath()
          Returns the part of this request's URL that calls the servlet.
 boolean isLoadOnStartup()
           
protected  void loadServlet()
          Load a Servlet instance.
 ServletHandler newServletHandler(javax.servlet.Servlet servlet)
          Create a new ServletHandler instance that will share the same ServletContextImpl and Servlet's listener but with an empty map of init-parameters.
 void removeInitParameter(java.lang.String name)
          Remove a servlet initialization parameter for this servlet.
 void removeProperty(java.lang.String name)
          Remove a configured property.
 boolean removeServletListener(java.lang.String listenerName)
          Remove Servlet listeners that implement EventListener
 void service(Request request, Response response)
          
 void setClassLoader(java.lang.ClassLoader classLoader)
           
 void setContextPath(java.lang.String contextPath)
          Programmatically set the context path of the Servlet.
protected  void setDispatcherHelper(DispatcherHelper dispatcherHelper)
           
 void setProperty(java.lang.String name, java.lang.Object value)
          Set a configured property.
 void setServletInstance(javax.servlet.Servlet servletInstance)
          Set the Servlet instance used by this ServletHandler
 void setServletPath(java.lang.String servletPath)
          Programmatically set the servlet path of the Servlet.
 void start()
          
 
Methods inherited from class org.glassfish.grizzly.http.server.HttpHandler
customizedErrorPage, doHandle, getRequestURIEncoding, isAllowCustomStatusMessage, isAllowEncodedSlash, sendAcknowledgment, setAllowCustomStatusMessage, setAllowEncodedSlash, setDecodeUrl, setRequestURIEncoding, setRequestURIEncoding, updateContextPath
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOAD_ON_STARTUP

public static final java.lang.String LOAD_ON_STARTUP
See Also:
Constant Field Values

servletInstance

protected volatile javax.servlet.Servlet servletInstance

properties

protected final java.util.Map<java.lang.String,java.lang.Object> properties
Holder for our configured properties.


initialize

protected boolean initialize
Initialize the ServletContext


classLoader

protected java.lang.ClassLoader classLoader

INCREMENT

public static final int INCREMENT
See Also:
Constant Field Values
Constructor Detail

ServletHandler

public ServletHandler()

ServletHandler

public ServletHandler(java.lang.String servletName)

ServletHandler

public ServletHandler(javax.servlet.Servlet servlet)
Create a ServletAdapter which support the specific Servlet

Parameters:
servlet - Instance to be used by this adapter.

ServletHandler

protected ServletHandler(java.lang.String servletName,
                         ServletContextImpl servletCtx,
                         java.util.Map<java.lang.String,java.lang.String> contextParameters,
                         java.util.Map<java.lang.String,java.lang.String> servletInitParameters,
                         java.util.List<java.lang.String> listeners)
Convenience constructor.

Parameters:
servletCtx - ServletContextImpl to be used by new instance.
contextParameters - Context parameters.
servletInitParameters - servlet initialization parameters.
listeners - Listeners.

ServletHandler

protected ServletHandler(java.lang.String servletName,
                         ServletContextImpl servletCtx,
                         java.util.Map<java.lang.String,java.lang.String> contextParameters,
                         java.util.Map<java.lang.String,java.lang.String> servletInitParameters,
                         java.util.List<java.lang.String> listeners,
                         boolean initialize)
Convenience constructor.

Parameters:
servletCtx - ServletContextImpl to be used by new instance.
contextParameters - Context parameters.
servletInitParameters - servlet initialization parameters.
listeners - Listeners.
initialize - false only when the newServletHandler(javax.servlet.Servlet) is invoked.

ServletHandler

protected ServletHandler(java.lang.String servletName,
                         ServletContextImpl servletCtx,
                         java.util.Map<java.lang.String,java.lang.String> contextParameters,
                         java.util.Map<java.lang.String,java.lang.String> servletInitParameters,
                         boolean initialize)
Convenience constructor.

Parameters:
servletCtx - ServletContextImpl to be used by new instance.
contextParameters - Context parameters.
servletInitParameters - servlet initialization parameters.
initialize - false only when the newServletHandler(javax.servlet.Servlet) is invoked.

ServletHandler

public ServletHandler(javax.servlet.Servlet servlet,
                      ServletContextImpl servletContext)
Method Detail

start

public void start()

Overrides:
start in class HttpHandler

configureClassLoader

protected void configureClassLoader(java.lang.String applicationPath)
                             throws java.io.IOException
Create a URLClassLoader which has the capability of loading classes jar under an exploded war application.

Parameters:
applicationPath - Application class path.
Throws:
java.io.IOException - I/O error.

service

public void service(Request request,
                    Response response)

Specified by:
service in class HttpHandler

doServletService

protected void doServletService(Request request,
                                Response response)

customizeErrorPage

public void customizeErrorPage(Response response,
                               java.lang.String message,
                               int errorCode)
Customize the error page returned to the client.

Parameters:
response - the Response
message - the HTTP error message
errorCode - the error code.

loadServlet

protected void loadServlet()
                    throws javax.servlet.ServletException
Load a Servlet instance.

Throws:
javax.servlet.ServletException - If failed to Servlet.init(javax.servlet.ServletConfig).

configureServletEnv

protected void configureServletEnv()
                            throws javax.servlet.ServletException
Configure the ServletContextImpl and ServletConfigImpl

Throws:
javax.servlet.ServletException - Error while configuring Servlet.

addInitParameter

public void addInitParameter(java.lang.String name,
                             java.lang.String value)
Add a new servlet initialization parameter for this servlet.

Parameters:
name - Name of this initialization parameter to add
value - Value of this initialization parameter to add

removeInitParameter

public void removeInitParameter(java.lang.String name)
Remove a servlet initialization parameter for this servlet.

Parameters:
name - Name of this initialization parameter to remove

getInitParameter

public java.lang.String getInitParameter(java.lang.String name)
get a servlet initialization parameter for this servlet.

Parameters:
name - Name of this initialization parameter to retrieve

containsInitParameter

public boolean containsInitParameter(java.lang.String name)
if the servlet initialization parameter in present for this servlet.

Parameters:
name - Name of this initialization parameter

addContextParameter

public void addContextParameter(java.lang.String name,
                                java.lang.String value)
Add a new servlet context parameter for this servlet.

Parameters:
name - Name of this initialization parameter to add
value - Value of this initialization parameter to add

addFilter

public void addFilter(javax.servlet.Filter filter,
                      java.lang.String filterName,
                      java.util.Map initParameters)
Add a Filter to the FilterChain.

Parameters:
filter - an instance of Filter
filterName - the Filter's name
initParameters - the Filter init parameters.

getServletInstance

public javax.servlet.Servlet getServletInstance()
Return the Servlet instance used by this ServletHandler

Returns:
Servlet instance.

setServletInstance

public void setServletInstance(javax.servlet.Servlet servletInstance)
Set the Servlet instance used by this ServletHandler

Parameters:
servletInstance - an instance of Servlet.

getServletPath

public java.lang.String getServletPath()
Returns the part of this request's URL that calls the servlet. This path starts with a "/" character and includes either the servlet name or a path to the servlet, but does not include any extra path information or a query string. Same as the value of the CGI variable SCRIPT_NAME.

This method will return an empty string ("") if the servlet used to process this request was matched using the "/*" pattern.

Returns:
a String containing the name or path of the servlet being called, as specified in the request URL, decoded, or an empty string if the servlet used to process the request is matched using the "/*" pattern.

setServletPath

public void setServletPath(java.lang.String servletPath)
Programmatically set the servlet path of the Servlet.

Parameters:
servletPath - Path of Servlet.

getContextPath

public java.lang.String getContextPath()
Returns the portion of the request URI that indicates the context of the request. The context path always comes first in a request URI. The path starts with a "/" character but does not end with a "/" character. For servlets in the default (root) context, this method returns "". The container does not decode this string.

It is possible that a servlet container may match a context by more than one context path. In such cases this method will return the actual context path used by the request and it may differ from the path returned by the ServletContext.getContextPath() method. The context path returned by ServletContext.getContextPath() should be considered as the prime or preferred context path of the application.

Returns:
a String specifying the portion of the request URI that indicates the context of the request
See Also:
ServletContext.getContextPath()

setContextPath

public void setContextPath(java.lang.String contextPath)
Programmatically set the context path of the Servlet.

Parameters:
contextPath - Context path.

addServletListener

public void addServletListener(java.lang.String listenerName)
Add Servlet listeners that implement EventListener

Parameters:
listenerName - name of a Servlet listener

removeServletListener

public boolean removeServletListener(java.lang.String listenerName)
Remove Servlet listeners that implement EventListener

Parameters:
listenerName - name of a Servlet listener to remove

getProperty

public java.lang.Object getProperty(java.lang.String name)
Return a configured property. Property apply to ServletContextImpl and ServletConfigImpl

Parameters:
name - Name of property to get.
Returns:
Value of property.

setProperty

public void setProperty(java.lang.String name,
                        java.lang.Object value)
Set a configured property. Property apply to ServletContextImpl and ServletConfigImpl. Use this method to map what's you usually have in a web.xml like display-name, context-param, etc.

Parameters:
name - Name of the property to set
value - of the property.

removeProperty

public void removeProperty(java.lang.String name)
Remove a configured property. Property apply to ServletContextImpl and ServletConfigImpl

Parameters:
name - Property name to remove.

isLoadOnStartup

public boolean isLoadOnStartup()
Returns:
is the servlet will be loaded at startup

destroy

public void destroy()
Destroy this Servlet and its associated ServletContextListener

Overrides:
destroy in class HttpHandler

newServletHandler

public ServletHandler newServletHandler(javax.servlet.Servlet servlet)
Create a new ServletHandler instance that will share the same ServletContextImpl and Servlet's listener but with an empty map of init-parameters.

Parameters:
servlet - - The Servlet associated with the ServletHandler
Returns:
a new ServletHandler

getServletCtx

protected ServletContextImpl getServletCtx()

getListeners

protected java.util.List<java.lang.String> getListeners()

getContextParameters

protected java.util.Map<java.lang.String,java.lang.String> getContextParameters()

getClassLoader

public java.lang.ClassLoader getClassLoader()

setClassLoader

public void setClassLoader(java.lang.ClassLoader classLoader)

getName

public java.lang.String getName()
Overrides:
getName in class HttpHandler

addFilter

protected void addFilter(FilterConfigImpl filterConfig)
Add a filter to the set of filters that will be executed in this chain.

Parameters:
filterConfig - The FilterConfig for the servlet to be executed

setDispatcherHelper

protected void setDispatcherHelper(DispatcherHelper dispatcherHelper)
Overrides:
setDispatcherHelper in class HttpHandler


Copyright © 2011 Oracle Corpration. All Rights Reserved.