Professional Documents
Culture Documents
AOOP3111 - Lecture 5&6 - 2024 - Servlets
AOOP3111 - Lecture 5&6 - 2024 - Servlets
What is a Servlet?
• A servlet is a java class that extends an application
hosted on a web server.
• As a technology servlet provides a model of
communication between a web user request and the
application or program on the web server
• Handles the HTTP request-response process (for our
purposes)
• Often thought of as an applet that runs on a server.
• The foundation for Java Server Pages (JSP).
LECTURE 5 & 6 - SERVELETS AND JAVA SERVER PAGES
Associate Degree in Information Technology
AIOOP23111- OBJECT ORIENTED PROGRAMMING 2
Year 3_Semester: 05 From Jan 2024 to May 2024
Common Uses
• Processing and/or storing data submitted by an
HTML form.
• Providing dynamic content, e.g. returning the
results of a database query to the client.
• Managing state information on top of the
stateless HTTP, e.g. for an online shopping cart
system which manages shopping carts for many
concurrent customers and maps every request to
the right customer.
LECTURE 5 & 6 - SERVELETS AND JAVA SERVER PAGES
Associate Degree in Information Technology
AIOOP23111- OBJECT ORIENTED PROGRAMMING 2
Year 3_Semester: 05 From Jan 2024 to May 2024
Architectural Roles
• Servlets provide the middle tier in a three or
multi-tier system.
• Servlets provide logic and/or traffic control for
requests/response to the server.
• Servlets allow web developers to remove code
from the client and place the logic on the
server.
LECTURE 5 & 6 - SERVELETS AND JAVA SERVER PAGES
Associate Degree in Information Technology
AIOOP23111- OBJECT ORIENTED PROGRAMMING 2
Year 3_Semester: 05 From Jan 2024 to May 2024
Architectural Roles
• Connection management
• Business rule enforcement
• Transaction management
• Mapping clients to a redundant set of
servers
• Supporting different types of clients such
as pure HTML, WML clients, other Java
based clients
LECTURE 5 & 6 - SERVELETS AND JAVA SERVER PAGES
Associate Degree in Information Technology
AIOOP23111- OBJECT ORIENTED PROGRAMMING 2
Year 3_Semester: 05 From Jan 2024 to May 2024
What is required?
• Servlets are not run in the same sense as applets and
applications.
• Since they extend a web server, they require a servlet
container to function.
• A servlet container is a part of a web server or
application server that provides the network services
over which request and response are sent.
• Contains and manages the servlets through there
lifecycle.
• The container provides other services as well.
LECTURE 5 & 6 - SERVELETS AND JAVA SERVER PAGES
Associate Degree in Information Technology
AIOOP23111- OBJECT ORIENTED PROGRAMMING 2
Year 3_Semester: 05 From Jan 2024 to May 2024
doPost method
• The syntax for doPost method
Protected void doPost (HttpServeleRequest
request, HttpServletResponse response) throws
ServletException, IOException{
}
LECTURE 5 & 6 - SERVELETS AND JAVA SERVER PAGES
Associate Degree in Information Technology
AIOOP23111- OBJECT ORIENTED PROGRAMMING 2
Year 3_Semester: 05 From Jan 2024 to May 2024
HttpServletRequest &
HttpServletResponse
• Interfaces used for accessing request
parameters and constructing client responses.
• The web container has concrete objects which
implement these interfaces and passes into the
servlet.
LECTURE 5 & 6 - SERVELETS AND JAVA SERVER PAGES
Associate Degree in Information Technology
AIOOP23111- OBJECT ORIENTED PROGRAMMING 2
Year 3_Semester: 05 From Jan 2024 to May 2024
HttpServletRequest &
HttpServletResponse
• HttpServletRequest
• It let you get the incoming data from the user
• It is more of a form/query
• String getParameter(String)
• Enumeration getParameters(String[])
LECTURE 5 & 6 - SERVELETS AND JAVA SERVER PAGES
Associate Degree in Information Technology
AIOOP23111- OBJECT ORIENTED PROGRAMMING 2
Year 3_Semester: 05 From Jan 2024 to May 2024
HttpServletRequest &
HttpServletResponse
• HttpServletResponse
• Let you send content back to the client
• Writer getWriter()
• ServletOutputStream getOutputStream()
LECTURE 5 & 6 - SERVELETS AND JAVA SERVER PAGES
Associate Degree in Information Technology
AIOOP23111- OBJECT ORIENTED PROGRAMMING 2
Year 3_Semester: 05 From Jan 2024 to May 2024
IOException and
ServeletException
IOException
• It detects or handles an error from the input
and output request
ServeletException
• Detects errors if POST request is sent
LECTURE 5 & 6 - SERVELETS AND JAVA SERVER PAGES
Associate Degree in Information Technology
AIOOP23111- OBJECT ORIENTED PROGRAMMING 2
Year 3_Semester: 05 From Jan 2024 to May 2024
Class.forName
• It is a method in java used to dynamically load
a class into a Java Virtual Machine (JVM)
• It takes a class name as a parameter and
returns an instance of the class object that
represents the specified class
• The syntax of class.forName
Class.forName(“com.mysql.jdbc.Driver”);
LECTURE 5 & 6 - SERVELETS AND JAVA SERVER PAGES
Associate Degree in Information Technology
AIOOP23111- OBJECT ORIENTED PROGRAMMING 2
Year 3_Semester: 05 From Jan 2024 to May 2024
Logic Layer
(Servlets, JavaBeans, EJBS, etc)
Resources