• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Doubts on Servlet

 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
few doubts regarding servlets :

1. Can a servlet have a constructor ? - my answer is yes.
2. is it mandatory that i should override the doGet() and doPost() methods ? Can I have a servlet(user defined servlet) without doGet() and doPost() methods ?
3 Suppose I have a userdefined servlet that extends HttpServlet what are the methods that I mandatorily need to override in my user defined servlet ?
4. When do we use ServletResponse and when do we use HttpServletResponse. The methods like encodeURL, sendRedirect etc are present in HttpServletResponse but not in ServletResponse.
 
author & internet detective
Posts: 41125
848
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jose,
1) Yes. Any class can have a constructor.
2/3) What happened when you tried it? Just create a class extending HttpServlet with no methods and see what compiler errors if any you get. While someone could answer this, it is much more instructional to try it.
4) When you have a HttpServlet, you should always be using HttpServletResponse.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jose chiramal wrote:
1. Can a servlet have a constructor ? - my answer is yes.


Yes! check the API

jose chiramal wrote:
2. is it mandatory that i should override the doGet() and doPost() methods ? Can I have a servlet(user defined servlet) without doGet() and doPost() methods ?


Then how do you communicate with the client?

jose chiramal wrote:
3 Suppose I have a userdefined servlet that extends HttpServlet what are the methods that I mandatorily need to override in my user defined servlet ?


I think, it depends on our requirements!

jose chiramal wrote:
4. When do we use ServletResponse and when do we use HttpServletResponse.


Check the servlet hierarchy!
 
Ranch Hand
Posts: 384
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. well, yes
2. no you cannot ... because then you wont be able to communicate with the container ... (the client). you need to extend atleast one of the 8 methods. check for the methods in the Servlet spec
3. any one of the above 8 methods ... mostly doGet() or doPost() since the html sends requet in these two forms only
4. it depends on you ... which one fits best for your app. the methods you are talking about are HTTP related so they are in HTTPServlet Class only
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to override atleast anyone of the eight methods (doGet(), doPost(),doDelete(), doOptions(), doPut(), doHead(), doOptions(), doHead()) in your servlet depending on purpose of the servlet. But the most commonly used methods are doGet(), doPost().

If you are calling the servlet from a HTML you can define the method that has to be called in the servlet in the method attribute of form tag. Default is doGet().

Well you don't have to care about when to use the methods in the ServletResponse or when not to, because we are dealing with HTTP we only use HttpServletResponse, which inherits ServletResponse.




 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic