• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The droid you seek is a context listener.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Javadoc: ServletContextListener
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic