0

I have a Webservice using jersey. I want to access a hashmap which is created in a servlet from rest service. That hashmap in servlet sholud be created only once during application startup. this is web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>GrpService_Map</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
<servlet>
    <servlet-name>jersey-serlvet</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
      <param-value>com.ericsson.mapService</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>jersey-serlvet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

Service class TestSErvice is within com.ericsson.mapService package .My question is where will I place the required servlet that creates the hashmap and how will I access that map from TestSErvice ? This is my service class TestService.java

@Path("/Service")
public class TestService {
    @GET
    @Consumes("application/xml")
    @Produces(MediaType.TEXT_PLAIN)
    @Path("/cacheserviceTestMap")
    public String createGroupCache(@Context HttpServletRequest request)
            throws IOException {

        //here i want to access the IndexMap hashmap
        return "Success";
    }
}

This is my servlet class

public class TestSerlvet extends HttpServlet{
     protected void doGet(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException 
        {
Map<String, Map<String,List<String>>> IndexMap = new HashMap<String, Map<String,List<String>>>();
        Map<String,List<String>> signumMap = new HashMap<String,List<String>>();
        List<String> grp = new ArrayList<String>();
        grp.add("1");
        grp.add("2");
        signumMap.put("eab", grp);
        signumMap.put("eabc",grp);
        signumMap.put("exy",grp);
        IndexMap.put("I", signumMap);
}
}

Thank you

Ipsita Tah
  • 237
  • 2
  • 4
  • 11

1 Answers1

0

The servlet should be initialized (and mapped) in web.xml with load-on-startup set to a lower value than that of jersey-servlet in order for it to be loaded during container startup before any REST call can be serviced. The servlet's doGet method's IndexMap local variable should be converted into a static field of the servlet which can be publicly accessed. The initialization of that field should occur in the servlet's init() method in order to ensure that the HashMap is initialized before any REST endpoint can access it.

Once the above is done, TestService.createGroupCache() should be able to read the already initialized HashMap by calling the static getter method of the servlet.

Mark A. Fitzgerald
  • 1,249
  • 1
  • 9
  • 20