0% found this document useful (0 votes)
6 views26 pages

Java Servlets

The document provides an overview of Java Servlets, detailing their role in server-side programming to handle client requests and generate dynamic web content. It explains the servlet life cycle, including the init, service, and destroy methods, as well as session management through HttpSession and cookies. Additionally, it covers URL rewriting for session tracking when cookies are disabled and introduces basic XML concepts for data structuring.

Uploaded by

Karthikeyini S
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)
6 views26 pages

Java Servlets

The document provides an overview of Java Servlets, detailing their role in server-side programming to handle client requests and generate dynamic web content. It explains the servlet life cycle, including the init, service, and destroy methods, as well as session management through HttpSession and cookies. Additionally, it covers URL rewriting for session tracking when cookies are disabled and introduces basic XML concepts for data structuring.

Uploaded by

Karthikeyini S
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/ 26

Server-Side Programming: Java

Servlets
Java Servlets, Sessions, Cookies &
XML Fundamentals
Presented by: S. Karthikeyini
Server-Side Programming: Java Servlets

Server-Side Programming: Java Servlets


Java Servlets are Java programs that run on a server, handle client requests
(typically from a web browser), and generate dynamic web content (like
HTML pages).
Example:
A servlet to display "Hello, User!"
public class HelloServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.getWriter().println("Hello, User!");
}
}
Servlet Life Cycle

• The servlet life cycle is managed by the servlet


container (like Tomcat), and it has 3 main methods:
• init() – Called once when the servlet is first created.
• service() – Called for every client request.
• destroy() – Called once before the servlet is
removed.
• Flow: init() → service() → destroy()
• The lifecycle of a Servlet is managed by the Servlet
Container (like Tomcat) and involves these phases:
• Loading and Instantiation
– Servlet class is loaded and instantiated.
• Initialization (init())
– Called only once when the servlet is first loaded.
• Request Handling (service())
– Called for every client request.
• Destruction (destroy())
– Called when the servlet is taken out of service.
Example
Servlet Lifecycle Methods in the Example
• init()
– Called once when the servlet is first loaded.
– You typically use it to initialize resources (like DB
connections, config values, etc.).
Output here:
Servlet is initialized
destroy()
• Called once when the servlet is being
unloaded (e.g., when the server shuts down).
• Used for cleanup (e.g., closing DB
connections).
Output here:
• Servlet is destroyed
doGet()
• Handles HTTP GET requests.
• This method is invoked every time a client
sends a GET request to the servlet.
• The response.getWriter().println() sends
output to the browser.
Output shown in browser:
• Hello from servlet
Important Notes
• The class extends HttpServlet, which is part of
the javax.servlet.http package.
• The doGet() method overrides the HttpServlet
method to respond to HTTP GET requests.
• Exceptions: IOException is handled for output
operations.
Parameter Data

• You can send data to a servlet using GET or


POST requests.
GET or POST
Session
• HttpSession allows data to be stored for a particular
user across multiple requests.
➤ Example:
HTTP Session
A session is used to maintain user-specific data across
multiple HTTP requests, even though HTTP is stateless
by nature.
Cookies

Cookies are small pieces of data stored on the client-side. Cookies are small
pieces of text information sent by the server to the client (browser) and
stored there. On every subsequent request to the same server, the browser
sends those cookies back.
Explanation
Real-Life Analogy

• Imagine you visit a hotel (web server), and the


receptionist (server) gives you a token (cookie)
with your name on it. The next time you enter,
you show your token, and they greet you by
name.
URL Rewriting
URL Rewriting
• Used when cookies are disabled to maintain session by
appending session ID to the URL.
• ➤ Example:
• String url = response.encodeURL("profile.jsp");
URL Rewriting
• A session ID is stored in a cookie.
• This session ID helps the server recognize the
user across different requests.
If cookies are disabled, the server can’t store
the session ID in the browser.
• So we use a technique called URL Rewriting.
URL Rewriting
URL Rewriting:
• It means adding the session ID directly to the URL.
Example:
String url = response.encodeURL("profile.jsp");
This method:
• Automatically appends the session ID to the URL only if cookies are
disabled.
• If cookies are enabled, it just returns the original URL.
For example, it might return:
• profile.jsp;jsessionid=ABCD1234XYZ
• This allows the server to track the session using the session ID in
the URL.
URL Rewriting

• response.encodeURL("yourPage.jsp") helps
maintain sessions without cookies.
• It’s a built-in solution in servlets for URL-based
session tracking.
• Helps maintain user state even when the
browser blocks cookies.
• Let me know if you want a visual flow or a full
servlet example using this!
XML Basics

XML Basics
Structuring Data
• XML is used to store and transport data in a
structured format.
Example:
<student> <name>Alice</name>
<age>22</age></student>
XML Schema Documents (XSD)

• XSD is used to define the structure and data


types of an XML document.
XML Namespaces
• Used to avoid element name conflicts when
combining XML documents.
Example:
<book xmlns:ns1="http://example.com/ns1">
<ns1:title>XML Guide</ns1:title></book>
Summary
Q&A
• Thank you!
• Any questions or discussions?

You might also like