• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getContextPath() in HttpServletRequest

 
bharadwaja kambhammettu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()
 
Jerry Bustamente
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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


In this case how to get the path.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1691
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ben Souther , Stefan Evans
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic