Forums Register Login

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

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
The droid you seek is a context listener.
+Pie Number of slices to send: Send
Javadoc: ServletContextListener
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
The method is passed a ServletContextEvent instance from which you can obtain the ServletContext.

Edit: Oh.... that's not what you asked.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.

Ruth Stout was famous for gardening naked. Just like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1140 times.
Similar Threads
context parameter
Submission and startup of the client and of the server
JDO and transactions
Using Java, how to detect if an App is running
Struts init()
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 20:53:39.