• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getContextPath() in HttpServletRequest

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 489
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ben Souther , Stefan Evans
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Surfs up space ponies, I'm making gravy without this lumpy, tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic