0% found this document useful (0 votes)
12 views8 pages

Servlets Intro

Java servlets are server-side programs that handle client requests and provide dynamic responses, typically using the HTTP protocol. They offer advantages over earlier technologies like CGI by being platform-independent, scalable, and efficient in resource sharing. Servlets are integral to Java server-side technology and can be created by extending the HttpServlet class, with a defined lifecycle managed by a servlet container.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views8 pages

Servlets Intro

Java servlets are server-side programs that handle client requests and provide dynamic responses, typically using the HTTP protocol. They offer advantages over earlier technologies like CGI by being platform-independent, scalable, and efficient in resource sharing. Servlets are integral to Java server-side technology and can be created by extending the HttpServlet class, with a defined lifecycle managed by a servlet container.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Unit III

Introduction to Servlets

--Java servlets are server-side programs (run inside a web server)


that handle clients' requests and return dynamic or customized
responses for each request.

-- The dynamic response could be based on user's input (e.g., search,


online shopping, online transaction) with data retrieved from
databases or other applications.

-- Java servlets typically run on the HTTP protocol.

-- HTTP is an asymmetrical request-response protocol. The client


sends a request message to the server, and the server returns a
response message.

-- The message consists of two parts: header (information about the


message) and body (contents).
Need for dynamic content
-- Applets, one of the earliest attempts to generate dynamic content,
focused on using the client platform to deliver dynamic user
experiences. At the same time, developers also investigated using
the server platform for the same purpose.

-- Initially, Common Gateway Interface (CGI) server-side scripts


were the main technology used to generate dynamic content.

-- CGI scripting technology had many shortcomings, including


platform dependence and lack of scalability.
To address these limitations, Java Servlet technology was created
as a portable way to provide dynamic, user-oriented content.

-- There are many (competing) server-side technologies available:


Java-based (servlet, JSP, JSF, Struts, Spring, Hibernate), ASP, PHP,
CGI Script, and many others.

-- Java servlet is the foundation of the Java server-side technology.

-- JSP, JSF, Struts, Spring, Hibernate, and others, are extensions of


the servlet technology.

-- Dynamic content means : user will ask for particular details in the
database, upon that server will process user request and response
appropriately by handing user required data from database.
Eg. Online Banking Systems, Online Reservation System.
Java Servlet technology

1. A Servlet is a server-side program which services http requests


and returns http response.

2. Servlet is a non visual applet which runs on a server, it has a


similar lifecycle as an applet.

3. Servlets are most commonly used with http hence also known as
http Servlets.
5. Supported by virtually all web servers and web browsers.

6. Executes all requests in the form of threads of a single process,


hence increasing performance.

7. Servlets can easily share resources unlike CGI.

8. Servlets can be easily ported along platforms which support java,


this is because Servlets run inside JVM. hence they are platform
independent.

9. When requesting dynamic content from a web server, a user


request is handed to Servlets for processing upon which Servlets
give relevant responses in the form of a web page only.

10. Most Java servlets are designed to respond to HTTP requests in


the context of a Web application.

11. When you create a Java servlet, you typically subclass


HttpServlet. This class has methods that give you access to the
request and response wrappers you can use to handle requests and
create responses.

12. When a user issues a request via a URL, the Java servlet classes
convert it to an HttpServletRequest and send it to the target pointed
to by the URL.
13. When the server side has done its work, the JRE put the results
in an HttpServletResponse and then sends it back to the client.

15. A container manages the runtime environment for servlets.

Servlet Container
Servlet container or Servlet Engine provide an execution
environment for Servlets and manage Servlets life cycle from
creation to destroy phase.

Why servlet?
Servlets are loaded into memory once and they can run from
memory thereafter.

Servlets are powerful object Oriented abstraction for HTTP.

Servlets are portable across multiple web servers and platforms.

Servlets are simple in design and implement.

Servlet tightly integrated with web servers.

Servlets efficiently services client request.

Servlets run within JVM which is why they are secure.

Servlets obey the rule of standard API.

Servlets processes request and returns HTML.


Servlets are good replacement to CGI due to their robustness and
scalability.

Servlets supported by various server.

Since they reside in server they provide good code security.

Simple Java Servlet


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// Extend HttpServlet class


public class HelloWorld extends HttpServlet
{
private String message;

public void init() throws ServletException


{
message = "Hello World";
}

public void doGet(HttpServletRequest request,


HttpServletResponse response) throws ServletException,
IOException
{
// Set response content type
response.setContentType("text/html");

PrintWriter out = response.getWriter();


out.println("<h1>" + message + "</h1>");
}
}

The servlet example can be created by 3 ways:


● By implementing Servlet interface,
● By inheriting GenericServlet class, (or)
● By inheriting HttpServlet class
The most commonly used approach is by extending HttpServlet
because it provides http request specific methods such as doGet(),
doPost(), doHead() etc.

Steps:
1. Create Servlet
2. Compile servlet
3. Create deployment descriptor (web.xml)
Web Container gets the information about the servlet to be
invoked.
<web-app> represents the whole application.
<servlet> is sub element of <web-app> and represents the servlet.
<servlet-name> is sub element of <servlet> represents the name of the servlet.
<servlet-class> is sub element of <servlet> represents the class of the servlet.
<servlet-mapping> is sub element of <web-app>. It is used to map the servlet.
<url-pattern> is sub element of <servlet-mapping>. This pattern is used at
client side to invoke the servlet.

4. Start the Server and deploy the project

You might also like