• Post Reply Bookmark Topic Watch Topic
  • New Topic

override HttpServlet init() method  RSS feed

 
Josh Diggs
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to override just the init() method in HttpServlet, and let the super class handle interpreting my jsp pages. When extending javax.servlet.http.HttpServlet, the super class just outputs a not supported message.

Does anyone know how I can accomplish this in tomcat 6. I glanced at some of the org.apache.catalina classes, but I'm not sure I'm moving in the right direction there.

Thanks
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

First off, leave the source of the Catalina classes out of it for now; you'll just confuse yourself.

You talk about "letting the superclass handle your JSP pages". I can't quite make sense of that - are you talking about a servlet or a JSP? In a JSP you wouldn't extend anything, nor would you use the init method (you might use jspInit, though).
 
Josh Diggs
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I'm trying to accomplish is to initialize a connection to another service at the application level (thus the extension of servlet to encapsulate the objects necessary for this connection). I have jsp pages that I would like to use to display the user interface, and I only want to run the initialization once when the application server starts. My understanding was that jsp pages are served by the standard or reference servlet, and that I would be able to override the init method of javax.servlet.http.HttpServlet and let the super.doGet() and super.doPost() methods do the real work of serving the jsp pages.

I missed that one by a mile, but I was wondering if there is still a way to do what I'm trying to do. My first solution was to use static variables for reference across the entire application, but I'm afraid that GC might wipe out some of these values if I'm not careful.

Thanks for the response
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Josh Diggs:
I missed that one by a mile, but I was wondering if there is still a way to do what I'm trying to do.

What you are looking for is a context listener.

You set up a class that implements the ServletContextListener interface. In the contextInitialized() method you can do whatever setup steps you deem appropriate and store data in the application context that will available throughout the lifetime of the application.

The listener is registered in the deployment descriptor.
[ July 01, 2008: Message edited by: Bear Bibeault ]
 
Josh Diggs
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's exactly what I wanted, thanks so much.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!