Nearby lessons

25 of 34

Servlet - Questions and Answers

📌 What You Will Learn
  • Understand FAQS
  • See complete working code examples

Test yourself with the complete Questions and Answers quiz — exam-style multiple-choice questions covering FAQS.

FAQS

  • Why GenericServlet class is declared as abstract?
  • HttpServlet class does not contain any asbtract method, still it is declared as abstract.What is

the reason?

  • Internally how web container will create servlet object?

4.Why every servlet should contains public no-arg constructor?

5.The main purpose of constructor is to perform initilization and every servlet compulsory contains

public no-arg constructor.Then what is need of init() method?

6.Is it possible to call destroy() method explicitly from init() and service() methods?

  • In How many ways we can develop servlet?
  • In Http based servlets , is it recommended to override service() method?

Q1. Why GenericServlet class is declared as abstract?

GenericServlet implements Servlet interface and provides implementation for all methods except

service() method.Hence GenericServlet class is declared as abstract.

Q2. HttpServlet class does not contain any asbtract method, still it is declared as abstract.What is

the reason?

Most of the methods present inside HttpServlet class are implemented to send error information

to the end user.B'z of this partial implementation HttpServlet class is declared as abstract.

Note:

  • If a class contains at least one abstract method then compulsory we should declare that class as

abstract otherwise we will get compile time error.

eg: GenericServlet

  • Eventhough class does not contain any abstract method still class declared as abstract if

implementation is partial.i.e abstract class can contain zero number of abstract methods also.

eg: HttpServlet

Q3. Internally how web container will create servlet object?

If we know class name at the beginning then we can create object by using new operator.

If we don't know class name at the beginning and it is available dynamically at runtime then we

can create object by using newInstance() method.

Web container don't know our servlet class names at the beginning and hence it is always using

newInstance() method to create Servlet object.

Q4.Why every servlet should contains public no-arg constructor?

web container will create servlet object by using newInstance() method.Internally newInstance()

method will call public no-arg constructor. Hence every servlet class should compulsory contain

public no-arg constructor,otherwise we will get java.lang.InstantiationException.

  • The main purpose of constructor is to perform initilization and every servlet compulsory

contains public no-arg constructor.Then what is need of init() method?

To perform initialization of servlet compulsory we should pass ServletConfig argument.But in

servlets constructor cannot take any arguments because newInstance() method.

Hence we cannot use constructor concept to perform initialization of the servlet.

Some mechanism must be required to perform initialization which is nothing but init() method.

init() → can take ServletConfig as argument

Constructor → cannot take any argument

Q6. Is it possible to call destroy() method explicitly from init() and service() methods?

Just before destroying servlet object web container will always calls destroy() method to perform

clean up activities.

But based on our requirement we can call destroy() method explicitly from init() and service()

methods,then it will be executed just like a normal method and servlet object wont be destroyed.

init()

{

destroy();

}

service()

{

destroy();

}

Q7. In how many ways we can develop a servlet?

3 ways

By implementing Servlet interface directly

By extending GenericServlet

By extending HttpServlet

Q8. In Http based servlets , is it recommended to override service() method?

No. If we override service() method then for any type of request same service() method will be

executed.

Playing Games with Request object

Objective:

By using HttpServletRequest, write code for

  • Retrieving form parameters
  • Retrieving Request headers
  • Retrieving Cookies
  • Retrieving Client and Server information

FAQs

  • What is the purpose of RequestDispatcher?
  • In How many ways we can get RD?
  • What is the difference b/w getting RD from request and context objects?
  • what are differences b/w forward() and include()?
  • what are differences b/w forward() and sendRedirect()?
  • What is Foreign RequestDispatcher and how we can get it?

7.What are the attributes added by web container while forwarding and including the request.

  • What is the purpose of these attributes and explain meaning?

Filters & Wrappers

Objectives:

  • Describe the web container's Request Processing Model?
  • Write & Configure a Filter?
  • Create a Request & Response Wrapper for the given problem?

4.Describe how to apply a Filter or wrapper?

Filters can be used for pre processing of request and post processing of request before they reach

target resource in the web application.

RequestCustomized

Request

CustomizedResponseTarget
ResponseServlet
Web BrowserFilterWeb Server

The most common application areas of Filters are logging,security checks,altering request

information,compressing response,encryption of response,authentication etc...

Filters concept introduced in Servlet 2.3 Version.

Filter API

Filter API contains the following 3 interfaces.

FAQs

Every Filter in Java has to implement Filter interface either directly or indirectly.

Filter interface defines the following 3 methods

1.init():

public void init(FilterConfig config)throws ServletException

This method will be executed only once to perform initialization activities.

Example03
JCode Cell
1 
2Filter
3FilterConfig
4FilterChain
5Filter(I):
6

FAQs

public void destroy()

This method will be executed only once to perform clean up activities just before taking the filter

from out of service.

Example04
JCode Cell
1 
2destroy()
3

FAQs

public void doFilter(ServletRequest req,ServletResponse resp,FilterChain fc)throws SE,IOE

Total filtering logic we have to define in this method only.

This method will be executed for every request.

By using FilterChain object we can forward the request to the next level.(it may be Servlet or

another Filter)

Example05
JCode Cell
1 
2doFilter()
3
📝 Key Takeaways
  • Key ideas of Servlet - Questions and Answers explained simply
  • Ready-to-use code examples
  • Exam-style questions at the end

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3