aspose file tools*
The moose likes Servlets and the fly likes how to get servletcontext name in a JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to get servletcontext name in a JSP" Watch "how to get servletcontext name in a JSP" New topic
Author

how to get servletcontext name in a JSP

Ramna Reddy
Ranch Hand

Joined: Aug 06, 2006
Posts: 96
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

Joined: Feb 23, 2009
Posts: 1183
You do not need to go through the session to obtain the ServletContext object. The implicit object 'application' does that for you.


JDBCSupport - An easy to use, light-weight JDBC framework -
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

I'm not sure how the proxy is working, but did you try getServletContext().getContextPath() instead of request.getContextPath()??


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ramna Reddy
Ranch Hand

Joined: Aug 06, 2006
Posts: 96
@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
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
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?


Ping & DNS - my free Android networking tools app
Ramna Reddy
Ranch Hand

Joined: Aug 06, 2006
Posts: 96
@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

Joined: Jan 10, 2002
Posts: 61434
    
  67

There is a supported way to get this value which you do not want to use. Why not?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to get servletcontext name in a JSP