0% found this document useful (0 votes)
4 views

Servlets

Servlets are server-side Java programs that handle client requests to create dynamic web applications, operating independently of protocols like HTTP and FTP. The servlet life cycle involves loading the servlet class, creating an instance, invoking the init method, processing requests through the service method, and finally invoking the destroy method. Servlets can handle data transmission using GET and POST methods, with the POST method being more secure for sensitive information.
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)
4 views

Servlets

Servlets are server-side Java programs that handle client requests to create dynamic web applications, operating independently of protocols like HTTP and FTP. The servlet life cycle involves loading the servlet class, creating an instance, invoking the init method, processing requests through the service method, and finally invoking the destroy method. Servlets can handle data transmission using GET and POST methods, with the POST method being more secure for sensitive information.
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/ 22

Servlets Architecture

What is Servlet?

Servlet is one of the server-side programming language which


runs on Java enabled server.

Servlets are nothing but the Java programs which reside on the
server side and their main purpose is to serve the client request. Servlets are
used to develop dynamic web applications and they are platform
independent, robust, and secured.

Servlets are protocol independent. i.e. they support FTP, SMTP,


HTTP etc. protocols.
Servlets Architecture :
 Client i.e. web browser sends the request to the web server.
 Web server receives the request and sends it to the servlet container.
Servlet container is also called web container or servlet engine. It is
responsible for handling the life of a servlet.
 Servlet container understands the request’s URL and calls the particular
servlet. Actually, it creates a thread for execution of that servlet. If there
are multiple requests for the same servlet, then for each request, one
thread will be created.
 Servlet processes the request object and prepares response object after
interacting with the database or performing any other operations and
sends the response object back to the web server.
 Then web server sends the response back to the client.
The advantages of Servlet:

1. Better performance:
Because it creates a thread for each request, not
process.
2. Portability:
Because it uses Java language.
3. Robust:
JVM manages Servlets, so we don't need to worry
about the memory leak, garbage collection, etc.
4. Secure:
Because it uses java language.
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.
As displayed in the above diagram, there are three states of a servlet:
new, ready and end.
The servlet is in new state if servlet instance is created.
After invoking the init() method, Servlet comes in the ready state. In
the ready state, servlet performs all the tasks.
When the web container invokes the destroy() method, it shifts to the
end state.
1. Servlet class is loaded
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.
3. 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.
4. Service method is invoked:
If servlet is initialized, it calls the service method. Notice that servlet
is initialized only once.
5. 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.
Servlet Life Cycle Methods:
There are three life cycle methods of a Servlet :
1. init()
2. service()
3. destroy()
1. init() method:
The Servlet.init() method is called by the Servlet container to
indicate that this Servlet instance is instantiated successfully and is about to
put into service.
2. service() method:
The service() method of the Servlet is invoked to inform the Servlet
about the client requests.This method uses ServletRequest object to collect
the data requested by the client.
This method uses ServletResponse object to generate the output
content.
3. destroy() method:
The destroy() method runs only once during the lifetime of a
Servlet and signals the end of the Servlet instance.
As soon as the destroy() method is activated, the Servlet container
releases the Servlet instance.
Form GET and POST actions:

We must have come across many situations when you need to pass
some information from your browser to web server and ultimately to your
backend program.

The browser uses two methods to pass this information to web


server. These methods are GET Method and POST Method.
GET Method:

The GET method is the default method to pass information from


browser to web server and it produces a long string that appears in your
browser's Location:box.

Never use the GET method if you have password or other sensitive
information to pass to the server. The GET method has size limitation: only
1024 characters can be used in a request string.

This information is passed using QUERY_STRING header and will


be accessible through QUERY_STRING environment variable and Servlet
handles this type of requests using doGet() method.
GET method type and doGet() method
The doGet() method in servlets is used to process the HTTP GET
requests. So, basically, the HTTP GET method should be used to get the
data from the server to the browser.

Although in some requests, the GET method is used to send data


from the browser to the server also. In this case, you need to understand the
below points on how the GET method will work.
The data that is being submitted to the server will be visible in the
URL using query parameters like this
“http://localhost:8080/HelloServlet/hello?myParam=myValue”.
So, if you are sending any sensitive information like passwords,
you should not use the GET method as the data entered can be clearly
visible in the browser URL.
POST Method:

A generally more reliable method of passing information to a


backend program is the POST method.

This packages the information in exactly the same way as GET


method, but instead of sending it as a text string after a ? (question mark)
in the URL it sends it as a separate message.

This message comes to the backend program in the form of the


standard input which you can parse and use for your processing. Servlet
handles this type of requests using doPost() method.
POST method type and doPost() method
The doPost() method in servlets is used to process the HTTP
POST requests. It is used to submit the data from the browser to the server
for processing.

The data submitted with POST method type is sent in the message
body so it is secure and cannot be seen in the URL. And there is no limit
on the data that can be sent through the POST method.

We will be using the doPost() method in this example. we will


learn how to handle some of the common HTML fields data such as Text
field, Checkbox, Radio button, Dropdown, etc., values in the servlet.

To achieve this, Java provides ServletRequest interface.


HttpServletRequest(I):

Http ServletRequest interface extends the ServletRequest


interface to provide the Http-specific request information for Servlets.

The servlet container creates an HttpServletRequest object and


passes it as an argument to the servlet’s service methods – doPost(),
doGet(), etc.,

The object provides data like parameter name and values,


attributes, and an input stream. And it has various methods to work with
the client data.

In this example, we will use getParameter()


and getParameterValues() to read values of the form fields.
getParameter() method:

The method returns the value of a field/parameter from the request


which is specified by the given name, as a String.

If the specified parameter name does not exist, it returns null.

This method should be used on the parameter that has only one
value. If multiple values are there, then it returns only the first value.

You might also like