0% found this document useful (0 votes)
9 views10 pages

Module 4

CLOUD COMPUTING

Uploaded by

Addds Muhammmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

Module 4

CLOUD COMPUTING

Uploaded by

Addds Muhammmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Module 4

Servlets
Servlets
• Java Servlet is a Java program that runs on a Java-enabled web server
or application server.
• It handles client requests, processes them, and generates responses
dynamically.
• Servlets are the backbone of many server-side Java applications due to
their efficiency and scalability.
• Servlets work on the server side.

• Servlets are capable of handling complex requests obtained from the


web server.
• Generate dynamic responses efficiently.
Creating a Basic Servlet
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class basic extends HttpServlet


{
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body><h1>Hello, World!</h1></body></html>");
}
}
Java Servlets Architecture
Execution of Java Servlets
• Execution of Servlets basically involves Six basic steps:
• The Clients send the request to the Web Server.
• The Web Server receives the request.
• The Web Server passes the request to the corresponding servlet.
• The Servlet processes the request and generates the response in the
form of output.
• The Servlet sends the response back to the webserver.
• The Web Server sends the response back to the client and the client
browser displays it on the screen.
Life Cycle of a Servlet (Servlet Life Cycle)

• 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.
2.Servlet instance is created.
3.init method is invoked.
4.service method is invoked.
5.destroy method is invoked.
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.
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.
init method is invoked
• The web container calls the init method only once after creating the
servlet instance. The init method is used to initialize the servlet. It is
the life cycle method of the javax.servlet.Servlet interface.
• public void init(ServletConfig config) throws ServletException
service method is invoked
• 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.
Syntax
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException
destroy method is invoked
• The web container calls the destroy method before removing the servlet
instance from the service. It gives the servlet an opportunity to clean up any
resource for example memory, thread etc.
Syntax
public void destroy()
A sim p leS erv letP ro g ram

import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class DemoServlet extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
res.setContentType("text/html");//setting the content type
PrintWriter pw=res.getWriter();//get the stream to write the data
//writing html in the stream
pw.println("<html><body>");
pw.println("Welcome to servlet");
pw.println("</body></html>");
pw.close();//closing the stream
}}

You might also like