• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Methods in Servlet

 
Ripan Karmakar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we use any user defined methods in the Servlet like as we define/use in Java.........if so any one reply with an example......

Regards

ripank
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure you can. But for the most part, you will want to put those methods in a Java Object that is agnostic to your Servlet. That's better design anyway.

 
Navin Ramineni
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Becareful with the method names. For example, if you override the service() methods in your servlet class, you will lose the functionality provided by the HttpServlet Class and the doXXX methods will not be called automatically
 
Kj Reddy
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Preferably make those methods private.

Regards
Jagdish
 
Vijayendra V Rao
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use methods in a servlet just the way you would use it a normal Java class. Quite a few times (make that most of the times if you wish), you will need to a few operations within your request handler methods. There is nothing to stop you from writing a method, public or private according to your app design, and calling it from your service methods.

However, as Gregg has rightly pointed out, having Java classes with specific responsibilities is way much better OOP. Service methods can use these objects to process user requests.

The bottom line is, yes you can definitely use methods inside your servlets just the way you do in normal Java classes
 
Manish Vyas
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr. Rao, you mean we can also use java methods in Servlet without doXXXX() method... Please clarify.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic