Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

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

LECTURE 5&6 - AIOOP23111


OBJECT ORIENTED PROGRAMMING 2 with JAVA
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 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

Web Container/Servlet engine

Java Classes Servlets


Browser Web Server
J2EE Container
Deployment Descriptor is
used to configure web
applications that are hosted Deployment Descriptor
in the App Server.
web.xml
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 Containers Are Provided by the App


Server
• Apache’s Tomcat @ Jakarta
• http://jakarta.apache.org/tomcat/index.html
• GlassFish
• ATG Dynamo
• Allaire/MacroMedia’s JRun
• IONA iPortal
• Sun’s Java Web Server * available with the J2EE SDK
• http://java.sun.com/docs/books/tutorial/servletrunner/index.ht
ml
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

Overview of the Servlet API


• All servlets implement the Servlet interface.
• The API provides two classes that implement
this interface, GenericServlet and
HttpServlet
• Servlet API is specified in two packages,
javax.servlet and javax.servlet.http
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

The doPost method


• Called by a sever to allow a servlet to handle a
post request
• It contains parameters HttpServeleRequest
• It also contains parameter
HttpServeleResponse
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

Proposed Architecture of Web Applications

Presentation Layer (JSP, HTML, WML)

Logic Layer
(Servlets, JavaBeans, EJBS, etc)

Data Store Layer


(MySQL, SQL Server, File System)
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

Example of a simple login form


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

Example of a simple login form


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

Example of a simple login form


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

Resources

• J2EE Developer’s Guide –


http://java.sun.com/j2ee/j2sdkee/techdocs/gui
des/ejb/html/DevGuideTOC.html
• J2EE Tutorial –
http://java.sun.com/j2ee/tutorial/1_3-fcs/index
.html
• Server Side Programming –
http://www.theserverside.com

You might also like