Servlet basic concepts
Servlet basic concepts
Servlets are a key component of Java EE (Enterprise Edition) used to build web applications.
They run on a web server or application server and handle client requests, typically via HTTP.
Here are some basic concepts:
1. What is a Servlet?
A Servlet is a Java class that extends the capabilities of servers hosting applications accessed by
request-response programming models. It essentially acts as a middle layer between requests
from web browsers or other HTTP clients and databases or applications on the HTTP server.
2. Servlet Lifecycle
The lifecycle of a servlet is managed by the servlet container (e.g., Apache Tomcat, Jetty) and
consists of several phases:
Loading and Instantiation: The servlet container loads the servlet class and creates an
instance.
Initialization (init method): Called once when the servlet is first loaded, used for one-
time setup.
Request Handling (service method): Called for each request. Delegates to doGet(),
doPost(), doPut(), etc., depending on the HTTP method.
Destruction (destroy method): Called before the servlet is taken out of service, used for
cleanup.
3. Servlet Methods
HttpSession: Interface to manage user sessions, allowing you to store and retrieve user
data across multiple requests.
An XML file where servlets are configured. It defines servlet mappings, initialization
parameters, filters, etc.
9. Filters
Filters can be used to intercept requests and responses, allowing preprocessing or modification:
JavaServer Pages (JSP) is a server-side technology used to create dynamic web pages using Java.
It simplifies web development by allowing developers to embed Java code directly into HTML
pages.
1. What is JSP?
JSP is an extension of Java Servlets that allows the creation of dynamic web content using a
combination of:
It is compiled into a Servlet internally by the JSP container, making it efficient and scalable.
2. JSP Lifecycle
3. JSP Directives
jsp
CopyEdit
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8" %>
jsp
CopyEdit
<%@ include file="header.jsp" %>
jsp
CopyEdit
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
jsp
CopyEdit
<%
String message = "Hello, JSP!";
out.println(message);
%>
jsp
CopyEdit
Hello, <%= "World!" %>
Object Description
request Represents the HttpServletRequest object (client request data).
response Represents the HttpServletResponse object (server response).
session Represents the HttpSession object (user session tracking).
application Represents the ServletContext (shared data for all users).
out Used to send output to the client.
config Provides servlet configuration data.
pageContext Provides access to page attributes.
page Refers to the current JSP page itself.
exception Used for exception handling in error pages.
Print output:
Conditional check:
Loop through data:
bash
CopyEdit
http://localhost:8080/your_project/index.jsp
HTML Basic concepts
1. What is HTML?
HTML (HyperText Markup Language) is the standard language for creating web pages. It
defines the structure of a webpage using elements and tags.
html
CopyEdit
<!DOCTYPE html> <!-- Declares HTML version (HTML5) -->
<html> <!-- Root element of the page -->
<head> <!-- Metadata, styles, and scripts -->
<title>Page Title</title> <!-- Title shown in browser tab -->
</head>
<body> <!-- Visible content of the page -->
<h1>Welcome</h1>
<p>This is a paragraph.</p>
</body>
</html>
Main Parts:
3. HTML Tags
Types of Tags:
C. Lists
D. Tables
5. HTML Attributes
Common Attributes:
6. HTML Comments
Tag Purpose
<header> Defines page header
<nav> Navigation links
<section> Defines a section
<article> Standalone content
<aside> Sidebar content
<footer> Footer section
8. HTML5 Features
Technology Purpose
HTML Structure of web pages
CSS Styling (colors, fonts, layout)
JavaScript Adds interactivity
Bootstrap Prebuilt responsive design
React, Angular JavaScript frameworks for dynamic apps
XML Basic concepts
1. What is XML?
XML (Extensible Markup Language) is a structured, text-based format used to store and
transport data. It is self-descriptive and allows custom tags, unlike HTML.
Example:
3. XML Structure
A basic XML document has:
1. XML Declaration
2. Root Element
3. Nested Elements
4. Attributes (optional)
5. Comments (optional)
Breakdown:
Rules:
6. XML Comments
7. XML Namespaces
1. What is CSS?
CSS (Cascading Style Sheets) is used to style and format HTML elements. It controls the
layout, colors, fonts, and spacing of web pages.
CSS Frameworks
Framework Purpose
Bootstrap Prebuilt responsive design
Tailwind CSS Utility-first framework
Materialize Google's Material Design
1. What is PHP?
PHP (Hypertext Preprocessor) is a server-side scripting language used for web development. It
is embedded in HTML and executed on the server before the page is sent to the browser.
✔ Runs on the server
✔ Generates dynamic content
✔ Works with databases (MySQL, PostgreSQL, etc.)
5. Servlet
Technology Purpose
HTML Defines page structure
CSS Styles & layouts the page
JavaScript Adds interactivity
XML Stores & transports data
Servlet Handles backend processing in Java
JSP Generates dynamic content using Java
What is HTML?
HTML is the standard language used to create and structure content on the web. It defines the
structure and semantics of a webpage. HTML uses tags to organize content, such as headings,
paragraphs, links, and multimedia.
What is XML?
XML is a markup language designed to store and transport data. Unlike HTML, it is not used
to display content but to structure and organize data in a readable and easily accessible format.
Self-descriptive tags.
Used for data interchange between systems (APIs, web services, etc.).
Platform-independent.
Can store complex and hierarchical data.
What is CSS?
CSS is a style sheet language used to describe the look and feel of a document written in
HTML or XML. It controls the layout, colors, fonts, and overall presentation of web pages.
4. Servlet
What is a Servlet?
A Servlet is a Java-based server-side technology used for handling client requests and
generating dynamic web content. It runs on the server and processes HTTP requests, typically
returning HTML, XML, or JSON responses.
What is JSP?
JSP is a Java-based technology that allows developers to embed Java code directly into HTML
pages. It helps create dynamic web content by combining the power of Java with HTML. JSP is
often used alongside Servlets in Java-based web applications.
6. JavaScript
What is JavaScript?
JavaScript is a client-side scripting language that enables dynamic behavior on web pages. It
allows developers to create interactive features like form validation, animations, and real-time
updates.