This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to get servletcontext name in a JSP

 
Ramna Reddy
Ranch Hand
Posts: 96
  • 0
  • 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
  • 0
  • 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: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • 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
  • 0
  • 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
Pie
Posts: 42966
73
  • 0
  • 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
  • 0
  • 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: 64188
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • 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?
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic