Web Technology (BCS502)
Unit-5 Servlets & JSP
Edushine Classes
Follow Us
Free Notes Available on : https://telegram.me/rrsimtclasses
https://telegram.me/rrsimtclasses/
Web Technology (BCS502)
Servlet Overview –
Before Discussing about servlet let understand first What is Software Technology –
Software technology are non-installable S/W,but the S/W developed by S/W
technology are installable. JSP,Servelet, JDBC are S/W technology.
Overview About Servlet -
Servlet are simple Java programs that run on the server.
Servlets are most commonly used with HTTP, hence it is also called HTTP Servlet.
Servlet technology is used to create web applications (resides at server-side and
generates a dynamic web page).
Servlet technology is robust and scalable because of the Java language.
Before Servlet, CGI (Common Gateway Interface) scripting language was common as a
server-side programming language.
RRSIMT CLASSES WHATSAPP
Free Notes - 9795358008
Available Follow Us
on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
There are many interfaces and classes in the Servlet API such as Servlet,
GenericServlet, HttpServlet, ServletRequest, ServletResponse, etc.
The javax.servlet and javax.servlet.http packages represent interfaces and classes for
servlets.
RRSIMT CLASSES WHATSAPP
Free Notes - 9795358008
Available Follow Us
on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
What is Servlet – (V.V.VIMP)
A Servlet in Java is a program that runs on a server and handles requests from clients,
such as web browsers. Servlets are used to build dynamic web applications, like websites
or APIs.
Think of it like this:
When you type a URL in your browser and hit "Enter," the server (where the website
lives) receives your request. A Servlet processes this request, performs some tasks (like
fetching data from a database), and sends the response back to the browser.
Key Features of Servlets:
• Runs on a Server: Servlets are part of the server-side programming.
• Handles Requests and Responses: It takes client requests (like clicking a button or
entering data in a form), processes them, and sends back a response.
• Java-Based: Servlets are written in Java, making them portable, secure, and efficient.
RRSIMT CLASSES WHATSAPP
Free Notes - 9795358008
Available Follow Us
on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
• Used in Web Applications: They help create dynamic content like user
dashboards, search results, etc.
Servlet Architecture -
RRSIMT CLASSES WHATSAPP
Free Notes - 9795358008
Available Follow Us
on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
1. Web Browser:
The user interacts with a web browser (like Chrome, Firefox, or Edge).
The user sends a request to a web application by entering a URL or clicking a link. This is
known as an HTTP Request.
2. Web Server:
The Web Server receives the HTTP request from the browser.
It is responsible for handling the request and passing it to the appropriate Servlet.
3. Servlet Container:
The Servlet Container (also known as a Servlet Engine like Apache Tomcat) manages the
lifecycle of Servlets.
The web server forwards the request to the Servlet Container.
RRSIMT CLASSES WHATSAPP
Free Notes - 9795358008
Available Follow Us
on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
4. Servlet:
A Servlet is a Java program that handles the request.
The Servlet processes the request, often interacting with a Database to retrieve or store
information.
After processing, the Servlet generates a response, usually in the form of HTML.
5. Database:
The Database stores the data needed for the application (like user details, product info,
etc.).
The Servlet communicates with the database to perform operations like fetching or
updating data.
6. HTTP Response:
Once the Servlet has processed the request and possibly retrieved data from the
database, it sends back an HTTP Response to the web server.
RRSIMT CLASSES WHATSAPP
Free Notes - 9795358008
Available Follow Us
on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
The web server then forwards this response to the user's browser, which displays
the result (like a webpage or data).
Working of Servlet -
RRSIMT CLASSES WHATSAPP
Free Notes - 9795358008
Available Follow Us
on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
How Servlets Work:
1. Client Sends a Request:
A user clicks a link or submits a form.
The browser sends this request to the server.
2. Server Passes the Request to the Servlet:
The server checks if there’s a Servlet mapped to handle that request.
3. Servlet Processes the Request:
It performs tasks like:
Accessing databases.
Performing calculations.
Fetching or updating information.
4. Servlet Sends a Response:
It generates a response (like HTML, JSON, or plain text) and sends it back to the
client’s browser.
RRSIMT CLASSES WHATSAPP
Free Notes - 9795358008
Available Follow Us
on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
Lifecycle of Servlet -
The lifecycle of a Servlet has 3 main stages:
Now let discuss in detail -
RRSIMT CLASSES WHATSAPP
Free Notes - 9795358008
Available Follow Us
on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
1.Initialization:
The servlet is created and initialized with the init() method (called once).
2.Request Handling:
For every client request, the service() method is called. It decides how to handle
GET, POST, or other HTTP methods.
3.Destruction:
When the server shuts down or the servlet is no longer needed, the destroy()
method is called to clean up resources.
Here’s a basic Servlet that responds with "Hello, World“ ;
RRSIMT CLASSES WHATSAPP
Free Notes - 9795358008
Available Follow Us
on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// Set response content type
response.setContentType("text/html"); •doGet Method:
Handles GET requests from the client.
// Write the response •response.getWriter():
PrintWriter out = response.getWriter(); Used to send a response back to
out.println("<h1>Hello, World!</h1>");
}
the browser.
}
RRSIMT CLASSES WHATSAPP
Free Notes - 9795358008
Available Follow Us
on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
Handling HTTP GET and POST Requests in Servlets –
In Servlet, HTTP GET and HTTP POST are two common methods for sending requests
from a client (like a web browser) to a server. Let's break them down and explain how
to handle these requests in Servlets.
1. HTTP GET Request:
Used for: Fetching data from the server.
Characteristics:
The request data is appended to the URL (e.g.,
www.example.com?name=John&age=25).
It is visible in the URL.
It has limitations on the amount of data you can send (typically around 2000 characters).
RRSIMT CLASSES WHATSAPP
Free Notes - 9795358008
Available Follow Us
on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
Example of Handling a GET Request in a Servlet:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
// Annotation to map this Servlet to a specific URL
@WebServlet("/greet")
public class GetRequestServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Retrieve parameters from the request
String name = request.getParameter("name");
// Send a response back to the client
response.setContentType("text/html");
response.getWriter().println("<h1>Hello, " + name + "!</h1>");
}}
RRSIMT CLASSES WHATSAPP
Free Notes - 9795358008
Available Follow Us
on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
Explanation:
1. doGet() Method: This method handles GET requests.
2. request.getParameter("name"): Retrieves the value of the name parameter from
the URL.
3. response.getWriter().println(): Sends the response back to the client (browser).
Example URL:
If the user goes to http://localhost:8080/greet?name=John, the server will respond
with: "Hello, John!"
RRSIMT CLASSES WHATSAPP
Free Notes - 9795358008
Available Follow Us
on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
2. HTTP POST Request:
Used for: Sending data to the server (e.g., form submissions).
Characteristics:
Data is sent in the body of the request (not visible in the URL).
It is more secure than GET for sensitive data.
Can send a large amount of data.
Example of Handling a POST Request in a Servlet:
Explanation:
•doPost() Method: This method handles POST requests.
•request.getParameter("name"): Retrieves the name parameter from the form data.
•response.getWriter().println(): Sends the response back to the client, confirming form
submission.
RRSIMT CLASSES WHATSAPP
Free Notes - 9795358008
Available Follow Us
on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/submitForm")
public class PostRequestServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Retrieve form data
String name = request.getParameter("name");
String email = request.getParameter("email");
// Send a response back to the client
response.setContentType("text/html");
response.getWriter().println("<h1>Form Submitted!</h1>");
response.getWriter().println("<p>Name: " + name + "</p>");
response.getWriter().println("<p>Email: " + email + "</p>");
}}
RRSIMT CLASSES WHATSAPP
Free Notes - 9795358008
Available Follow Us
on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
Example ( HTML) -
<form action="submitForm" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<label for="email">Email:</label>
<input type="email" id="email" name="email">
<input type="submit" value="Submit">
</form>
When the user fills out the form and submits it, the data is sent to the submitForm
Servlet via a POST request.
RRSIMT CLASSES WHATSAPP
Free Notes - 9795358008
Available Follow Us
on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
Key Differences Between GET and POST:
1. GET:
• Data is sent in the URL.
• Used for fetching data.
• Not secure for sensitive data.
2. POST:
• Data is sent in the request body.
• Used for sending data (like forms).
• More secure than GET.
RRSIMT CLASSES WHATSAPP
Free Notes - 9795358008
Available Follow Us
on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
Redirecting Requests to Other Resources in Servlets –
In web applications, you might want to send the user from one page to another
automatically. This is called redirecting. In Servlets, there are two main ways to redirect a
request:
1. Client-side Redirect (HTTP Redirect)
2. Server-side Forward
1. Client-side Redirect (HTTP Redirect) -
What It Is: The server sends a special response to the browser telling it to go to a different
URL.
How It Works: The browser receives the new URL and then sends a new request to that
URL.
When To Use: Use this when you want to send the user to another website or a
completely different resource.
RRSIMT CLASSES WHATSAPP
Free Notes - 9795358008
Available Follow Us
on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
Example -
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet; •response.sendRedirect("newPage"
): This tells the browser to go to
import javax.servlet.http.HttpServlet; "newPage".
import javax.servlet.http.HttpServletRequest; •Result: When a user visits /oldPage,
import javax.servlet.http.HttpServletResponse; they are redirected to /newPage.
@WebServlet("/oldPage")
public class RedirectServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Redirecting to a new URL
response.sendRedirect("newPage"); }}
RRSIMT CLASSES WHATSAPP
Free Notes - 9795358008
Available Follow Us
on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
2. Server-side Forward
What It Is: The server forwards the request to another resource (like another Servlet or
JSP) without the browser knowing.
How It Works: The server processes the second resource and sends the response back
to the browser.
When To Use: Use this when you want to process something in another Servlet or JSP
but keep the same URL in the browser.
RRSIMT CLASSES WHATSAPP
Free Notes - 9795358008
Available Follow Us
on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
Example –
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet; •RequestDispatcher dispatcher =
import javax.servlet.RequestDispatcher; request.getRequestDispatcher("nextPage.jsp"):
import javax.servlet.http.HttpServlet; Prepares to forward the request to "nextPage.jsp".
import javax.servlet.http.HttpServletRequest; •dispatcher.forward(request, response): Forwards
import javax.servlet.http.HttpServletResponse; the request and response to "nextPage.jsp".
@WebServlet("/forwardExample")
public class ForwardServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Forwarding to another resource (JSP or Servlet)
RequestDispatcher dispatcher = request.getRequestDispatcher("nextPage.jsp");
dispatcher.forward(request, response);
}
}
RRSIMT CLASSES WHATSAPP
Free Notes - 9795358008
Available Follow Us
on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
RRSIMT CLASSES WHATSAPP
Free Notes - 9795358008
Available Follow Us
on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
What is Session Tracking –
When you visit a website, it often needs to remember who you are as you move from
one page to another. For example, when you log into an online store, it needs to
remember you're logged in when you visit different sections of the site, like the
product page or your cart. This remembering process is called session tracking.
What is a Session?
Session: A session is like a conversation between your web browser and a web server.
It starts when you visit a website and ends when you leave or close the browser.
Why it's needed: HTTP, the protocol used for web communication, is stateless,
meaning it doesn’t remember anything from one page to another. Session tracking
helps maintain this memory.
RRSIMT CLASSES WHATSAPP
Free Notes - 9795358008
Available Follow Us
on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
How is Session Tracking Done?
1.Cookie –
A cookie is a small piece of data that a web server sends to a user's web browser. The
browser stores this data and sends it back to the server with future requests. Cookies
are used to remember information about the user, like login details or shopping cart
contents.
Why Use Cookies?
1. Remember user preferences: Cookies can store user settings, like language
preference or theme choice.
2. Session management: They help websites remember users between different
visits or as they navigate from page to page.
3. Tracking: Cookies can track user activity to provide personalized experiences or for
analytics.
RRSIMT CLASSES WHATSAPP
Free Notes - 9795358008
Available Follow Us
on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
How Cookies Work (Step by Step):
i. Client Request: When you visit a website, your browser sends a
request to the server.
ii. Server Response with Cookie: The server processes your request and
sends back a response, which may include a cookie.
iii. Storing the Cookie: Your browser stores the cookie on your computer.
iv. Subsequent Requests: Each time you make a new request to the same
server, your browser sends the stored cookie back to the server.
v. Using the Cookie: The server uses the cookie to remember your
information, like your login status or cart items.
RRSIMT CLASSES WHATSAPP
Free Notes - 9795358008
Available Follow Us
on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
Types of Cookies:
i. Session Cookies:
Temporary: These cookies are deleted when you close your browser.
Usage: They are used for temporary things like remembering what's in your
shopping cart during a single session.
ii. Persistent Cookies:
Long-term: These cookies stay on your computer even after you close your
browser.
Usage: They store things like login credentials so you don't have to log in every
time you visit a site.
iii. Secure Cookies:
Encrypted: These cookies are used over secure connections (HTTPS) and help in
securely transferring sensitive data.
RRSIMT CLASSES WHATSAPP
Free Notes - 9795358008
Available Follow Us
on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
iv. Third-party Cookies:
Different domain: These cookies are set by websites other than the one you are
visiting, often used for advertising and tracking.
Free Notes Available on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
Advantages of Cookies:
i. Lightweight: They are small and don’t require much storage.
ii. Persistent: They can store data between sessions, providing a consistent user
experience.
Disadvantages of Cookies:
i. Privacy concerns: Cookies can be used to track user behavior across websites, raising
privacy issues.
ii. Limited size: They can only store a small amount of data (usually up to 4KB per
cookie).
iii. Browser dependent: Users can disable cookies, which may affect website
functionality.
RRSIMT CLASSES WHATSAPP
Free Notes - 9795358008
Available Follow Us
on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
Session Tracking with HttpSession –
Session tracking is a way to maintain user information across multiple requests in a web
application. Since HTTP is a stateless protocol (it doesn't remember user data between
requests), we need a way to track the user’s state, and HttpSession is one of the ways to
do this in Java.
What is HttpSession?
HttpSession is an interface provided by Java Servlet API that allows you to store
information about a user session between HTTP requests. Each user interacting with a
web application gets a unique session that can store user-specific data like login status
or preferences.
Why Use HttpSession?
•To remember user data across multiple requests (like login status).
•To store user-specific information (like items in a shopping cart).
•To maintain session data between different pages of a web application.
Free Notes Available on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/Login")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Get username from request
String username = request.getParameter("username");
// Create or retrieve the session Key Methods of HttpSession:
HttpSession session = request.getSession(); 1.getSession(): Creates a new session or retrieves an existing one.
// Store the username in the session
2.setAttribute(String name, Object value): Stores an attribute in the
session.setAttribute("username", username); session.
3.getAttribute(String name): Retrieves the value of an attribute.
// Redirect to a welcome page 4.removeAttribute(String name): Removes an attribute from the session.
response.sendRedirect("welcome.jsp");
} 5.invalidate(): Ends the session.
}
Free Notes Available on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
Advantages of Using HttpSession:
1. Simple to Use: HttpSession makes it easy to store and retrieve user data.
2. Built-in Session Management: The servlet container handles the creation and
management of sessions.
3. Secure: Sensitive data can be stored on the server-side, reducing security risks.
Disadvantages:
1. Server Memory Usage: Each session consumes server memory, which can be an issue
with many users.
2. Session Timeout: If not managed properly, sessions can expire, causing loss of user
data.
Free Notes Available on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
Introduction Java Server Pages(Overview) –
JavaServer Pages (JSP) is a technology in Java used to create dynamic web pages. It
allows developers to mix HTML with Java code, making it easier to build web
applications that generate content dynamically.
Think of JSP Like This:
Imagine a webpage that changes based on the user. For example, a welcome page that
greets users by their name, "Hello, Arman!" instead of just a plain "Hello, User!"
JSP helps you create such pages by embedding Java code into the HTML.
Here is the JSP Architecture mean how JSP Works –
Free Notes Available on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
Java Server Pages(JSP) Architecture -
This diagram shows how JSP (JavaServer Pages) works in a web applicationLet see Step by step –
Free Notes Available on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
1. Client (Browser):
The user interacts with a web browser (like Chrome or Firefox) and sends a request to
view a web page.
2. Request Sent Over the Internet:
The browser sends the client request (usually an HTTP request) over the internet to the
web server.
3. Web Server:
The web server receives the request and processes it.
If the request is for a JSP page, the server forwards it to the JSP engine.
4. JSP Engine (Servlet):
The JSP engine converts the JSP page into a servlet (Java code).
This servlet handles the request, interacts with any databases (if needed), and
generates dynamic HTML content.
RRSIMT CLASSES WHATSAPP
Free Notes - 9795358008
Available Follow Us
on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
5. Database Interaction:
If data is needed from a database (like fetching user info or product details), the servlet
queries the database.
The database sends back the requested data to the servlet.
6. HTML Output:
The servlet processes the data and creates an HTML response.
This HTML response (output) is sent back to the web server.
7. Response Sent to Client:
The web server sends the HTML response back to the client's browser.
The browser displays the web page with the dynamic content that was generated.
RRSIMT CLASSES WHATSAPP
Free Notes - 9795358008
Available Follow Us
on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
Summary –
• Client (browser) sends a request to view a web page.
• The web server processes the request, forwards it to the JSP (which is converted
into a servlet).
• The servlet handles the request, fetches data from the database if needed, and
sends back an HTML page.
• The browser displays the HTML page to the user.
RRSIMT CLASSES WHATSAPP
Free Notes - 9795358008
Available Follow Us
on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
Lifecycle of JSP -
JSP File
Translation phase
Servlet File
Compilation phase
Servlet Class
Called once jspInit()
jspService() Handle multiple request
and Response
Called once jspDestroy()
Free Notes
RRSIMT CLASSES Available
WHATSAPP on : https://telegram.me/rrsimtclasses
– 9795358008 Follow us On
Web Technology (BCS502)
This diagram outlines the steps involved in the life cycle of a JSP
1. Translation Phase:
•JSP File: You write a web page using a JSP file (with .jsp extension).
•The JSP file is translated into a Servlet file. This step converts your JSP page
into a Java servlet.
2.Compilation Phase:
•The Servlet file is then compiled into a Servlet class (a Java class).
•This is done so that the server can execute the code and handle requests.
3.Execution Phase:
•Once the servlet class is ready, the JSP page goes through the following
methods:
a. jspInit():
•This method is called once when the JSP page is loaded for the first time.
•It’s used for initializing resources like database connections.
Free Notes
RRSIMT CLASSES Available
WHATSAPP on : https://telegram.me/rrsimtclasses
– 9795358008 Follow us On
Web Technology (BCS502)
b. jspService():
•This method handles multiple requests.
•Every time a user sends a request to the JSP page, the jspService() method is
called to process the request and send a response.
•It dynamically generates the content (like HTML) to send back to the client.
c. jspDestroy():
•This method is called once when the JSP page is no longer needed.
•It’s used for cleanup, like closing database connections or releasing resources.
Free Notes
RRSIMT CLASSES Available
WHATSAPP on : https://telegram.me/rrsimtclasses
– 9795358008 Follow us On
Web Technology (BCS502)
Key Features of JSP:
i. Dynamic Content: Generates content based on user actions or data (e.g.,
displaying user-specific data).
ii. Simpler than Servlets: JSP is more focused on presentation (like HTML), while
servlets are for processing logic.
iii. Combines Java and HTML: You can write Java code directly inside HTML.
iv. Part of Java EE: Works well with other Java technologies like Servlets and JDBC.
Free Notes
RRSIMT CLASSES Available
WHATSAPP on : https://telegram.me/rrsimtclasses
– 9795358008 Follow us On
Web Technology (BCS502)
A First Java Server Page Example (VV.IMP)
Step 1: Create a Basic HTML Page
<html>
<head> This page will always show the
<title>Welcome Page</title> same content when a user visits
</head> it. But what if you want to make
<body> it dynamic, like greeting the
<h1>Welcome to My Website</h1> user by name? This is where
<p>This is a simple web page.</p> JSP comes in.
</body>
</html>
Free Notes
RRSIMT CLASSES Available
WHATSAPP on : https://telegram.me/rrsimtclasses
– 9795358008 Follow us On
Web Technology (BCS502)
Step 2: Add Java Code to Your Page
<html>
<head>
<title>Welcome Page</title>
Now here you can see
</head>
how a static page convert
<body>
into a dynamic JSP page
<h1>Welcome to My Website</h1>
by adding some Java
<p>
code. We'll use a JSP
<%
scriptlet to include Java
// Java code to get the current time
code inside our HTML.
java.util.Date date = new java.util.Date();
Here's how the page
out.println("Current date and time: " + date);
looks after adding some
%>
Java code:
</p>
</body>
</html>
Free Notes
RRSIMT CLASSES Available
WHATSAPP on : https://telegram.me/rrsimtclasses
– 9795358008 Follow us On
Web Technology (BCS502)
•<% ... %>: This is a scriptlet. It allows you to embed Java code in your JSP page.
•java.util.Date date = new java.util.Date();: This line creates a new Date object
representing the current date and time.
•out.println(...): This line prints the current date and time into the HTML page.
Step 3: Save the File with a .jsp Extension
Save your file as welcome.jsp. The .jsp extension tells the server that this file contains JSP
code that needs to be processed.
Now you can Access the JSP page by typing the URL in your browser (e.g.,
http://localhost:8080/welcome.jsp).
Here is the output -
RRSIMT Free Notes
WHATSAPP – : https://telegram.me/rrsimtclasses
Available on Follow us
CLASSES 9795358008 On
Web Technology (BCS502)
When you visit the welcome.jsp page, the server processes the Java code and sends the
result to your browser. You’ll see something like this:
<html>
<head>
<title>Welcome Page</title>
</head>
<body>
<h1>Welcome to My Website</h1>
<p>Current date and time: Wed Jan 13 12:34:56 IST 2025</p>
</body>
</html>
Free Notes Available on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
<html>
<head> Another Example it a bit
<title>Personalized Welcome</title> more interactive by greeting
</head>
<body> the user with their name.
<h1>Welcome to My Website</h1> http://localhost:8080/welcome.jsp?name=Arman
<p> When you visit this URL, the page will greet you
<%
with "Hello, Arman!".
String name = request.getParameter("name");
if (name != null) {
out.println("Hello, " + name + "!");
} else {
out.println("Hello, Guest!");
}
%>
</p>
</body>
</html>
Free Notes Available on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
Free Notes Available on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
Implicit Objects in JSP –
In JSP (JavaServer Pages), implicit objects are special objects provided by the JSP
container. You don’t need to create them; they are automatically available in your JSP
pages. These objects help you interact with different parts of the web application like
requests, responses, sessions, etc.
Here’s most commonly used implicit objects, with simple examples.
1. request Object
Contains all the information about the request from the client (like a web browser).
•Use: To get data sent from a form or URL.
Here is Basic Simple Example –
Free Notes Available on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
you have a form where the user enters their name:
<form action="welcome.jsp">
Enter your name: <input type="text" name="username">
<input type="submit" value="Submit">
</form>
In welcome.jsp -
<%
// Get the 'username' parameter from the request
String name = request.getParameter("username");
out.println("Hello, " + name + "!");
%>
Retrieves the value entered
by the user in the form.
Free Notes Available on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
2. response Object
Used to send a response back to the client.
•Use: To redirect to another page or send output.
In Jsp-
<% Redirect the user to another page:
response.sendRedirect("newpage.jsp");
%>
3. session Object
Represents the session for a user. It stores information (like user details) that you want
to keep as long as the user is on your site.
•Use: To save data between different pages for the same user.
Free Notes Available on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
In JSP -
Save and retrieve user data:
<%
// Set an attribute in the session
session.setAttribute("username", "Arman");
%>
<a href="welcome.jsp">Go to Welcome Page</a>
4. out Object
Used to send output (like HTML content) to the client.
•Use: To display dynamic content in the web page.
In JSP –
<% Display a message:
out.println("Welcome to our website!");
%>
Free Notes Available on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
Standard Action in JSP –
Standard actions in JSP are special tags that perform specific tasks such as including
other files, forwarding requests, or working with JavaBeans. These actions are
predefined and are used to control the behavior of your JSP pages.
Here are the most common standard actions explained in simple terms, with examples.
1. <jsp:include>
Includes the content of another file (e.g., another JSP or HTML) at runtime.
•Use: To reuse common parts of your website like headers, footers, or menus.
Example:
You have main jsp file.
You have a header file header.jsp:
<jsp:include page="header.jsp" />
<!-- header.jsp -->
<p>This is the main content of the page.</p>
<h1>Welcome to My Website</h1>
Free Notes Available on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
2. <jsp:forward>
Forwards the request from one JSP page to another.
•Use: To redirect users based on some conditions.
Example:
In first.jsp, you might check a condition and forward the user:
<%
boolean isLoggedIn = false; // Example condition
if (!isLoggedIn) {
%>
<jsp:forward page="login.jsp" />
<%
}
%>
<p>Welcome, user!</p>
Free Notes Available on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
JSP Directives -
JSP Directives are special instructions in a JSP page that provide global information
about how the JSP should be processed by the server. They tell the server how to handle
certain parts of the page or the entire page.
There are three main types of JSP directives:
1.<%@ page %> Directive
2.<%@ include %> Directive
3.<%@ taglib %> Directive
Free Notes Available on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
Custom Tag Libraries –
Custom Tag Libraries in JSP are user-defined tags that help simplify and enhance the
functionality of JSP pages. Instead of writing repetitive Java code in JSP, you can create
custom tags that encapsulate the logic and make your pages cleaner and easier to
maintain.
Why Use Custom Tag Libraries?
i. Reusability: You can create a tag once and reuse it across multiple JSP pages.
ii. Simplifies JSP Pages: Reduces the amount of Java code in JSP pages, making them
easier to read.
iii. Encapsulation: Encapsulates complex functionality into simple tags.
iv. Separation of Concerns: Keeps Java code separate from HTML, following a clean
MVC (Model-View-Controller) pattern.
Free Notes Available on : https://telegram.me/rrsimtclasses
Web Technology (BCS502)
Thank You…
Free Notes Available on : https://telegram.me/rrsimtclasses