File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes how-to call some servlet or bean when war is loaded Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "how-to call some servlet or bean when war is loaded" Watch "how-to call some servlet or bean when war is loaded" New topic
Author

how-to call some servlet or bean when war is loaded

Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

My application needs some once-only code to be run first, before any other pages, JSP, HTML, etc are delivered. Its easy to have the main beans call the startup code, but I don't see how to make the container (Tomcat, Glassfish, etc.) call the code at startup or at reload. I assume that something in the WEB.XML can do this, but I can't find it. Googling finds zillions of entries, the query is not precise enough for a good result.

Thanks
Pat
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61589
    
  67

The droid you seek is a context listener.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61589
    
  67

Javadoc: ServletContextListener
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Bear Bibeault wrote:Javadoc: ServletContextListener


I see, only thing I don't see is how the Listener that I seek is able to tell the host and port that the container is using. Everything else I think I want is there
in the ServletContext
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61589
    
  67

The method is passed a ServletContextEvent instance from which you can obtain the ServletContext.

Edit: Oh.... that's not what you asked.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Bear Bibeault wrote:Edit: Oh.... that's not what you asked.


Yeah. What I'm trying to do is define some EL accessible values that can be used by anything.
Its easy to do once the user is logged in, I can call the once-only code with the request which have the host, port, context, etc.

The problem is that I want to display a page, the index.jsp before the user has done anything, and if this is the first user since the WAR was deployed, before anyone has had a chance to do anything.

For now, I'm thinking that this may be impossible, and I should just hard code the images, stylesheets, etc. on the index.jsp page

Pat
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61589
    
  67

Pat Farrell wrote: and I should just hard code the images, stylesheets, etc. on the index.jsp page

Why would you do that? You don't need the domain or port to create server-relative URLs for such resources. Just the context path.
Daniel Val
Ranch Hand

Joined: Jan 09, 2012
Posts: 44
Pat Farrell wrote:
Bear Bibeault wrote:Edit: Oh.... that's not what you asked.


Yeah. What I'm trying to do is define some EL accessible values that can be used by anything.
Its easy to do once the user is logged in, I can call the once-only code with the request which have the host, port, context, etc.



How you do it:

NetworkInterface.getNetworkInterfaces(); then each of it would have a number of InetAddresses that are retrieved with the accessor getInetAddresses(); then each InetAddress will have getHostName and getHostAddress that would retrieve the stuff you mentioned.

However:
- As another person said, if this is to refer resources from your app, you don't need it, just put the relative path
- A machine can have more than one ip address and your container might listen to all of them (the very usual setup)
- Usually when you say http://whateveraddress.com, then this resolves to an ip address that might not be of the machine, but of the router that is placed on the network as a gateway to the internet. And the machine would not have a routable ip address thus the value you get might not be a good one.

Now, if you want to display it - don't use any listener, create a custom JSP tag and write that code inside and then simply render the value to the output.

D.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how-to call some servlet or bean when war is loaded