• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The droid you seek is a context listener.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Javadoc: ServletContextListener
 
Pat Farrell
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
reply
    Bookmark Topic Watch Topic
  • New Topic