===========================================
1.What is the Servlet?
Ans :
A servlet is a Java technology-based Web component. Managed by a web server or Application server (Tomcat, Weblogic, Glashifish..etc).
Servlet generates dynamic content and interacts with web clients(browsers) via a request and response programming model.
so we call servlets are server side components.
servlets are not designed for a specific protocols. But mostly it works on HTTP based protocol. Therefore, the word "servlet" is often used in the meaning of "HTTP Servlet".
2 : How many types of servlets are there?
Ans :
Saying there are two types of servlet
1.GenericServlet
2.HttpServlet is always wrong answer.
There is a possibility of developing ‘n’ types of servlet like HTTP protocol base servlet, FTP protocol base servlets, SMTP protocol base servlets etc.
Since all the web-servers & Application servers available in the market are coming as HTTP protocol based servers. so the programmer prefers to develop their servlets as HTTP servlets.
The javax.servlet.http.HttpServlet class is designed based on HTTP protocol Standards.
3. How a servlet is executing without main() method?
Ans.
If you give a java class directly to JVM/JRE for execution then JVM/JRE expects public static void main (strong args[]) to start the execution. But we are not giving our web application directly to the JVM/JRE. We are hosting our web application in web Container. This web container is already containing main(). So this main() will be execute.
Conclusion : web-server/web-container is also a java application that also contains main() method, so we no need to keep main() in our web application.
4 : What is the difference between GenericServlet and HttpServlet?
Ans :
| GenericServlet | HttpServlet |
| Signature: public abstract class GenericServlet extends java.lang.Object implements Servlet, ServletConfig, java.io.Serializable | Signature: public abstract class HttpServlet extends GenericServlet implements java.io.Serializable |
| GenericServlet defines a generic, protocol-independent servlet.(for any protocol like FTP, SMTP etc.) | HttpServlet defines a HTTP protocol specific servlet |
| In GenericServlets you cannot use Cookies or HttpSession, Session tracking is not possible is not possible. | These all are possible in HttpServlet . |
| Generic servlet overrides service method | Http servlets overrides doGet and doPost methods |
| To write a generic servlet, you need only override the abstract service method. | To write a http servlet, you need only override the any doXXX(-,-) or service(-,-) method. |
5. Can I keep main() method in our servlet class?
Ans:
You can place but it never executes automatically .because it is not a life cycle method.
You can see main() method acting as helper method(normal method) to life cycle methods.
you can call this main() from any lifecycle method explicitly as a helper method.
6 : Can we write a default constructor for servlet ?
Ans :
Yes.But not recommended.
Container by default calls the default constructor. If we don't write container will create default constructor.
7 :Can we place both parameterized and zero Argument constructor in my servlet?
Ans :
Yes you can ,
In a servlet class you can place parameterized constructor along with user-defined zero argument constructor but these parameterized constructors never executes.
8 :What is the difference between code placed in the constructor and code placed in the init() method?
Ans :
To read init parameters of a servlet we need ServletConfig object. But this ServletConfig object is created after the execution of constructor and before execution of init() method.
So code placed in the constructor cannot read init parameters because ServletConfig object is not available. But code placed in the init() can read init parameters because ServletConfig object is available in the init().
9: For initializing a servlet can we use constructor in place of init ().
Ans:
No, Because ServletConfig object is not available in constructor.
(by using ServletConfig only we can read init-parameters from web.xml)
10. When servlet object will be created?
Ans .
The web-container creates object for a servlet when one of the following situations occur
a. When servlet gets first request from the browser.
b. For first request given to servlet after restarting the server.
c. For first request given to servlet after restarting the web-application.
d. For first request given to servlet after reloading the web-application.
e. During server startup or when web-application is deployed. When<load-on-startup> is enabled.
11. When servlet object will be destroyed?
Ans.
The web-container destroys servlet object when one of the following situations occur.
a. When you shutdown the server
b. When server is crashed
c. When you stop the running web-application.
d. When web-application is reloaded.
f. When web-application is undeployed
f. When garbage collector destroys the servlet object.
12: How can we create deadlock condition on our servlet?
Ans:
one simple way to call doPost() method inside doGet() and doGet()method inside doPost() it will create deadlock situation for a servlet. This is rather simple servlet interview questions but yet tricky if you don’t think of it.
13 : Who will create Servlet Object ?
Ans :
WebServer Or Application server
Description : Creating servlet object managing servlet object by calling life cycle methods, processing request & destroying servlet object is the responsibility of the underlying “web-server/application server”.
Programmer just writes the logic he wants to execute by taking the support of life cycle methods.
14. When request & response objects will be created in servlet?
Ans.
For every new request coming to servlet a separate request & response objects will be created by “web server” before executing the service() method these two objects are visible in the service() method once request related response goes to browser request,response objects of that HttpServlet will be destroyed.
Note : If 100 requests are given to a servlet 100 pairs of request & response objects will be created & will be destroyed at the end of request processing & response generation.
15 : how to develop our servlet as the best HttpServlet?
Ans :
Step 1:
Keep programmers initialization logic [like getting DB connection] by overriding public void init() method.
If programmers initialization logic is placed by overriding public void init(servltconfig eg) make sure that you are calling super.init(eg) method from it .
Step 2:
Instead of placing request processing logic in the service() method it is recommended to place request processing logic by overriding doxxx() methods like doGet(),doPost() method. Because these are developed based on HTTP protocol and provides error messages .
Step 3:
Close All opened resources (Jdbc connections , File closing,network connections) in destroy().
===========================================



good job
ReplyDeletethese all FAQ's are Nataraz sir servlet class noteS.. COOL any way nice work.
ReplyDeleteyes 50% natraj and 50 % duragasoft SCWCD questions good work sir users can easily understand classroom notes plz try to upload corejava , jsp , struts,spring notes.
ReplyDeletenice one
ReplyDeletegood work
ReplyDeleteYES IT WAS 80% NATARAJ SIR FAQ QUESION,IT IS GOOD JOB TO UP0DATE BECAUSE OF THE IT IS USEFUL TO MANY CANDIDATES TO ATTEND INTERVIEW PURPOSE...Thanks to Natraz sir.
ReplyDeletenice thanku sir please update it for struts spring hibernate and webservices also
ReplyDeletegood
ReplyDeletenatraj notes wont contain depth subject...........surely its 100% mr.Durga notes.......
ReplyDeletenatraj wont teach core java my dear
Deletedear once u listen natraz sir .....and u decide...natraz sir is the legend of java....
Deletethanks myjavahub
ReplyDeletethanks my javahub.........
ReplyDeleteMyeclipse Shortcuts
thanks myjavahub
ReplyDeleteCore Java Interview Questions
the above facts are in natraj sir srvlet notes
ReplyDeletethanks to javahub
ReplyDeletethanks to chamu
ReplyDeletesome good tutorials on java
ReplyDeletehttp://sandeeppateltechblogs.blogspot.in/
Thanks THANKS JavaHub.PLEASE Provide COREJAVA,ADV JAVA,STRUTS,HIBERNATE,SPRING,WEBSERVICES,DESIGNPATTERNS OF NATARAZ AND DURGASOFT NOTES AND FAQS .IT WILL BE HELPFUL TO JAVA SEEKERS ON EXPERIENCE
ReplyDeletethis enough we want some more additonal questions
ReplyDeleteThis comment has been removed by the author.
ReplyDeletegood job, this is purely Nataraz sir faqs
ReplyDeletei clear my doubts thanks to myjavahub
ReplyDeleteCould u post core java quations also
ReplyDeleteNATARAJ sir previously degree college faculty,not having s/w work experience
ReplyDeleteya all r know that one
DeleteNATARAZ sir leaving from satya ,so no new Batches now
ReplyDeletestr we looking for core java,advanced java faqs
ReplyDeletefully natraz sir notes....any way good...this is very helpfull to us
ReplyDeletesir plz upload your core java notes..
ReplyDeleteyou can find all the java meterial on Myjavahub
ReplyDeletewww.myjavahub.in