?
Solved

context path in servlet

Posted on 2008-11-18
3
Medium Priority
?
830 Views
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.
0
Comment
Question by:angela_smith
2 Comments
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 22982987
request.getContextPath()/custom/xsl/abc.xsl;
0
 
LVL 4

Accepted Solution

by:
pellep earned 400 total points
ID: 22983143
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 http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletContext.html#getRealPath(java.lang.String) )

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 http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletContext.html#getResourceAsStream(java.lang.String) ). 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

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
A solution for Fortify Path Manipulation.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

807 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