Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Use of request.getContextPath in a JSP

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am little confused about the use of getContextPath method in the following code of a JSP :

<a href="<%=request.getContextPath()%>/servlet/FrontController?action=sayHello">Say Hello</a>

<a href="<%=request.getContextPath()%>/servlet/FrontController?action=sayGoodbye">Say Goodbye</a>

<a href="<%=request.getContextPath()%>/servlet/FrontController?action=displayDate">Display Date</a>

The API for getContextPath()says "it returns the context path of requested URI " ,so what does it mean by �context path� of requested URI.
 
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
http://www.mydomain.com:8080/myWebApp/index.html

J2EE (now called Java EE) application servers can host more than one "webapp".
The portion of the URI just after the port (shown in bold) is known as the contextPath for a webapp.

In this case 'index.html' is in root directory of the webapp called myWebApp.

In those links you've posted, the developer (rightly) built the relative links by reading the contextPath from the request object. This allows for the renaming of the webapp without the need to alter every link in the app.
 
Ghazala Islam
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so you mean the context path in uri is used only when there is a possibility of change of webapp name,otherwise the code could've been :

a href ="/servlet/FrontController?action=displayDate">Display Date</a>
 
Sheriff
Posts: 67595
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. The point is that for resources referenced fomr the client such as images, style sheets and script files. the context path is required. Otherwise, the server wouldn't know which web app was being referenced.

If you hard-code the context path, you'd need to fix all these references up if it were to change.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic