File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes  Problem of finding the Context URL in WebApplication Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark " Problem of finding the Context URL in WebApplication" Watch " Problem of finding the Context URL in WebApplication" New topic
Author

Problem of finding the Context URL in WebApplication

kishore routhu
Greenhorn

Joined: Jan 17, 2013
Posts: 14
I am working with Web-Application (TestApp). In this application i used to implement the ServletContextListener(as ContextListener) and
contextInitialized(ServletContextEvent event) method contains the following code segment.

ServletContext servletContext = event.getServletContext();
// where event is ervletContextEventURL
String servletPath = servletContext.getContextPath()+"/MyServlet";
// MyServlet is URL of the Servlet
URL servletURL = sevletContext.getResource();

Here i am trying to get the complete URL of MyServlet ( expected servletURL = http://localhost:8080/TestApp/MyServlet) but it always returns '0'.
And servletContext.getContextPath() always returns /TestApp (expected http://localhost:8080/TestApp).

Anybody please suggest me that is there any way to find URL of context(http://localhost:8080/TestApp)
OR particular Servlet from ServletContext(http://localhost:8080/TestApp/MyServlet).

Using ApplicationServer : Jboss AS 7.1.1
Prajakta Acharya
Ranch Hand

Joined: Nov 08, 2012
Posts: 138

Have you tried using getRealPath(String path) of ServletContext?


Regards,
Prajakta
kishore routhu
Greenhorn

Joined: Jan 17, 2013
Posts: 14
Yes i tried with getRealPath("/MyServlet") but it returns as follows

/usr/java/jboss/jboss-as-7.1.1.Final/standalone/tmp/vfs/temp8e6f1bb35e6dff50/TestApp.war-bdc0e65482008d47/MyServlet
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61458
    
  67

You cannot get the URL of a servlet because that is controlled by the mappings in the web.xml, not their location in the file system.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
kishore routhu
Greenhorn

Joined: Jan 17, 2013
Posts: 14
I need to get the context URL
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem of finding the Context URL in WebApplication