1.What is the Servlet?

A servlet is a Java technology-based Web component. Managed by a web server or Application server (Tomcat, Web logic, WebSphere, JBoss..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?

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?

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?

Generic Servlets
Http Servlets
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 is a protocol independent  servlet.(for any protocol like FTP, SMTP etc.)
HttpServlet is a protocol dependent 
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?
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 life cycle method explicitly as a helper method.

6 . Can we write a default constructor for servlet ?
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?
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?
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 ().

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?

The web-container creates object for a servlet when one of the following situations occur


  •  When servlet gets first request from the browser.
  •  For first request given to servlet after restarting the server.
  • For first request given to servlet after restarting the web-application.
  • For first request given to servlet after reloading the web-application.
  • During server startup or when web-application is deployed. When is enabled.
11. When servlet object will be destroyed?

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
g. When garbage collector destroys the servlet object.

12. How can we create deadlock condition on our servlet?
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 ?

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 and response objects will be created in servlet?

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 .What is Difference Between ServletConfig and ServletContext ?


ServletConfig :

  1. ServletConfig available in javax.servlet.*; package
  2. ServletConfig object is one per servlet class
  3. destroyed once the servlet execution is completed.
  4. We should give request explicitly, in order to create ServletConfig object for the first time.
  5. Object of ServletConfig will be created during initialization process of the servlet .
  6. This Config object is public to a particular servlet only .
In web.xml – tag will be appear under tag

Example:

HelloServlet
com.Tutorial4u.HelloServlet

topic
hellotutorial4u@blogspot.com 

ServletContext :

  1. ServletContext available in javax.servlet.*; package
  2. ServletContext object is global to entire web application
  3. Object of ServletContext will be created at the time of web application deployment
  4. Scope: As long as web application is executing, ServletContext object will be available, 
  5. it will be destroyed once the application is removed from the server.
  6. .ServletContext object will be available even before giving the first request.

In web.xml – tag will be appear under tag

Example: 


website
tutorial-4ru.com

16.What is servlet Life Cycle ?



1.Loading Servlet Class : A Servlet class is loaded when first request for the servlet is received by the Web Container.

2.Servlet instance creation : After the Servlet class is loaded, Web Container creates the instance of it. Servlet instance is created only once in the life cycle.

3.Call to the init() method :
 init() method is called by the Web Container on servlet instance to initialize the servlet.

Signature of init() method : 
    

public void init(ServletConfig config) throws ServletException

4.Call to the service() method :
 The containers call the service() method each time the request for servlet is received. The service() method will then call the doGet() or doPost() methos based ont eh type of the HTTP request, as explained in previous lessons.

Signature of service() method : 


public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException


5.Call to destroy() method :  The Web Container call the destroy() method before removing servlet instance, giving it a chance for cleanup activity.

17.Can we call a servlet from another servlet?

Yes. We can call a servlet from another servlet this is known as inter servlet communication.By using RequestDispatcher object we can do this.

Syntax 


RequestDispatcher rd = request.getRequestDispatcher("servlet url name");
rd.forward(request, response);

18.What are the differences between forward() and sendRedirect() methods?
20.What are the differences between forward() and sendRedirect() methods?
21.How many scope in servlet?

18.What are the differences between doGet() and doPost()?



doGet()

                     doPost()
In doGet() method the parameters are attached to the URL and sent along with header information.
In doPost() method the parameters are attached in the body.
It does not provide data security.Not suitable for applying encoding , encription , security algorithms.
It provides data security. suitable for applying encoading, encriprion, security algorithms.
The doGet is faster than doPost.
It is slower compared to doGet.
Parameters are appended to the URL).
In doPost,the parameters are sent in separate line in the body.
 It can send little amount of data to the server(max of 256 kb).
It can send unlimited amount of data to the server.
It is idempotent (Able to be repeated safely many times).
Not idempotent.
 It allows bookmarks.
 It disallows bookmarks.


1 2 3

No comments:

Post a Comment

No of Occurrence in Array

package com.tutorial4u; import java.util.HashMap; /************************************************************  No of Occurrence in Array...