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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:12:31+00:00 2026-06-10T13:12:31+00:00

In the following snippet: ServletContext context = request.getServletContext(); String path = context.getRealPath(/); What does

  • 0

In the following snippet:

ServletContext context = request.getServletContext();
String path = context.getRealPath("/");

What does / in the method getRealPath() represent? When should I use it?

  • 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-10T13:12:32+00:00Added an answer on June 10, 2026 at 1:12 pm

    Introduction

    The ServletContext#getRealPath() is intented to convert a web content path (the path in the expanded WAR folder structure on the server’s disk file system) to an absolute disk file system path.

    The "/" represents the web content root. I.e. it represents the web folder as in the below project structure:

    YourWebProject
     |-- src
     |    :
     |
     |-- web
     |    |-- META-INF
     |    |    `-- MANIFEST.MF
     |    |-- WEB-INF
     |    |    `-- web.xml
     |    |-- index.jsp
     |    `-- login.jsp
     :    
    

    So, passing the "/" to getRealPath() would return you the absolute disk file system path of the /web folder of the expanded WAR file of the project. Something like /path/to/server/work/folder/some.war/ which you should be able to further use in File or FileInputStream.

    Note that most starters don’t seem to see/realize that you can actually pass the whole web content path to it and that they often use

    String absolutePathToIndexJSP = servletContext.getRealPath("/") + "index.jsp"; // Wrong!
    

    or even

    String absolutePathToIndexJSP = servletContext.getRealPath("") + "index.jsp"; // Wronger!
    

    instead of

    String absolutePathToIndexJSP = servletContext.getRealPath("/index.jsp"); // Right!
    

    Don’t ever write files in there

    Also note that even though you can write new files into it using FileOutputStream, all changes (e.g. new files or edited files) will get lost whenever the WAR is redeployed; with the simple reason that all those changes are not contained in the original WAR file. So all starters who are attempting to save uploaded files in there are doing it wrong.

    Moreover, getRealPath() will always return null or a completely unexpected path when the server isn’t configured to expand the WAR file into the disk file system, but instead into e.g. memory as a virtual file system.

    getRealPath() is unportable; you’d better never use it

    Use getRealPath() carefully. There are actually no sensible real world use cases for it. Based on my 20 years of Java EE experience, there has always been another way which is much better and more portable than getRealPath().

    If all you actually need is to get an InputStream of the web resource, better use ServletContext#getResourceAsStream() instead, this will work regardless of the way how the WAR is expanded. So, if you for example want an InputStream of index.jsp, then do not do:

    InputStream input = new FileInputStream(servletContext.getRealPath("/index.jsp")); // Wrong!
    

    But instead do:

    InputStream input = servletContext.getResourceAsStream("/index.jsp"); // Right!
    

    Or if you intend to obtain a list of all available web resource paths, use ServletContext#getResourcePaths() instead.

    Set<String> resourcePaths = servletContext.getResourcePaths("/");
    

    You can obtain an individual resource as URL via ServletContext#getResource(). This will return null when the resource does not exist.

    URL resource = servletContext.getResource(path);
    

    Or if you intend to save an uploaded file, or create a temporary file, then see the below “See also” links.

    See also:

    • getResourceAsStream() vs FileInputStream
    • Recommended way to save uploaded files in a servlet application
    • Simple ways to keep data on redeployment of Java EE 7 web application
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wonder why the following snippet does not give the expected result? It sorts
I have the following snippet which i use to load xml via jQuery from
The following snippet does work for what I need. I believe though that there
I have the following snippet that loads the images: String imgName = /assets/ +
I have the following snippet of PowerShell script in my profile: . (join-path (split-path
The following snippet of C# code: int i = 1; string result = String.Format({0},{1},{2},
In the following snippet: public class a { public void otherMethod(){} public void doStuff(String
I'm using following snippet for saving content: private void writeToFile(NodeRef nodeRef, String content) throws
Consider the following snippet (error handling missing on purpose): void* foo(const char *path, off_t
Following snippet attempts to write the name of directories and files present in some

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.