servletsasynchronousservletconfig

Servlet url pattern - java.lang.NullPointerException in servlet Context


my servlet performs three tasks:

When it receives a message "resourceupdate" stores the requested resource (an HTML page) to an object sharedClient.

When it receives an asynchronous get (querystring indicated by "t = UPDATE") adds the object sharedClient this request pending.

When it receives a delivery_resource (indicated by pathinfo myServlet/sharedsessionID - eg. myServlet/8439jfndofsd93jcanjc) performs the dispatch of the resource.

this is my Web.xml:

<servlet>
    <description></description>
    <display-name>MyServlet</display-name>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>it.package.MyServlet.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/MyServlet/*</url-pattern>
</servlet-mapping>

I keep all the active SharedClients in this way:

ConcurrentHashMap<String, SharedClient> hmClients = (ConcurrentHashMap<String, SharedClient>)    this.getServletConfig().getServletContext().getAttribute("sharedClients");

And I get the SharedClient with a specific SharedSessionID in this way:

SharedClient targetClient = hmClients.get(ssid);

now the problem: until I do a resourceupdate servlet works fine, but when i do a delivery_resource (to a path of type myServlet/sharedsessionID) the servlet is unable to obtain the sharedClient reference, and I get a Null Pointer Exception.

if (resourceUri!=null) {
String ssid = resourceUri.substring(1);
ConcurrentHashMap<String, SharedClient> hmClients = (ConcurrentHashMap<String, SharedClient>) this.getServletConfig().getServletContext().getAttribute("sharedClients");
if (hmClients.containsKey(ssid)) {
    SharedClient targetClient = hmClients.get(ssid);
    if (targetClient.getParticipantSession().containsKey(session)) { 
    IoUtils.copy(targetClient.streamSnapshot(), response.getOutputStream());
        }
} else System.out.println("hmClient do not contains ssid"); //always in this branch

From this time also the long polling with asynchronous get are not completed successfully.

If i do not do a delivery_resource to a path of type myServlet/sharedsessionID after the update the asynchronous get continue to function properly.

Thanks to all.


Solution

  • ok, i resolved.

    i need to write

     @WebServlet(urlPatterns = {"/myServlet/*"}, asyncSupported=true)
    

    before myServlet declaration.