• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

how to get servletcontext name in a JSP

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

i have to get the servletcontext name in my application.

Eg: http://localhost:8080/myapplication/index.jsp, here i have to get the servletcontext/webapp name (i.e. myapplication) in my app. But here, i should not use request.getContextPath(); as this comes from the browser URL. The reason is, For my application i am not giving the direct URL of server to client. Client access the application from a proxy. So, in my application i can't take this servletcontext name from the browser.

For this, i am doing this :-

in deployment descriptor (web.xml)

i have given the tag as,


<web-app>
<display-name>myapplication</display-name>
- - -
- - -
</web-app>

In JSP, to get the servletcontextname i am using this:
request.getSession(false).getServletContext().getServletContextName();
Is any other way to get servletcontext name, because in the way which i have mentioned i should give the name compulsory in deployment descriptor file.

Please let me know if any other choices. Thanks in advance.....



 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You do not need to go through the session to obtain the ServletContext object. The implicit object 'application' does that for you.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure how the proxy is working, but did you try getServletContext().getContextPath() instead of request.getContextPath()??
 
Ramna Reddy
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Ankit: getServletContext().getContextPath() is not working. I think getContextPath() is not valid in servletcontext.

Can you suggest any other way/idea instead of doing all this with deployment descriptor and getting value.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can you suggest any other way/idea instead of doing all this with deployment descriptor and getting value.


What was wrong with the solution Sebastian posted?
 
Ramna Reddy
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Ulf Dittmer:
Sebastian suggested to use "application'' instead of "request.getSession(false).getServletContext",
But the problem is not to do with this.

Please can anybody suggest me in this. Without depending/using on this deployment descriptor can we get servlet context name? If, how we can? Thanks in advance.
 
Sheriff
Posts: 67753
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
There is a supported way to get this value which you do not want to use. Why not?
 
Oh, sure, you could do that. Or you could eat some pie. While reading this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic