Chapter 6: Java Web Software
Agenda
• Introduction to Servlets
• Java Server Pages
• The NetBeans IDE
• Storing Information on Clients
• JavaBeans
• Model-View-Controller Application Architecture
• Java Server Faces
• Summary • Review Questions • Exercises
Introduction to Servlets
What is a Servlet?
• A Servlet is a Java program that runs on a web server and handles HTTP requests (from
browsers or other clients).
It receives requests from web browsers (clients), processes them (using Java code, databases,
etc.), and then sends back a response, usually in HTML.
• It is used to generate dynamic web content — unlike static HTML pages that don’t change.
• How Servlets Work :
1. When a user (browser) requests a specific URL linked to a servlet,
→ the web server recognizes this request and creates or reuses the servlet object.
2. The servlet called a servlet container (or servlet engine),runs inside a special environment.
Introduction to Servlets
Servlet Container
• It’s part of the web server responsible for executing servlets, loading and running the servlet
class, handling HTTP methods like GET or POST and Managing security and lifecycle
(creation, execution, and destruction).
• When a servlet is called, the container passes two objects: HttpServletRequest for input data
and HttpServletResponse for sending output.
• The servlet reads request data, performs logic or database actions, and generates a dynamic
response, the servlet sends this output through the response object.
• Finally, the servlet container returns the response to the web server, which sends it to the
client’s browser for display.
Introduction to Servlets
Servlet Methods Overview
• Servlets handle client requests using methods like doGet() and doPost(), which are called
by the web server automatically depending on the HTTP request type.
The doGet() method processes requests sent with the GET method (e.g., clicking a link or
submitting a form with method="GET").
The doPost() method processes requests sent with POST (e.g., submitting form data securely).
• Both methods receive two objects:
- HttpServletRequest request → holds client request data.
- HttpServletResponse response → allows sending data back to the client.
• To send output to the browser:
- Set content type → [Link]("text/html");
- Create a writer → PrintWriter out = [Link]();
- Use [Link]() to generate HTML.
Servlets • Example:
C:\tomcat\webapps\MyServletApp\
WEB-INF\classes\[Link]
• In Servlets, you must write all the HTML content using import [Link].*;
Java statements like [Link]("<html>...</html>");, import [Link].*;
which makes the code longer and harder to read. import [Link].*;
public class WelcomeServlet extends
HttpServlet {
• A Servlet is a Java class saved with the “.java” extension, public void doGet(HttpServletRequest
It is compiled into bytecode (.class) and executed by the request, HttpServletResponse response)
throws ServletException, IOException
web server (e.g., Tomcat).
{
[Link]("text/html");
• The servlet runs on the server, processes client requests, PrintWriter out =
and generates dynamic HTML that is sent back to the [Link]();
browser. [Link]("<html>");
[Link]("<body>");
[Link]("<h2>Welcome!</h2>");
• Servlet = Java code + HTML generated on the Server. String name = "Ali";
[Link]("Hello, " + name + "!");
[Link]("</body>");
[Link]("</html>");
[Link]();
}}
Servlets C:\tomcat\webapps\MyServletApp\
WEB-INF\[Link]
• C:\tomcat\webapps\ <web-app>
MyServletApp\ <servlet>
<servlet-name>welcome</servlet-name>
– WEB-INF\classes\ <servlet-class>WelcomeServlet</servlet-
class>
• javac -cp C:\tomcat\lib\servlet- </servlet>
[Link] [Link] <servlet-mapping>
• [Link] <servlet-name>welcome</servlet-name>
<url-pattern>/welcome</url-pattern>
MyServletApp/welcome </servlet-mapping>
</web-app>
Servlets
Example Servlets vs HTML
Java HTML
C:\tomcat\webapps\HelloApp\WEB-INF\classes\
[Link] C:\tomcat\webapps\HelloApp\[Link]
cd "C:\tomcat\webapps\HelloApp\WEB-INF\classes"
& javac -cp "C:\tomcat\lib\[Link];."
<!DOCTYPE html>
[Link]
<html>
import [Link].*; <head><title>Call Servlet Example</title></head>
import [Link].*; <body>
import [Link].*; <form action="HelloServlet" method="GET">
<input type="submit" value="Send Request">
public class HelloServlet extends HttpServlet { </form>
protected void doGet(HttpServletRequest </body>
request, HttpServletResponse response) </html>
throws ServletException, IOException {
XML code:
C:\tomcat\webapps\HelloApp\WEB-INF\[Link]
// Step 1: Define the content type
<web-app>
[Link]("text/html");
<servlet>
<servlet-name>hello</servlet-name>
// Step 2: Create output stream
<servlet-class>HelloServlet</servlet-class>
PrintWriter out = [Link]();
</servlet>
<servlet-mapping>
// Step 3: Generate HTML response
<servlet-name>hello</servlet-name>
[Link]("<html><body>");
<url-pattern>/HelloServlet</url-pattern>
[Link]("<h2>Servlet Received GET
</servlet-mapping>
Request!</h2>");
</web-app>
[Link]("</body></html>");
[Link]
}
}
Java Server Pages(Jsp)
• In servlets, the entire HTML response is produced using Java statements like:
• [Link]("<html><body><h1>Hello!</h1></body></html>"); this means the markup (HTML) is
mixed directly with Java code.
Web designers work on layout, style, and user interface (HTML, CSS, design).
Programmers focus on logic, form processing, and database access (Java code).
→ Servlets mix these two layers, so both groups must edit the same code —
which is inefficient and confusing.
• JSP solves this by separating presentation (HTML) from logic (Java), allowing each
team to work independently.`
• Java Server Pages (JSP), which are built on top of servlets, provide alternative ways of constructing
dynamic Web documents. It is ways, not way, because JSP includes several different approaches
to generate Web documents dynamically.
?What
Java is JSP
Server (Java Server Pages)
Pages(Jsp)
• Java Server Pages (JSP) is a server-side technology used to
create dynamic web pages using Java.
It allows you to mix HTML and Java code in a single file — but :Example
in a cleaner and more readable way than servlets. C:\tomcat\webapps\MyJSPApp\[Link]
• Instead of writing long [Link]("<html>...</html>"); lines in >html<
servlets, you can write HTML normally and only insert small >body<
pieces of Java code when needed. >h2>Welcome!</h2<
%<
• The JSP file is saved with the “.jsp” extension, The web server ;"String name = "Ali
(e.g., Tomcat) converts the JSP into a servlet automatically. [Link]("Hello, " + name
;+ "!")
• The servlet runs on the server, generating dynamic HTML sent to >%
the browser. >body/<
>html/<
• JSP = HTML + Java on the Server.
[Link]
[Link]
The NetBeans IDE
• NetBeans serves as a versatile Integrated Development Environment (IDE) primarily
used for Java programming, offering a comprehensive suite of tools for code creation,
debugging, and deployment.
• Beyond Java, NetBeans supports various programming languages and technologies,
making it applicable for a wide range of development projects including web
applications, mobile app development.
• It has a large set of project templates to choose from and handles a lot of the mundane
and time-consuming tasks associated with Java web development, such as creating and
maintaining XML configuration files.
The NetBeans IDE
Used NetBeans for Web Development
• Simplifies development of dynamic web applications, it automatically builds the
required WAR (Web Application Archive) structure.
• Integrates with web servers provides ready templates for Servlets, JSP pages, HTML ,
CSS and JavaScript.
• It can be downloaded as a bundled package with the Java Development Kit (JDK) and
also comes bundled with Apache Tomcat, one of the most popular web servers used for
Java web development.
The NetBeans IDE
Install NetBeans and Create Web page
Part1: Installation Steps
Go to the official site: [Link] .1
✨ This is a good links
.Click Download → Choose your version (latest or LTS) .2 that shows the
installation and creating
.Install Java JDK (Java Development Kit) first .3
web page in NetBeans :
.Then install NetBeans IDE (select Java EE or Java Web during setup) .4 🔗
How to Install NetBeans IDE and Java JDK on Windo
ws | Step-by-Step Guide – YouTube
Part2: Create a Web Application in NetBeans Steps
🔗
1. Open NetBeans → File → New Project How to Create java Web application using NetBeans
IDE | Install Configure Tomcat 10 Server - YouTube
2. Select Java Web → Web Application
3. Choose server: Apache Tomcat or GlassFish
4. Give project name (e.g., MyWebApp)
5. Finish → NetBeans creates folders and web files automatically.
The NetBeans IDE
Adding a JSP or Servlet
:Example :Example
Java
Jsp
>html< <
;[Link]("<html>")
>body<
;[Link]("<body>") >h2>Welcome to My Web App!</h2<
[Link]("<h2>Hello from Servlet! [Link]("Today is: " + new %<
;</h2>") >%;[Link]())
;[Link]("</body>") >body/<
;[Link]("</html>") >html/<
Expected Output on Browser Expected Output on Browser
!Hello from Servlet Welcome to My Web App!
Today is: Fri Oct 10 2025 [Link] GMT+0300
Storing Information on Clients
• Client-side storage means saving data in the user’s browser or device, not on the server.
• It helps the website remember user details (like name or preferences) between visits.
• Used to reduce server load and improve user experience.
• Common Methods:
• Cookies: Small text files stored by the browser (e.g., for login info).
• URL Rewriting: Adds information (like session ID) to the web address.
• Hidden Form Fields: Invisible fields in HTML forms holding data.
• Web Storage (HTML5): Uses localStorage or sessionStorage for larger data.
• Example (Cookie in Servlet):
• This code creates a cookie named username and stores it on the client’s browser.
later, the servlet can read this cookie to identify the user.
Example:
Cookie user = new
Cookie("username", "Ali");
[Link](user);
JavaBeans
• A JavaBean is a reusable Java class that follows special rules so it can be easily used
in JSP pages, Servlets, or other Java programs.
• JavaBeans are simple data holder classes used to connect Java logic with JSP pages
— they make web applications modular, reusable, and clean.
• JavaBeans can be used in Servlets and JSPs, in Servlets usually create and fill the Bean
with data, while JSPs read and display the Bean’s data to the user.
• Why Use JavaBeans ?
• To separate logic from data (Servlet does logic, Bean stores data).
• To reuse data across pages (store once, use many times).
• To make code modular and organized (clean MVC structure).
• To easily pass data between Servlets and JSPs.
JavaBeans
Model–View-Controller(MVC)
It’s a design pattern used to organize web applications into three parts, so code becomes
.cleaner, easier to manage, and reusable
Part Role File Type
The Controller(C) — receives requests
from the browser, processes data, Servlet
Servlet
connects with database, and decides ([Link])
which page to show.
The Model(M) — stores and carries data JavaBean
JavaBean
(for example: name, age, course). ([Link])
The View(V) — displays the data and JSP page
JSP
results to the user using HTML. ([Link])
Model–View-Controller(MVC)
MVC Flow Diagram
Example: Student Form
➤ Step 1 — User fills form
• The user types their name and course, then clicks Submit.
HTML form sends the data to the Servlet.
➤ Step 2 — Servlet (Controller)
• Receives the request
• Creates a JavaBean object
• Fills it with the user’s data
• Forwards it to a JSP page
➤ Step 3 — JavaBean (Model)
• Holds the user data (e.g., name = “Saad”, course = “Web Programming”)
➤ Step 4 — JSP (View)
• Displays that data on a web page using <jsp:getProperty>
Simple MVC
Example
Model — [Link]
*/
Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/[Link] *
to change this license
Click nbfs://nbhost/SystemFileSystem/Templates/Classes/[Link] to edit *
this template
/*
;package mypackage
**/
*
author as349@ *
/*
{ public class StudentBean
;private String name
;private String course
}{ )(public StudentBean
public String getName() { return name; }
public void setName(String name) { [Link] = name; }
public String getCourse() { return course; }
public void setCourse(String course) { [Link] = course; }
}
Simple MVC
Example
Controller — [Link]
package mypackage;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet(name = "StudentServlet", urlPatterns = {"/StudentServlet"})
public class StudentServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
StudentBean student = new StudentBean();
[Link]([Link]("name"));
[Link]([Link]("course"));
[Link]("studentInfo", student);
RequestDispatcher rd = [Link]("[Link]");
[Link](request, response);
}
}
Simple MVC
Example
View — [Link]
>%"page contentType="text/html" pageEncoding="UTF-8@%<
>DOCTYPE html!<
>html<
>head<
meta http-equiv="Content-Type" content="text/html;<
>"charset=UTF-8
>title>JSP Page</title<
>head/<
>body<
jsp:useBean id="studentInfo"<
>/ "class="[Link]" scope="request
p>Name: <jsp:getProperty name="studentInfo" property="name"<
>/></p
p>Course: <jsp:getProperty name="studentInfo"<
>property="course" /></p
>body/<
>html/<
Expected Output on Browser
Saad :Name
Course: Web Programming
Simple MVC
Example
View — [Link]
>%"page contentType="text/html" pageEncoding="UTF-8@%<
>DOCTYPE html!<
>html<
>head<
>"meta http-equiv="Content-Type" content="text/html; charset=UTF-8<
>title>JSP Page</title<
>head/<
>body<
>h2>Enter Student Info</h2<
>"form action="StudentServlet" method="post<
>/Name: <input type="text" name="name" /><br/><br
>/Course: <input type="text" name="course" /><br/><br
>/ "input type="submit" value="Submit<
>form/<
>body/<
>html/<
Expected Output on Browser
Saad :Name
Course: Web Programming
JavaServer Faces (JSF)
• Java Server Faces (JSF) is a Java web framework used for building user
interfaces for web applications.
• JSF provides reusable UI components, event handling, and integration with
Servlets and JSP.
• It simplifies web development by following the MVC pattern, which separates the
presentation (view) from the application logic (model/controller).
• JSF manages UI components, form inputs, validation, and events automatically.
JavaServer Faces (JSF)
Example :Example
>? "xml version="1.0" encoding="UTF-8?<
>DOCTYPE html!<
"html xmlns="[Link]
• JSF (Java Server Faces) Uses XHTML pages with >"xmlns:h="[Link]
special JSF tags like <h:form>, <h:inputText>, and >h:head<
<h:commandButton>. >title>Welcome Page</title<
>h:head/<
>h:body<
• JSF (Java Server Faces) ,Uses Facelets (.xhtml) pages >h:form<
instead of JSP for cleaner design. >/ ":h:outputText value="Enter your name<
>/br/><br<
>/ "h:inputText value="#{[Link]}<
• JSP focuses on creating pages with embedded Java >/br/><br<
h:commandButton value="Say Hello"<
code,it provides a full component-based framework for>/ "action="#{[Link]}
building and managing dynamic web interfaces. >h:form/<
>/br<
>/ "h:outputText value="#{[Link]}<
>h:body/<
>html/<
Expected Output on Browser
!Hello, Abdullah
JavaServer Faces (JSF)
Example public String getMessage() {
package mypackage; return message;
import [Link];
}
import [Link];
public void sayHello() {
import [Link];
if (name == null ||
@ManagedBean(name = "userBean")
@SessionScoped
[Link]().isEmpty()) {
public class UserBean implements Serializable { message = "Hello!";
private String name; } else {
private String message; message = "Hello, " +
// getter & setter name + "!";
public String getName() { }
return name; }
} }
public void setName(String name) {
[Link] = name;
}
Expected Output on Browser
!Hello, Abdullah
Summary
• A servlet is a Java program that resides on the Web server and is enacted when a request is
received from a Web client.
• A program called a servlet container, which runs on the Web server, controls the execution
of servlets.
• The most common uses of servlets are as server-side programs to generate Web documents
dynamically.
• The easiest way to develop Java-based Web applications is with an IDE.
• NetBeans is one of the most widely used Java Web IDEs.
• A Web server can use cookies to store information about clients on the clients themselves.
• Servlets should be used when there is little static content in the return document; JSP
should be used when there is little dynamic content.
• JSP is a collection of several approaches to support dynamic documents on the server.
• JSF adds to JSP the capabilities for building event-driven user interfaces to Web
applications.
Review Questions
• What is a servlet container?
• Describe the two parameters to doGet and doPost?
• What are the primary benefits of using an IDE for building servlet applications?
• What is a cookie and how is a cookie added to a response by a servlet?
• Why would a Web server need to store information on a client about the client’s previous
requests?
• What is a JavaBean and how are beans used by JSP applications?
• What happens during the translation process for JSP documents?
• What role do beans play in the design of an MVC-based Web application?
• What problem of JSP does JSF solve and which design pattern does JSF follow?
• What are the two standard tag libraries of JSF?
• What is the form of a JSF expression?
Mini Project
Project Title: Student Information Display
Objective :Create a simple Java web application that allows the student to
enter their name and department, then displays a welcome message using a servlet.
Requirements:
• 🔹 Software:
• NetBeans IDE (or Eclipse)
• Apache Tomcat Server
• JDK 8 or higher
• 🔹 Files Needed:
1. [Link] — for user input (HTML form).
2. [Link] — to process and display the result.
End of class
Thanks