• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Retreiving the application name only

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings,

I am in need of retrieving the application name only from a URL.

For example:
URLstring = "http://www.somewebaddresshere.com/applicationName/folderOne/folderWhatever/file.jsp"

I need to retrieve only the "/applicationName" from this URL. (The / can be there or not; that doesn't matter.)

I know that if I do a request.getContextPath I can retrieve this: "/applicationName/folderOne/folderWhatever" but I just want the application Name.
Is there some way to do this?

Thank you very much!
 
Sheriff
Posts: 67265
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you verify what you get from getContextPath() or are you just guessing?
 
bryan nelson
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've verified it...along with about 7 or 8 other getXXXXX methods from the HttpServletRequest object.

None of them so far return just the application name.

 
Bear Bibeault
Sheriff
Posts: 67265
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you have the context mapped in the container? getContextPath() will only return the, well, context path (what you are calling the "application name").
 
bryan nelson
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example, I'm using WebSphere and the name of the project/application that we are using is call "seeti". All of our urls look like this because they are running from that project/application:

http://www.testurl.com/seeti/folder/folder2/test.html

So in this instance I want to be able to pull back just the application name "seeti" (or whatever it may be at the time).
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

There are at least two getContextPath() methods, one in HttpServletRequest which is derived from the request URL and

the javax.servlet.ServletContext method getContextPath()

What does it return?
 
Ranch Hand
Posts: 577
Tomcat Server Notepad Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bryan,

I know that if I do a request.getContextPath I can retrieve this: "/applicationName/folderOne/folderWhatever" but I just want the application Name.
Is there some way to do this?


Why don't you use String utility methods to parse the above context path string to get application name(as you know the first name is always going to be application name)? I know this is a basic thing for anyone to do. What made this idea didn't work for your requirement?
 
bryan nelson
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William,

The one in the HttpServletRequest gives me this: "/seeti/folder/folder2"

How do I access the method from the ServletContext object? Actually, how to I access the ServletContext object at all?


Naren,

I can definitely do this...and it is the best option right now. However, I was just very curious as to whether or not there was a more straightforward way to do it with Java Servlets that I was just not aware of yet. But thanks!
 
Bear Bibeault
Sheriff
Posts: 67265
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If /seeti is the context path that you've mapped to the application, and getContextPath() is not returning just "/seeti", then the container is doing it wrong.

I've moved this to the WebSphere forum as this issue has not cropped up in any other known container. I'd check with WebSphere support to see if this is a known issue.
 
bryan nelson
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear...hopefully someone here will have an idea.

I'm still wondering how to access the ServletContext object though so I can try the other method...
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the ServletContext API:

The ServletContext object is contained within the ServletConfig object, which the Web server provides the servlet when the servlet is initialized.
...
See Also:
Servlet.getServletConfig(), ServletConfig.getServletContext()



Hope that helps you track that down.
 
bryan nelson
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That did help.

ServletContext.getContextPath() gives me this:

com.ibm.ws.webcontainer.facade.ServletContextFacade@156a156a

Not exactly what I was going for.

I'm using Websphere 6.1 if that matters.

Any other ideas?
 
Ranch Hand
Posts: 147
Eclipse IDE Tomcat Server Debian
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you can be sure that the first part of the URI is the application, couldn't you find the application name simply by splitting the path on the '/', and look for the first (non-empty) item? This is typically what I do to retrieve the application name.
 
Ranch Hand
Posts: 446
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you may already know... the EAR contains >= 1 WAR and each WAR has a unique context path. This context path is what is part of the URL. This allows the web container to delegate the requests targeted to specific web applications.

Now one can keep the request context path same as the application name. In that case you can get the request context path from HTTP Request object.

You can maintain a mapping property file, which matches the context path to application names.
 
bryan nelson
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pete,

Yes...that's exactly what I'm doing now. I just wondered if there was an even simpler way of doing it.

Deepak,

Where would this mapping be located? In the application.xml? The web.xml? I am unfamiliar with where to place this mapping.


Thank you to all.
 
Deepak Pant
Ranch Hand
Posts: 446
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a sample of application.xml file. This file get bundled in the EAR file and is present under META-INF folder.
The web request context path is mentioned here as well as in the "ibm-web-ext.xml" file, which gets bundled in the WAR file.


<?xml version="1.0" encoding="UTF-8"?>
<application
id="...some unique app id..."
version="5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/application_5.xsd">

<description>...some desc...</description>
<display-name>...display name...</display-name>

<!-- EJB 3.0 Jar Modules -->
<module>
<ejb>...name of the jar file with extn...</ejb>
</module>

<!-- Web Modules -->
<module>
<web>
<web-uri>...name of the war file with extn...</web-uri>
<context-root>...context-root-uri...</context-root>
</web>
</module>

<!-- Web Modules -->
<module>
<web>
<web-uri>...name of the war file with extn...</web-uri>
<context-root>...context-root-uri...</context-root>
</web>
</module>

</application>

 
bryan nelson
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. This is very helpful.

I hope this helps someone else!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!