Server-Side Programming: Java
Servlets
Java Servlets, Sessions, Cookies &
XML Fundamentals
Presented by: S. Karthikeyini
What is a Java Servlet?
• A Java Servlet is a server-side program that
handles requests and responses.
• It extends the capabilities of a server by
generating dynamic content.
• Servlets are managed by a servlet container
like Apache Tomcat.
What is a Servlet Container
• A Servlet Container (also called a Web
Container) is a part of a web server or
application server that:
• Manages the lifecycle of servlets
• Handles HTTP requests and responses
• Provides a runtime environment for Java web
applications (like servlets, JSPs)
Popular Servlet Containers:
Popular Servlet Containers:
• Apache Tomcat (most widely used)
• Jetty
• GlassFish
• WildFly (JBoss)
Introduction to Java 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.
Key Features:
• Servlets work on the server side.
• Servlets are capable of handling complex requests obtained
from the web server.
• Generate dynamic responses efficiently.
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.
Revisiting Java
• Java is a robust, platform-independent
language used in enterprise and web
applications.
• Java EE (Java Platform, Enterprise Edition)
enables server-side development using
technologies like Servlets and JSPs.
• Servlets are Java programs that run on a
server to handle client requests.
Servlet Lifecycle
• 1. Loading and Instantiation: Servlet class is
loaded and instance is created.
• 2. Initialization (init()): Called once when the
servlet is first created.
• 3. Request Handling (service()): Called for each
client request.
• 4. Destruction (destroy()): Called once when
the servlet is taken out of service.
Servlet Lifecycle Methods
init(): This method itializes the Servlet instance.
service(): This method Processes requests and invokes
either doGet() and doPost() based on the request type.
destroy(): This method cleans up resources when the
servlet is terminated.
Servlet Lifecycle Methods
• - init(ServletConfig config): Initializes the
servlet.
• - service(HttpServletRequest req,
HttpServletResponse res): Handles requests.
• - destroy(): Cleans up resources before the
servlet is destroyed.
Parameter Data in Servlets
• - Retrieve data using
request.getParameter("param") for form
submissions.
• - Use getParameterValues() for multiple
selections (e.g., checkboxes).
• - Commonly used in doGet() and doPost()
methods.
Managing Sessions
• HttpSession helps maintain user-specific data
across multiple requests.
• session.setAttribute("key", value) stores data.
• session.getAttribute("key") retrieves data.
• - Sessions can be invalidated using
session.invalidate().
Cookies in Java Servlets
• - Cookies are small data files stored on the
client.
• - Created using new Cookie("name", "value").
• - Added to response using
response.addCookie().
• - Read cookies using request.getCookies().
URL Rewriting
• - Used to maintain session state by appending
data to URL.
• - Useful when cookies are disabled.
• - Example: response.sendRedirect("page.jsp?
user=John");
XML Basics
• - XML stands for eXtensible Markup Language.
• - Used for storing and transporting data.
• - Platform and language independent.
• - Custom tags can be created by users.
Structuring Data in XML
• - XML is hierarchical and self-descriptive.
• - Example:
• <student>
• <name>John</name>
• <roll>101</roll>
• </student>
• - Tags must be properly nested and closed.
XML Namespaces
• - Used to avoid tag conflicts in XML
documents.
• - Defined using xmlns attribute with a unique
URI.
• - Example:
• <ns1:student
xmlns:ns1="http://example.com/students">
W3C XML Schema (XSD)
• - Defines structure and constraints of XML
documents.
• - Supports data types and validation.
• - Example:
• <xs:element name="roll" type="xs:int"/>
XSD
• XSD stands for XML Schema Definition. It is a
way to define the structure, content, and
data types of XML documents.
• While XML is used to store and transport data,
it doesn't enforce rules. That’s where XSD
comes in — to validate that an XML document
is well-formed and follows certain rules.
Key Features of XSD
Feature
:
Description
Specifies what elements and attributes
Defines structure
are allowed, and in what order
You can define types like xs:string,
Enforces data types
xs:integer, xs:date, etc.
Avoids conflicts between elements from
Supports namespaces
different XML vocabularies
Allows creation of your own types (like
Custom data types
enums, restrictions, patterns)
Ensures XML documents are correct
Validates documents
before processing them in applications
Summary
• - Java Servlets are key to server-side Java
programming.
• - Lifecycle includes init, service, and destroy
methods.
• - Sessions and cookies manage user data.
• - XML and XSD ensure structured and
validated data exchange.
Q&A
• Thank you!
• Any questions or discussions?