• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

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: 67671
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.
 
eat bricks! HA! And here's another one! And a tiny ad!
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic