Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7566859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:31:07+00:00 2026-05-30T14:31:07+00:00

I am trying to write a simple program where i can put a HashMap

  • 0

I am trying to write a simple program where i can put a HashMap on an application scoped session and get two applications/contexts deployed as two war files access the HashMap.

Servlet 1

public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException{
        PrintWriter out = response.getWriter();

        HashMap<String ,String> hm = new HashMap<String, String>();
        hm.put("1", "1");

        this.getServletContext().setAttribute("usermanager", hm);
        this.getServletConfig().getServletContext().setAttribute("usermanager2", hm);   
    }

Servlet 2

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException{

    HashMap newMap2 = (HashMap) this.getServletConfig().getServletContext().getAttribute("usermanager"); 
    HashMap newMap3 = (HashMap) this.getServletContext().getAttribute("usermanager2"); 

    System.out.println("newmap2 size " + newMap2.size());
    System.out.println("newmap3 size " + newMap3.size());   
}

To test this i restarted Tomcat 6 then run access Servlet 1 first so that it initializes the Hashmap object. When i subsequently access Servlet 2 i get a NULL-POINTER error which is pointing at the line number where i tried to call newMap2.size()

What am i doing wrong?

java.lang.NullPointerException
    at com.TestServlet.doGet(TestServlet.java:24)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:291)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:619)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:31:08+00:00Added an answer on May 30, 2026 at 2:31 pm

    As @Peter Cetinski already mentioned a Servlet Context always relates to exactly one web application (WAR file). Nevertheless, the Servlet API provides a way to access ‘foreign’ Servlet Contexts by calling javax.servlet.ServletContext.getContext(uriPath).

    So if you really want/need to spread your Servlets across different WARs and communication between them is required, you can do this.

    Assuming that Servlet 1 is located in web application warA, Servlet 2 located in web application warB, and the applications context path is the same as the web application name (e.g. http://myserver/warA and http://myserver/warB), the code for Servlet 2 could look like this:

    /* retrieve foreign context */
    ServletContext ctxWarA = this.getServletContext().getContext("warA");
    
    /* ... and work with it */
    Map<String, String> map = (Map<String, String>) ctxWarA.getAttribute("usermanager");
    

    There are some constraints:

    • you must explicitly allow warB to access the foreign context by specifying this in the applications’ context.xml: <Context crossContext="true"/>

    • this setup is proprietary (I cannot tell you how this works in other application servers/servlet engines)

    • if you want to share custom classes they must be shared between the applications (place them in <tomcat>/lib

    • in the standard setup you work with different classloaders for warA and warB, you should be aware of that

    • Tomcat does not guarantee a certain startup sequence (there is no way to configure this). In your case warB must be able to handle this and expect warA to be not yet started

    If you need to share objects/information between web applications, I’d consider those alternative approaches:

    • a shared singleton located in Tomcat/lib (I know a singleton, but it works)

    • an object located in the JNDI tree (Tomcat documentation)

    • information required to build the cache in a shared data store (file, database, etc.) and each applications loads the data at startup

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a simple program in VC++ which will just initialize the
I am trying to write a simple program to open a socket channel to
I'm just starting out writing trying to write a simple program in C and
I'm trying to write a simple curl program to retrieve the web page in
I'm trying to write a very simple program, I want to print out the
I'm trying to write a simple stock check program, and I have a Table
I'm trying to write a very simple image processing program for fun and practice.
Today I'm trying the xtend to write a simple program. Looks good, but it
I'm trying to write a simple program to calculate betweeness using brandes_betweenness_centrality from boostlib.
I am trying to write a simple program that lets me overlay a dot

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.