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

  • Home
  • SEARCH
  • 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 6724617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:42:39+00:00 2026-05-26T09:42:39+00:00

I have two servlets each in a separate web app running in the same

  • 0

I have two servlets each in a separate web app running in the same container (Tomcat). Let the apps be app1 & app2, & servlets be serv1 & serv2.

I’m using serv1 (in app1) to call serv2(in app2).Im also trying to share a same session across these two servlets. Below is my code fragment.

serv1 (app1):

URLConnection connection = new URL("http://localhost:8080/app2/serv2").openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));  
HttpSession session = request.getSession(true);  
String sessionId = session.getId();  
ServletContext myContext = getServletContext();
myContext.setAttribute("MYSHAREDSESSIONID", sessionId);
myContext.setAttribute("SHAREDSESSION", session);

serv2( app2):

ServletContext callingContext = getServletContext().getContext("/app1");  
String jsessionId = (String)callingContext .getAttribute("MYSHAREDSESSIONID");

server.xml

<Host>
 <Context path="/app1" crossContext="true" />
 <Context path="/app2" crossContext="true" />
</Host>

The problem is Im getting null pointer exception in serv2 getAttribute(). The root cause is, the getContext("/app1") is returning null. The context names are correct. I don’t know why Im not able to retrieve the servlet context. I don’t want to use cookies or url rewriting. Please help.

  • 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-26T09:42:40+00:00Added an answer on May 26, 2026 at 9:42 am

    It looks fine so far. Apparently it’s been overridden by a context.xml maintained/generated elsewhere. You can read in the Tomcat’s Context configuration reference about the rules. Do you for example have one in webapp’s /META-INF? You should then define crossContext in there.

    Or are you using an IDE like Eclipse to deploy the webapps or something? The IDE has then to be configured to not deploy into workspace metadata, but straight into Tomcat, otherwise Eclipse will use its own copy of context.xml (which you can find in Servers project). You can configure it in the server’s preferences. Here’s a screen from Eclipse:

    enter image description here

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

Sidebar

Related Questions

I have two web applications running in same Servlet container ( Tomcat ), A
I have two servlets which are running on different tomcat servers. I and trying
I have two servlets A & B. On B i intend to have a
I am using Tomcat 7 as a servlet container and I have two war
I have a web application which has two servlets that exchange information through context
The set up I have websphere 6.1 running two application servers. Each running the
I have two applications written in Java that communicate with each other using XML
I have two threads, one needs to poll a bunch of separate static resources
Let's say I have two PDF templates created with Adobe Acrobat, which are both
I've got a situation where I have two different webapps running on a single

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.