context path in servlet

Posted on 2008-11-18
Last Modified: 2013-11-24
I have a file located in Tomcat/webapps/myapp/custom/xsl/abc.xsl

How can I get the context path, so that I just append custom/xsl/abc.xsl to it and get to the file.
Question by:angela_smith
    LVL 20

    Expert Comment

    by:chaitu chaitu
    LVL 4

    Accepted Solution

    request.getContextPath() will return you the URI context of a request (that is, the URI realtive to the server). In your case, that would equate to "/custom/xsl" assuming that your myapp is configured to be at the context root ("/").

    If you're after the actual path to a file in the machine filesystem, you can use the getRealPath function in the ServletContext (see )

    In your case, inside your servlet call getServletContext().getRealPath("/custom/xsl/abc.xsl");

    There are some warnings attached to using this method (as you can see in the javadoc). For instance, of you package your webbapp in a WAR archive, getRealPath won't work.

    A more "proper" way to go about this is to use the getResourceAsStream function (see ). This will give you an InputStream from which you can read the content of any file inside your webapp.

    //Inside your servlet
    InputStream is = getServletContext().getResourceAsStream("/custom/xsl/abc.xsl");

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Suggested Solutions

    Title # Comments Views Activity
    how to hide hide popup page refresh second time? 10 59
    sumHeights  challenge 17 50
    Fibonacci challenge 11 61
    solarwind tftp server 2 16
    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
    The viewer will learn how to implement Singleton Design Pattern in Java.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now