• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubts on Servlet  RSS feed

 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • 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.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36446
454
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • 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!
 
Lalit Mehra
Ranch Hand
Posts: 384
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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
 
Saravanan. Shanmugam
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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.




 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!