• 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 not working

 
Ranch Hand
Posts: 310
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String str = getServletContext().getContextPath();

this code is not compiling. Please check it in your container and give me a reply please.

compile error says

---------- Compile ----------
CheckContext.java:16: cannot find symbol
symbol : method getContextPath()
location: interface javax.servlet.ServletContext
out.println("<center><h1>" + sc.getContextPath() + "</h1></center>");
^
1 error

Output completed (1 sec consumed) - Normal Termination



please check this code

 
Ranch Hand
Posts: 502
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
getContextPath() is not a part of ServletContext class. It is a part of HttpServletRequest class.

In your code,change this

String str = getServletContext().getContextPath();

TO This


String str = req.getContextPath();
[ December 12, 2006: Message edited by: Prabhu Venkatachalam ]
 
Ranch Hand
Posts: 292
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

out.println("<center><h1>" + sc.getContextPath()+ "</h1></center>");



There is no method of that name in ServletContext API...you have getContext() though
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I also agree with Prabhu that String str = req.getContextPath(); should work because the method getContextPath() is in the HttpSrvletRequest and Not in ServlerContext that you earlier mentioned.

Let us know if it works...
 
Sreeraj G Harilal
Ranch Hand
Posts: 310
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But in my J2EE API documentaion says ServletContext has getContextPath() method.

Will you please check the documentation and tell me that your documentation also contain this method or not.
 
Prabhu Venkatachalam
Ranch Hand
Posts: 502
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please send us the Link of document you referred.
 
Sreeraj G Harilal
Ranch Hand
Posts: 310
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everybody.
is documentation also has errors? . Beware!
I download it from sun's site
 
Prabhu Venkatachalam
Ranch Hand
Posts: 502
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, I found out. It is added in J2EE 5.

Please check which version of servlet API you have. It seems like you are using old version(J2EE 4).

hth,
 
Prabhu Venkatachalam
Ranch Hand
Posts: 502
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


is documentation also has errors? [shocked] . Beware!
I download it from sun's site



document doesn't have any error. you are referring to J2EE 5 API documentation and having J2EE 4 jars on your classpath. That is the reason for Compilation error.

Please refer my pervious post.
[ December 12, 2006: Message edited by: Prabhu Venkatachalam ]
 
Sreeraj G Harilal
Ranch Hand
Posts: 310
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Prabhu, now i checked the documentation online.I can see the difference .
In documentation of J2EE 1.4 does not shows ServletContext a method like getContextPath().

But in documentation of j2EE 1.5 it shows ServletContext has a method like getContextPath().

see the difference

documentation 1.5 [/url]
documentation 1.4 [URL=http://java.sun.com/j2ee/1.4/docs/api/]
 
Sreeraj G Harilal
Ranch Hand
Posts: 310
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Prabhu Venkatachalam for your reply.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic