Unit-2 Java Servlets
Unit-2 Java Servlets
steps:
1. The Clients send the request to the Web Server.
2. The Web Server receives the request.
3. The Web Server passes the request to the
corresponding servlet.
4. The Servlet processes the request and generates
the response in the form of output.
5. The Servlet sends the response back to the
webserver.
CGI(Common Gateway Interface)
CGI is actually an external application that is written by
Execution of Servlets basically involves Six basic steps:
technology:
• If the number of clients increases, it
takes more time for sending the
response.
• For each request, it starts a process,
and the web server is limited to start
processes.
Advantages of Servlet
The advantages of Servlet are as follows:
Execution of Servlets basically involves Six basic steps:
applications.
• It manages the lifecycle of a Servlet.
• It handles authorization and authentication of
resource access.
• It provides the service of decoding and encoding
MIME-based messages.
Servlet Life Cycle
Execution of Servlets basically involves Six basic steps:
The web container maintains the life cycle of a servlet
instance. Let's see the life cycle of the servlet:
1. Servlet class is loaded
The classloader is responsible to load the servlet class.
The servlet class is loaded when the first request for the
servlet is received by the web container.
2. Servlet instance is created
The web container creates the instance of a servlet after
loading the servlet class. The servlet instance is created
only once in the servlet life cycle.
Servlet Life Cycle
3. init method is invoked
Execution of Servlets basically involves Six basic steps:
The web container calls the service method each time when
request for the servlet is received. If servlet is not initialized,
it follows the first three steps as described above then calls
the service method. If servlet is initialized, it calls the service
method. Notice that servlet is initialized only once. The
syntax of the service method of the Servlet interface is given
below:
public void service(ServletRequest request,
ServletResponse response)
throws ServletException, IOException
Servlet Life Cycle
5.Execution
destroy method is invoked
of Servlets basically involves Six basic steps:
@WebServlet("/example")
public class ExampleServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException {
response.getWriter().println("Hello, Servlet!");
}
}
ServletConfig Interface
• An object of ServletConfig is created by the web
container for each servlet. This object can be used to get
configuration information from web.xml file.
• The core advantage of ServletConfig is that you don't
need to edit the servlet file if information is modified
from the web.xml file.
Syntax:-
public ServletConfig getServletConfig();
ServletContext Interface
• An object of ServletContext is created by the web
container at time of deploying the project. This object
can be used to get configuration information from web.
xml file. There is only one ServletContext object per web
application.
• It is easy to maintain if any information is shared to all
the servlet, it is better to make it available for all the
servlet. We provide this information from the web.xml
file, so if the information is changed, we don't need to
modify the servlet.
• getServletContext() method of ServletConfig interface
returns the object of ServletContext.
RequestDispatcher Interface