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 9231147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:59:55+00:00 2026-06-18T05:59:55+00:00

At many places in my web application I need a certain String which is

  • 0

At many places in my web application I need a certain String which is :

request.getServletContext().getRealPath("/"); 
// I can only get this once the web-app starts

Sometimes a simple java class needs to know this string. I don’t want to pass each and every time this string to the class’s function. One way could be to stash this string in a file at the very beginning of the web application. Every time I would need this, simply read the file.But this doesn’t seem a pretty good option. Is there any other way out ?

May be I can store in the context.xml of the application. If it is possible how do I do that ?

If there is a better way please suggest.

I am using tomcat as the server.

  • 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-06-18T05:59:57+00:00Added an answer on June 18, 2026 at 5:59 am

    Invoke run the following code when your application starts (e.g. with a ServletContextListener):

    public void contextInitialized(ServletContextEvent sce) {
       ServletContextRootRealPath.set(sce.getServletContext().getRealPath("/"));
    }
    

    Then later whenever you need it you simply call

    ServletContextRootRealPath.get()
    

    Since it’s all static the variable is accessible JVM-wide to every code that runs in this JVM.

    Utility class

    public final class ServletContextRootRealPath {
      private static String path;
    
      private ServletContextRootRealPath() {
        // don't instantiate utility classes
      }
    
      public static void set(String rootRealPath) {
        path = rootRealPath;
      }
    
      public static String get() {
        return path;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In many places in our application we have code like this: using(RAPI rapi =
There are many places in my application where I need to generate links with
Many stackoverflow questions link to http://code.google.com/p/google-web-toolkit/wiki/RequestFactoryInterfaceValidation#IDE_configuration which shows a place to set Annotation Processing
I have used the conditional operator in many places within my Java application. Now
I'm working on a ASP.NET MVC3 web application (not written by me) which has
In many places in msdn documentation you can find references to App_Data directory. For
I've been creating this pretty large web application for sometime and have been updating
Good day, I'm developing a simple Java EE web application in NetBeans 7.2.1 which
Situation I have web application I have class which does complicated mathematics computation Equations
We are an ISV with a web application. Unlike StackExchange or Trello, this web

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.