• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Do servlets have dynamic binding?

 
Ravi Danum
Ranch Hand
Posts: 154
1
  • Mark post as helpful
  • send pies
  • 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




 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15627
46
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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: 154
1
  • Mark post as helpful
  • send pies
  • 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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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: 154
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Many thanks, Bear -- that is what is needed in my design.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic