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