This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes getContextPath() in HttpServletRequest Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "getContextPath() in HttpServletRequest" Watch "getContextPath() in HttpServletRequest" New topic
Author

getContextPath() in HttpServletRequest

bharadwaja kambhammettu
Greenhorn

Joined: Apr 07, 2005
Posts: 5
hi all,

What is the difference between getContextPath() and getRealPath() in HttpServletRequest ? Can anybody throw some light on this ?
I intend to call them from the JSP implicit object, request.


cheers & take care,<br />Bharadwaja K
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

getRealPath() in ServletRequest is deprecated. You have to use the getRealPath(String path) method of the ServletContext object instead. This would return you the absolute path to the resources of the web application on the container that hosts it.

getContextPath() returns the context portion of the request uri. So a call to getContextPath() in index.jsp with the url, http://<m/c> ort/web_app/jsp/index.jsp would return /web_app/jsp.

ram.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The best place to go if you need information about a method is the API.

java.lang.String&#41;" target="_blank" rel="nofollow">http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html#getRealPath(java.lang.String)
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpServletRequest.html#getContextPath()


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Jerry Bustamente
Ranch Hand

Joined: May 24, 2004
Posts: 90
I would like to ask for more clarification.

As a result of the example below would it be accurate to say that getContextPath()always returns the directory structure up to the directory that contains the resource?

Thank you.

Sincerely,

Jerry B.

getContextPath() returns the context portion of the request uri. So a call to getContextPath() in index.jsp with the url, http://<m/c> ort/web_app/jsp/index.jsp would return /web_app/jsp.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

No context path returns the name of your app as seen in the URL.
I.E: in <a href="http://localhost:8080/<b rel="nofollow">MyApp</b>/mainpage.jsp" target="_blank">http://localhost:8080/MyApp/mainpage.jsp
"MyApp" is your contextPath.

getRealPath, on the other hand, returns the path on the server's file system leading to your application if the app is being run as an exploded file system.
If your app has been deployed as war file, getRealPath will return null.
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

If your app has been deployed as war file, getRealPath will return null.


In this case how to get the path.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Vishnu Prakash:


In this case how to get the path.

There isn't one.
The server may or may not unpack the war file in it's own working directories but that isn't a space you, the programmer, would concern yourself with.

For this reason, I recommend not using getRealPath if you want your app to be portable.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
If you want to retrieve resources from your WAR, you might try ServletContext.getResource, which returns a URL.
ServletContext.getResourceAsStream() returns an input stream

Can be useful for accessing properties files etc that you put in web.xml.
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
Thanks Ben Souther , Stefan Evans
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
From ServletContext


getResource

public URL getResource(String path)
throws MalformedURLException

Returns a URL to the resource that is mapped to a specified path. The path must begin with a "/" and is interpreted as relative to the current context root.

This method allows the servlet container to make a resource available to servlets from any source. Resources can be located on a local or remote file system, in a database, or in a .war file.

The servlet container must implement the URL handlers and URLConnection objects that are necessary to access the resource.

This method returns null if no resource is mapped to the pathname.

Some containers may allow writing to the URL returned by this method using the methods of the URL class.

The resource content is returned directly, so be aware that requesting a .jsp page returns the JSP source code. Use a RequestDispatcher instead to include results of an execution.

This method has a different purpose than java.lang.Class.getResource, which looks up resources based on a class loader. This method does not use class loaders.


This method allows the servlet container to make a resource available to servlets from any source. Resources can be located on a local or remote file system, in a database, or in a .war file.

The above sentence looks contradicting to


The path must begin with a "/" and is interpreted as relative to the current context root.


As for my understanding goes this method is useful to find resources within the web application and NOT outside the web application.

We use ClassLoader's getResource to access resources from anywhere in the file system.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getContextPath() in HttpServletRequest
 
Similar Threads
Wildcard servlet mappings in web.xml - how to get the part behind the * ?
Wrong answer in HFSJ 2nd Edition on Chapter 5 question 14
context root
Retreiving the application name only
difference between the getContextPath of ServletRequest and ServletContext