• 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

Do servlets have dynamic binding?

 
Ranch Hand
Posts: 165
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hello All,

Is there a notion of virtual methods in servlets?  For example, given that servlet A is a base class for servlet B.

If servlet A has a doGet method, but servlet B does not, will a doGet call on servlet B cause A's doGet to be called?

If A's doGet method does get called in this case, and during the execution of A's doGet, a method in B is called.  

Note that A has the method that was called, but will B's method be the one executes?  It is like virtual in c++.

Do servlets have this capability?


Thanks for any help you can give.

-Ravi




 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Servlets are classes just like any other class in Java, and methods in Java are always virtual*, so yes, there's dynamic binding, just like in any other class.

(* unless the method is static or final, but in that case you can't override it anyway).
 
Ravi Danum
Ranch Hand
Posts: 165
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thank you, Jesper.  This is perfectly logical, and I will keep it in mind for future servlet design/development.

Since I sent the question, I redesigned the servlet.  The base servlet contains an init(ServletConfig config) method which is used to access a properties file using getResourceAsStream(...).  The properties it gets are stored in static variables, and can be accessed by any subclasses.  This base servlet will be initialized first by using load-on-startup in the web.xml file.  The base servlet also contains a protected method that can be called by its subclasses.  Any subclasses can implement the init() method, and doGet.  The protected base class method can be accessed by the subclasses as need be.

-Ravi
 
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
A better design might be to use a context listener to preload the properties into app context.
 
Ravi Danum
Ranch Hand
Posts: 165
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Many thanks, Bear -- that is what is needed in my design.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic