Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to get servletcontext name in a JSP

 
Ramna Reddy
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • 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.....



 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • 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.
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64824
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a supported way to get this value which you do not want to use. Why not?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic