| | | | Question Answer Tutorial - JAVA->SERVLET | | | | Question: 16.What is the use of servlet wrapper classes?
| | Answer: Servlet HTTP API provides two wrapper classes – HttpServletR equestWrapper and />HttpServletResponseWrapper. These wrapper classes are provided to&nb sp;help developers with custom />implementation of servlet request and response types. We c an extend these classes and override only />specific methods we need to implement for custom request&n bsp;and response objects. These classes are not />used in normal servlet programming
| | | | | | | | Question: What is SingleThreadModel interface? | | Answer: SingleThreadModel interface was provided for thread safety and&nb sp;it guarantees that no two threads />will execute concurrently in the servlet’s service method.  ;However SingleThreadModel does not solve />all thread safety issues. For example, session attributes and static variables can still be accessed />by multiple requests on multiple threads at the same time, even when SingleThreadModel servlets are />used. Also it takes out all the benefits of multithre ading support of servlets, thats why this />interface is Deprecated in Servlet 2.4
| | | | | | | | Question: 18.Do we need to override service() method?
| | Answer: When servlet container receives client request, it invokes t he service() method which in turn />invokes the doGet(), doPost() methods based on the HTTP&nb sp;method of request. I don’t see any use case />where we would like to override service() method. The  ;whole purpose of service() method is to />forward to request to corresponding HTTP method implementations . If we have to do some />pre-processing of request, we can always use servlet filte rs and listeners
| | | | | | | | Question: Is it good idea to create servlet constructor? | | Answer: We can define a constructor for servlet but I don’t&nbs p;think its of any use because we won’t be having />access to the ServletConfig object until unless servlet is initialized by container. Ideally if we />have to initialize any resource for servlet, we should&nbs p;override init() method where we can access />servlet init parameters using ServletConfig object.
/>
| | | | | | | | Question: What is difference between GenericServlet and HttpServlet?
| | Answer: GenericServlet is protocol independent implementation of Servlet interface whereas HttpServlet is />HTTP protocol specific implementation. Most of the times w e use servlet for creating web application />and that’s why we extend HttpServlet class. HttpServlet cl ass extends GenericServlet and also />provide some other methods specific to HTTP protocol. />
| | | | | | | | | | | | |