Basic meaning of Servlet is Let Serv.
It a technology to receive a user's Request and Serv back a Response.
In java Servlet is an Interface.
It contains four methods declaration.
public interface Servlet{
void destroy();
ServletConfig getServletConfig();
java.lang.String getServletInfo();
void init(ServletConfig config);
void service(ServletRequest req, ServletResponse res);
}
Usually developers prepare servlet classes to receive HTTP request and send back HTTP Response.
Servlet works on the HTTP (Hyper Text Transfer Protocol) protocol.
The servlet interface contains method to Inititlize a servlet, to serv the request, and to provide back response.
and to destroy or remove servlet from the server.
To use this servlet interface or to implement this servlet interface, developer can prepare a class which extends the GenericServlet (javax.servlet.GenericServlet) class OR developer can extends the
HttpServlet(javax.servlet.http.HttpServlet) class.
All the servlet classes have their servlet life cycle.
First the Servlet is Initialized by the container using the init(ServletConfig config) method.
Then all the clients requests are handled using the service(ServletRequest req, ServletResponse res) method and finally servlet is destroyed by the container.
(Note: All the Servlet instances are destroyed when server is shutting down )
The init(ServletConfig config) method is called exactly once in the life cycle of a servlet. It is called by Container.
Before serving any request the servlet's init methos's execution must be successfully complete.
All servlets have their configuration information in the ServletConfig object.
ServletConfig object are servlet specific.
Servlet is initialized on the applications startup or on the first request received to that servlet.
There is always one instance of each servlet in webapp.
Developer cannot destroy servlet on their need base. Only container can destroy the servlet.
ServletConfig (Servlet Configuration)
public interface ServletConfig{
java.lang.String getInitParameter(java.lang.String name);
java.util.Enumeration<java.lang.String> getInitParameterNames();
ServletContext getServletContext();
java.lang.String getServletName();
}
ServletConfig is an Interface
When the Web Container initializes the Servlet then it container creates the ServletConfig object by reading servlet specific information from the web.xml file and passes the ServletConfig object to the Servlet's init method.
There is always one ServletConfig object per servlet instance.
Developer can get the ServletConfig object using the getServletConfig() method.
i.e.
ServletConfig sc = getServletConfig();
out.println(sc.getInitParameter("dbUrl"));
ServletConfig object provides the ServletContext object using the getServletContext() method.
It provides the Init Parameters values configured into web.xml file, using the getInitParameter(java.lang.String name) method.
It provides the Servlet Instance name using the getServletName() method.
It provides its own Init Parameters names using the getInitParameterNames() method.
ServletContext (Servlet Context)
ServletContext is an Interface
public interface ServletContext{
.......
java.lang.Object getAttribute(java.lang.String name);
java.util.Enumeration<java.lang.String> getAttributeNames();
java.lang.String getContextPath();
java.lang.String getInitParameter(java.lang.String name);
.......
}
It has lots of more methods.
There is one ServletContext object "per application" per JVM(Java Virtual Machine).
Container created the ServletContext object when it created the servlet config object.
It passed the ServletContext object to the servlet config. so developer can have the servlet context object using the ServletConfig object.
Developer can put all the application specific informtion into the servlet context object. So, the information will be accessible to whole application.
It means to share the information between multiple servlets in a web app, servlet context can be used.
It a technology to receive a user's Request and Serv back a Response.
In java Servlet is an Interface.
It contains four methods declaration.
public interface Servlet{
void destroy();
ServletConfig getServletConfig();
java.lang.String getServletInfo();
void init(ServletConfig config);
void service(ServletRequest req, ServletResponse res);
}
Usually developers prepare servlet classes to receive HTTP request and send back HTTP Response.
Servlet works on the HTTP (Hyper Text Transfer Protocol) protocol.
The servlet interface contains method to Inititlize a servlet, to serv the request, and to provide back response.
and to destroy or remove servlet from the server.
To use this servlet interface or to implement this servlet interface, developer can prepare a class which extends the GenericServlet (javax.servlet.GenericServlet) class OR developer can extends the
HttpServlet(javax.servlet.http.HttpServlet) class.
All the servlet classes have their servlet life cycle.
First the Servlet is Initialized by the container using the init(ServletConfig config) method.
Then all the clients requests are handled using the service(ServletRequest req, ServletResponse res) method and finally servlet is destroyed by the container.
(Note: All the Servlet instances are destroyed when server is shutting down )
The init(ServletConfig config) method is called exactly once in the life cycle of a servlet. It is called by Container.
Before serving any request the servlet's init methos's execution must be successfully complete.
All servlets have their configuration information in the ServletConfig object.
ServletConfig object are servlet specific.
Servlet is initialized on the applications startup or on the first request received to that servlet.
There is always one instance of each servlet in webapp.
Developer cannot destroy servlet on their need base. Only container can destroy the servlet.
ServletConfig (Servlet Configuration)
public interface ServletConfig{
java.lang.String getInitParameter(java.lang.String name);
java.util.Enumeration<java.lang.String> getInitParameterNames();
ServletContext getServletContext();
java.lang.String getServletName();
}
ServletConfig is an Interface
When the Web Container initializes the Servlet then it container creates the ServletConfig object by reading servlet specific information from the web.xml file and passes the ServletConfig object to the Servlet's init method.
There is always one ServletConfig object per servlet instance.
Developer can get the ServletConfig object using the getServletConfig() method.
i.e.
ServletConfig sc = getServletConfig();
out.println(sc.getInitParameter("dbUrl"));
ServletConfig object provides the ServletContext object using the getServletContext() method.
It provides the Init Parameters values configured into web.xml file, using the getInitParameter(java.lang.String name) method.
It provides the Servlet Instance name using the getServletName() method.
It provides its own Init Parameters names using the getInitParameterNames() method.
ServletContext (Servlet Context)
ServletContext is an Interface
public interface ServletContext{
.......
java.lang.Object getAttribute(java.lang.String name);
java.util.Enumeration<java.lang.String> getAttributeNames();
java.lang.String getContextPath();
java.lang.String getInitParameter(java.lang.String name);
.......
}
It has lots of more methods.
There is one ServletContext object "per application" per JVM(Java Virtual Machine).
Container created the ServletContext object when it created the servlet config object.
It passed the ServletContext object to the servlet config. so developer can have the servlet context object using the ServletConfig object.
Developer can put all the application specific informtion into the servlet context object. So, the information will be accessible to whole application.
It means to share the information between multiple servlets in a web app, servlet context can be used.