SlideShare una empresa de Scribd logo
1 de 2
Servlet Viva Questions
1. : What is servlet?
A: A servlet is a Java programming language class that is used to extend the capabilities of servers that
host applications accessed by means of a request- response programming model. Before the servlet,
CGI scripting language was used as server side programming language.
2. Q: What is the use of servlet?
A: Uses of servlet includes:
Processing and storing data submitted by an HTML form.
Providing dynamic content.
A Servlet can handle multiple request concurrently and be used to develop high performance system
Managing state information on top of the stateless HTTP.
3. Q: What is the life cycle of servlet?
A: Life cycle of Servlet:
Servlet class loading
Servlet instantiation
Initialization (call the init method)
Request handling (call the service method)
Removal from service (call the destroy method)
4. Q: Why do we need constructor in servlet if we use the init ()?
A: Even though there is an init method in a servlet which gets called to initialize it, a constructor is still
required to instantiate the servlet. Even though you as the developer would never need to explicitly call
the servlet's constructor, it is still being used by the container.
5. Q: How servlet is loaded?
A: The servlet is loaded by:
First request is made.
Server starts up (auto-load).
There is only a single instance which answers all requests concurrently. This saves memory and allows
a Servlet to easily manage persistent data.
Administrator manually loads.
6. Q: When the servlet is unloaded?
A: Servlet gets unloaded when:
Server shuts down.
Administrator manually unloads.
7. Q: What is servlet interface?
A: The central abstraction in the Servlet API is the Servlet interface. All servlets implement this
interface, either directly or more commonly by extending a class that implements it.
8. Q: What is the generic servlet class?
A: GenericServlet is an abstract class that implements the Servlet interface and the ServletConfig
interface. In addition to the methods declared in these two interfaces, this class also provides simple
versions of the lifecycle methods init () and destroy (), and implements the log method declared in the
ServletContext interface.
9. Q: What is the difference between GenericServlet and HttpServlet?
A: The difference is:
 The GenericServlet is an abstract class that is extended by HttpServlet to provide HTTP
protocol-specific methods. But HttpServlet extends the GenericServlet base class and provides
a framework for handling the HTTP protocol.
 The GenericServlet does not include protocol-specific methods for handling request
parameters, cookies, sessions and setting response headers. The HttpServlet subclass passes
generic service method requests to the relevant doGet () or doPost () method.
 GenericServlet is not specific to any protocol. HttpServlet only supports HTTP and HTTPS
protocol.
10. Q: Why HttpServlet class is declared abstract?
A: The HttpServlet class is declared abstract because the default implementations of the main service
methods do nothing and must be overridden. This is a convenience implementation of the Servlet
interface, which means that developers do not need to implement all service methods.
If your servlet is required to handle doGet () requests for example, there is no need to write a doPost ()
method too.
11. Q: Can servlet have a constructor?
A: Yes
12. Q: What are the type of protocols supported by the HttpServlet?
A: It extends the GenericServlet base class and provides a framework for handling the HTTP protocol.
So, HttpServlet only supports HTTP and HTTPS protocol.
13. Q: What is the difference between the doGet () and doPost ()?
A: The difference is:
In doGet() the parameters are appended to the URL and sent along with header information. In doPost
(),send the information through a socket back to the webserver and it won't show up in the URL bar.
The amount of information you can send back using a GET is restricted as URLs can only be 1024
characters. You can send much more information to the server by using post and it's not restricted to textual
data either. It is possible to send files and even binary data such as serialized Java objects!
DoGet() is a request for information.It does not change anything on the server. (doGet () should be
idempotent). doPost () provides information (such as placing an order for merchandise) that the server is
expected to remember.
14. Q: When to use doGet() and when doPost()?
A:Always prefer to use GET (As because GET is faster than POST), except mentioned in the following
reason:
If data is sensitive.
Data is greater than 1024 characters.
If your application don't need bookmarks.
15. Q: How do I support both doGet () and doPost () from same servlet?
A:The easy way is, just support POST, then have your doGet method call your doPost method.

Más contenido relacionado

La actualidad más candente

Software Architecture Views and Viewpoints
Software Architecture Views and ViewpointsSoftware Architecture Views and Viewpoints
Software Architecture Views and ViewpointsHenry Muccini
 
Drone racing - beginner's guide
Drone racing - beginner's guideDrone racing - beginner's guide
Drone racing - beginner's guideBartosz Kosarzycki
 
محاضرة عن مقدمة عن النظم الهيدروليكية
محاضرة عن مقدمة عن النظم الهيدروليكيةمحاضرة عن مقدمة عن النظم الهيدروليكية
محاضرة عن مقدمة عن النظم الهيدروليكيةSabir Ahmed
 
Dosing pump catalog
Dosing pump catalogDosing pump catalog
Dosing pump catalogjon sun
 
Fieldbus Tutorial Part 3 - Example Applications
Fieldbus Tutorial Part 3  - Example ApplicationsFieldbus Tutorial Part 3  - Example Applications
Fieldbus Tutorial Part 3 - Example ApplicationsEmerson Exchange
 
Autonomous aerial vehicle based on computer vision
Autonomous aerial vehicle based on computer visionAutonomous aerial vehicle based on computer vision
Autonomous aerial vehicle based on computer visionSaad Mohammad Araf
 
System Analysis and Design
System Analysis and Design System Analysis and Design
System Analysis and Design Matthew McKenzie
 
Thermal analysis of shell and tube heat exchanger
Thermal analysis of shell and tube heat exchangerThermal analysis of shell and tube heat exchanger
Thermal analysis of shell and tube heat exchangereSAT Publishing House
 
Sample size issues on reliability test design
Sample size issues on reliability test designSample size issues on reliability test design
Sample size issues on reliability test designASQ Reliability Division
 
Synopsis of a Drone ( Quadcopter )
Synopsis of a Drone ( Quadcopter )Synopsis of a Drone ( Quadcopter )
Synopsis of a Drone ( Quadcopter )Ashwani Dixit
 
Requirement Elicitation
Requirement ElicitationRequirement Elicitation
Requirement ElicitationRavikanth-BA
 
Case study on aero elasticity
Case study on aero elasticityCase study on aero elasticity
Case study on aero elasticitysanjana_ane
 
Elements Of Aircraft Maintenance Reserve Development Iata Mcc
Elements Of Aircraft Maintenance Reserve Development   Iata MccElements Of Aircraft Maintenance Reserve Development   Iata Mcc
Elements Of Aircraft Maintenance Reserve Development Iata Mccspackert
 
Robinson Helicopter Private Lesson 1
Robinson Helicopter Private Lesson 1Robinson Helicopter Private Lesson 1
Robinson Helicopter Private Lesson 1Heather Howley
 

La actualidad más candente (18)

Software Architecture Views and Viewpoints
Software Architecture Views and ViewpointsSoftware Architecture Views and Viewpoints
Software Architecture Views and Viewpoints
 
Ch3. agile sw dev
Ch3. agile sw devCh3. agile sw dev
Ch3. agile sw dev
 
Drone racing - beginner's guide
Drone racing - beginner's guideDrone racing - beginner's guide
Drone racing - beginner's guide
 
محاضرة عن مقدمة عن النظم الهيدروليكية
محاضرة عن مقدمة عن النظم الهيدروليكيةمحاضرة عن مقدمة عن النظم الهيدروليكية
محاضرة عن مقدمة عن النظم الهيدروليكية
 
Dosing pump catalog
Dosing pump catalogDosing pump catalog
Dosing pump catalog
 
Fieldbus Tutorial Part 3 - Example Applications
Fieldbus Tutorial Part 3  - Example ApplicationsFieldbus Tutorial Part 3  - Example Applications
Fieldbus Tutorial Part 3 - Example Applications
 
Autonomous aerial vehicle based on computer vision
Autonomous aerial vehicle based on computer visionAutonomous aerial vehicle based on computer vision
Autonomous aerial vehicle based on computer vision
 
System Analysis and Design
System Analysis and Design System Analysis and Design
System Analysis and Design
 
Thermal analysis of shell and tube heat exchanger
Thermal analysis of shell and tube heat exchangerThermal analysis of shell and tube heat exchanger
Thermal analysis of shell and tube heat exchanger
 
Sample size issues on reliability test design
Sample size issues on reliability test designSample size issues on reliability test design
Sample size issues on reliability test design
 
Synopsis of a Drone ( Quadcopter )
Synopsis of a Drone ( Quadcopter )Synopsis of a Drone ( Quadcopter )
Synopsis of a Drone ( Quadcopter )
 
Requirement Elicitation
Requirement ElicitationRequirement Elicitation
Requirement Elicitation
 
System 800xA Hardware trouble shooting
System 800xA Hardware trouble shootingSystem 800xA Hardware trouble shooting
System 800xA Hardware trouble shooting
 
Chapter 7 Use Case Model
Chapter 7 Use Case ModelChapter 7 Use Case Model
Chapter 7 Use Case Model
 
Case study on aero elasticity
Case study on aero elasticityCase study on aero elasticity
Case study on aero elasticity
 
Landing gear
Landing gear Landing gear
Landing gear
 
Elements Of Aircraft Maintenance Reserve Development Iata Mcc
Elements Of Aircraft Maintenance Reserve Development   Iata MccElements Of Aircraft Maintenance Reserve Development   Iata Mcc
Elements Of Aircraft Maintenance Reserve Development Iata Mcc
 
Robinson Helicopter Private Lesson 1
Robinson Helicopter Private Lesson 1Robinson Helicopter Private Lesson 1
Robinson Helicopter Private Lesson 1
 

Similar a Servlet Interview Questions

SERVER SIDE PROGRAMMING
SERVER SIDE PROGRAMMINGSERVER SIDE PROGRAMMING
SERVER SIDE PROGRAMMINGPrabu U
 
Java EE 01-Servlets and Containers
Java EE 01-Servlets and ContainersJava EE 01-Servlets and Containers
Java EE 01-Servlets and ContainersFernando Gil
 
Servlet in java , java servlet , servlet servlet and CGI, API
Servlet in java , java servlet , servlet servlet and CGI, APIServlet in java , java servlet , servlet servlet and CGI, API
Servlet in java , java servlet , servlet servlet and CGI, APIPRIYADARSINISK
 
Advance Java Programming (CM5I) 6.Servlet
Advance Java Programming (CM5I) 6.ServletAdvance Java Programming (CM5I) 6.Servlet
Advance Java Programming (CM5I) 6.ServletPayal Dungarwal
 
Java Servlet
Java ServletJava Servlet
Java ServletYoga Raja
 
Http Server Programming in JAVA - Handling http requests and responses
Http Server Programming in JAVA - Handling http requests and responsesHttp Server Programming in JAVA - Handling http requests and responses
Http Server Programming in JAVA - Handling http requests and responsesbharathiv53
 
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B KuteJava Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B KuteTushar B Kute
 
UNIT-3 Servlet
UNIT-3 ServletUNIT-3 Servlet
UNIT-3 Servletssbd6985
 
An Introduction To Java Web Technology
An Introduction To Java Web TechnologyAn Introduction To Java Web Technology
An Introduction To Java Web Technologyvikram singh
 
java Servlet technology
java Servlet technologyjava Servlet technology
java Servlet technologyTanmoy Barman
 

Similar a Servlet Interview Questions (20)

Servlet
Servlet Servlet
Servlet
 
Java servlets
Java servletsJava servlets
Java servlets
 
SERVER SIDE PROGRAMMING
SERVER SIDE PROGRAMMINGSERVER SIDE PROGRAMMING
SERVER SIDE PROGRAMMING
 
Wt unit 3
Wt unit 3 Wt unit 3
Wt unit 3
 
TY.BSc.IT Java QB U3
TY.BSc.IT Java QB U3TY.BSc.IT Java QB U3
TY.BSc.IT Java QB U3
 
Java EE 01-Servlets and Containers
Java EE 01-Servlets and ContainersJava EE 01-Servlets and Containers
Java EE 01-Servlets and Containers
 
Servlets
ServletsServlets
Servlets
 
gayathri.pptx
gayathri.pptxgayathri.pptx
gayathri.pptx
 
Servlet in java , java servlet , servlet servlet and CGI, API
Servlet in java , java servlet , servlet servlet and CGI, APIServlet in java , java servlet , servlet servlet and CGI, API
Servlet in java , java servlet , servlet servlet and CGI, API
 
Servlet by Rj
Servlet by RjServlet by Rj
Servlet by Rj
 
Servlet classnotes
Servlet classnotesServlet classnotes
Servlet classnotes
 
Advance Java Programming (CM5I) 6.Servlet
Advance Java Programming (CM5I) 6.ServletAdvance Java Programming (CM5I) 6.Servlet
Advance Java Programming (CM5I) 6.Servlet
 
Java Servlet
Java ServletJava Servlet
Java Servlet
 
Http Server Programming in JAVA - Handling http requests and responses
Http Server Programming in JAVA - Handling http requests and responsesHttp Server Programming in JAVA - Handling http requests and responses
Http Server Programming in JAVA - Handling http requests and responses
 
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B KuteJava Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
 
Servlets
ServletsServlets
Servlets
 
UNIT-3 Servlet
UNIT-3 ServletUNIT-3 Servlet
UNIT-3 Servlet
 
An Introduction To Java Web Technology
An Introduction To Java Web TechnologyAn Introduction To Java Web Technology
An Introduction To Java Web Technology
 
java Servlet technology
java Servlet technologyjava Servlet technology
java Servlet technology
 
Servlet basics
Servlet basicsServlet basics
Servlet basics
 

Más de Vipul Naik

Jsp viva questions
Jsp viva questionsJsp viva questions
Jsp viva questionsVipul Naik
 
Javascript viva questions
Javascript viva questionsJavascript viva questions
Javascript viva questionsVipul Naik
 
Xml viva questions
Xml viva questionsXml viva questions
Xml viva questionsVipul Naik
 
Java questions for viva
Java questions for vivaJava questions for viva
Java questions for vivaVipul Naik
 
Html viva questions
Html viva questionsHtml viva questions
Html viva questionsVipul Naik
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generationVipul Naik
 

Más de Vipul Naik (6)

Jsp viva questions
Jsp viva questionsJsp viva questions
Jsp viva questions
 
Javascript viva questions
Javascript viva questionsJavascript viva questions
Javascript viva questions
 
Xml viva questions
Xml viva questionsXml viva questions
Xml viva questions
 
Java questions for viva
Java questions for vivaJava questions for viva
Java questions for viva
 
Html viva questions
Html viva questionsHtml viva questions
Html viva questions
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generation
 

Último

Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 

Último (20)

Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 

Servlet Interview Questions

  • 1. Servlet Viva Questions 1. : What is servlet? A: A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request- response programming model. Before the servlet, CGI scripting language was used as server side programming language. 2. Q: What is the use of servlet? A: Uses of servlet includes: Processing and storing data submitted by an HTML form. Providing dynamic content. A Servlet can handle multiple request concurrently and be used to develop high performance system Managing state information on top of the stateless HTTP. 3. Q: What is the life cycle of servlet? A: Life cycle of Servlet: Servlet class loading Servlet instantiation Initialization (call the init method) Request handling (call the service method) Removal from service (call the destroy method) 4. Q: Why do we need constructor in servlet if we use the init ()? A: Even though there is an init method in a servlet which gets called to initialize it, a constructor is still required to instantiate the servlet. Even though you as the developer would never need to explicitly call the servlet's constructor, it is still being used by the container. 5. Q: How servlet is loaded? A: The servlet is loaded by: First request is made. Server starts up (auto-load). There is only a single instance which answers all requests concurrently. This saves memory and allows a Servlet to easily manage persistent data. Administrator manually loads. 6. Q: When the servlet is unloaded? A: Servlet gets unloaded when: Server shuts down. Administrator manually unloads. 7. Q: What is servlet interface? A: The central abstraction in the Servlet API is the Servlet interface. All servlets implement this interface, either directly or more commonly by extending a class that implements it. 8. Q: What is the generic servlet class? A: GenericServlet is an abstract class that implements the Servlet interface and the ServletConfig interface. In addition to the methods declared in these two interfaces, this class also provides simple versions of the lifecycle methods init () and destroy (), and implements the log method declared in the ServletContext interface. 9. Q: What is the difference between GenericServlet and HttpServlet? A: The difference is:
  • 2.  The GenericServlet is an abstract class that is extended by HttpServlet to provide HTTP protocol-specific methods. But HttpServlet extends the GenericServlet base class and provides a framework for handling the HTTP protocol.  The GenericServlet does not include protocol-specific methods for handling request parameters, cookies, sessions and setting response headers. The HttpServlet subclass passes generic service method requests to the relevant doGet () or doPost () method.  GenericServlet is not specific to any protocol. HttpServlet only supports HTTP and HTTPS protocol. 10. Q: Why HttpServlet class is declared abstract? A: The HttpServlet class is declared abstract because the default implementations of the main service methods do nothing and must be overridden. This is a convenience implementation of the Servlet interface, which means that developers do not need to implement all service methods. If your servlet is required to handle doGet () requests for example, there is no need to write a doPost () method too. 11. Q: Can servlet have a constructor? A: Yes 12. Q: What are the type of protocols supported by the HttpServlet? A: It extends the GenericServlet base class and provides a framework for handling the HTTP protocol. So, HttpServlet only supports HTTP and HTTPS protocol. 13. Q: What is the difference between the doGet () and doPost ()? A: The difference is: In doGet() the parameters are appended to the URL and sent along with header information. In doPost (),send the information through a socket back to the webserver and it won't show up in the URL bar. The amount of information you can send back using a GET is restricted as URLs can only be 1024 characters. You can send much more information to the server by using post and it's not restricted to textual data either. It is possible to send files and even binary data such as serialized Java objects! DoGet() is a request for information.It does not change anything on the server. (doGet () should be idempotent). doPost () provides information (such as placing an order for merchandise) that the server is expected to remember. 14. Q: When to use doGet() and when doPost()? A:Always prefer to use GET (As because GET is faster than POST), except mentioned in the following reason: If data is sensitive. Data is greater than 1024 characters. If your application don't need bookmarks. 15. Q: How do I support both doGet () and doPost () from same servlet? A:The easy way is, just support POST, then have your doGet method call your doPost method.