0% found this document useful (0 votes)
8 views109 pages

Chapter 4 - JSP

Chapter 4 of CSC584 discusses JavaServer Pages (JSP), a technology for creating dynamic web pages using HTML and Java. It covers JSP constructs, advantages, the Model-View-Controller (MVC) architecture, the JSP lifecycle, and predefined variables. Additionally, it introduces JSP directives and JSTL (JSP Standard Tag Libraries) for simplifying JSP programming.

Uploaded by

nurahlee.dev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views109 pages

Chapter 4 - JSP

Chapter 4 of CSC584 discusses JavaServer Pages (JSP), a technology for creating dynamic web pages using HTML and Java. It covers JSP constructs, advantages, the Model-View-Controller (MVC) architecture, the JSP lifecycle, and predefined variables. Additionally, it introduces JSP directives and JSTL (JSP Standard Tag Libraries) for simplifying JSP programming.

Uploaded by

nurahlee.dev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 109

CSC584 Enterprise

Programming
Chapter 4 – JSP
MARSHIMA MOHD ROSLI
COMPUTER SCIENCE
JSP
 What is JSP?
Advantages of JSP
MVC – JSP?

Chapter 4 JSP Constructs


Outline JSP Predefined Variables
JSP Directives
JSTL Tags
What is JSP?
Mostly HTML page, with extension
.jsp
Include JSP tags to enable dynamic
content creation
Translation: JSP → Servlet class
Compiled at Request time
(first request, a little slow)

Execution: Request → JSP Servlet's


service method
Example:JSP
JavaServer Pages Technology
JavaServer Pages (JSP) technology provides a simplified,
fast way to create web pages that display dynamically-
generated content.

• JSP is a standard extension of


Java and is defined on top of
Servlet extensions.
• Its goal is to simplify
management and creation of
dynamic web pages.
• It is platform-independent,
secure, and it makes use of
Java as a server side scripting
language. 5
JSP Page
A JSP page is a page created by
the web developer that includes
JSP technology-specific tags,
declarations, and possibly
scriptlets, in combination with
other static HTML or XML tags.
A JSP page has the extension
.jsp; this signals to the web
server that the JSP engine will
process elements on this page.

6
What are the different types of JSP tags?
Advantages
Code -- Computation
HTML -- Presentation
Separation of Roles
◦ Developers
◦ Content Authors/Graphic Designers/Web
Masters
Model-View-Controller
JSP

Bean

Servlet
JSP Big Picture
GET /hello.jsp
Hello.jsp

<html>Hello!</html> HelloServlet.java

Server w/
JSP Container
HelloServlet.class
JSP Lifecycle

Init Event jspInit() 1) jsplnit(): The


container calls this to
initialize servlet instance.
It is called only once for
the servlet instance and
Request jspService() preceded every other
method.
Response
2) jspService(): The
container calls this for
each request and passes
it on to the objects.
Destroy Event jspDestroy() 3) jspDestroy(): It is
called by the container
just before destruction
of the instance.

11
A JSP File
A Simple JSP
<!-- CurrentTime.jsp -->

<HTML>

<HEAD>

<TITLE>

CurrentTime

</TITLE>

</HEAD>

<BODY>

Current time is <%= new java.util.Date() %>

</BODY>

</HTML>

13
How Is a JSP Processed?
Web Server Host
URL Example
http://www.server.com:8080/servlet/JSPFile
Host Machine File System
Send a request URL

Web Browser Web Server /servlet/JSPFile.jsp

HTML Page returned


Process Generate Get JSP Generated
Servlet Response file Servlates

Servlet Get Servlet JSP


Engine Translator

14
Activity
Explain the difference between Servlet and JSP.
JSP Constructs
There are three types of scripting constructs you can use to insert Java code into
the result servlet. They are expressions, scriptlets, and declarations.

expression A JSP expression is used to insert a Java


expression directly into the output. It has the
scriptlet following form:
<%= Java-expression %>
declaration
The expression is evaluated, converted into a
string, and sent to the output stream of the
servlet.

16
JSP Constructs
There are three types of scripting constructs you can use to insert Java code into
the resultant servlet. They are expressions, scriptlets, and declarations.

expression A JSP scriptlet enables you to insert a Java


statement into the servlet’s jspService method,
scriptlet which is invoked by the service method. A JSP
scriptlet has the following form:
declaration <% Java statement %>

17
JSP Constructs
There are three types of scripting constructs you can use to insert Java code into
the resultant servlet. They are expressions, scriptlets, and declarations.

expression A JSP declaration is for declaring methods or


fields into the servlet. It has the following
scriptlet form:
<%! Java method or field declaration %>
declaration

18
JSP Comment

HTML comments have the following form:


<!-- HTML Comment -->
If you don’t want the comment appear in the
resultant HTML file, use the following comment
in JSP:
<%-- JSP Comment --%>

19
<HTML>
<HEAD> Example 40.1 Computing
<TITLE> Factorials
Factorial
</TITLE> JSP scriptlet
</HEAD>
<BODY>

<% for (int i = 0; i <= 10; i++) { %>


Factorial of <%= i %> is
<%= computeFactorial(i) %> <br />
<% } %>
JSP expression
<%! private long computeFactorial(int n) {
if (n == 0)
return 1;
else
return n * computeFactorial(n - 1);
}
%>

</BODY> JSP declaration


</HTML>

20
Activity
Give examples
JSP Predefined Variables
You can use variables in JSP. For convenience, JSP provides eight predefined
variables from the servlet environment that can be used with JSP expressions and
scriptlets. These variables are also known as JSP implicit objects.

response
Represents the client’s request, which is an
out instance of HttpServletRequest. You can use it
session to access request parameters, HTTP headers
application such as cookies, hostname, etc.
config
pagecontext
page

22
JSP Predefined Variables
You can use variables in JSP. For convenience, JSP provides eight predefined variables from
the servlet environment that can be used with JSP expressions and scriptlets. These
variables are also known as JSP implicit objects.

request
Represents the servlet’s response, which is an
out instance of HttpServletResponse. You can use
session it to set response type and send output to the
application client.
config
pagecontext
page

23
JSP Predefined Variables
You can use variables in JSP. For convenience, JSP provides eight predefined variables from
the servlet environment that can be used with JSP expressions and scriptlets. These
variables are also known as JSP implicit objects.

request
response Represents the character output stream, which
is an instance of PrintWriter obtained from
session response.getWriter(). You can use it to send
application character content to the client.
config
pagecontext
page

24
JSP Predefined Variables
You can use variables in JSP. For convenience, JSP provides eight predefined variables from
the servlet environment that can be used with JSP expressions and scriptlets. These
variables are also known as JSP implicit objects.

request
response Represents the HttpSession object associated
out with the request, obtained from
request.getSession().
application
config
pagecontext
page

25
JSP Predefined Variables
You can use variables in JSP. For convenience, JSP provides eight predefined variables from
the servlet environment that can be used with JSP expressions and scriptlets. These
variables are also known as JSP implicit objects.

request
response Represents the ServletContext object for
out storing persistent data for all clients. The
session difference between session and application is
that session is tied to one client, but
config
application is for all clients to share persistent
pagecontext
page
data.

26
JSP Predefined Variables
You can use variables in JSP. For convenience, JSP provides eight predefined
variables from the servlet environment that can be used with JSP expressions and
scriptlets. These variables are also known as JSP implicit objects.

request
response Represents the ServletConfig object for the
out page.
session
application

pagecontext
page

27
JSP Predefined Variables
You can use variables in JSP. For convenience, JSP provides eight predefined
variables from the servlet environment that can be used with JSP expressions and
scriptlets. These variables are also known as JSP implicit objects.

request
response Represents the PageContext object.
out PageContext is a new class introduced in JSP to
session give a central point of access to many page
application attributes.
config

page

28
JSP Predefined Variables
You can use variables in JSP. For convenience, JSP provides eight predefined
variables from the servlet environment that can be used with JSP expressions and
scriptlets. These variables are also known as JSP implicit objects.

request
response Page is an alternative to this.
out
session
application
config
pagecontext

29
<!-- ComputeLoan.html -->
<html> Example 40.2
<head> Computing Loan
<title>ComputeLoan</title>
Write an HTML page that prompts
</head>
<body>
the user to enter loan amount,
Compute Loan Payment
annual interest rate, and number
of years. Clicking the Compute
<form method="get" action="ComputeLoan.jsp"> Loan Payment button invokes a
<p>Loan Amount JSP to compute and display the
<input type="text" name="loanAmount"><br> monthly and total loan payment.
Annual Interest Rate
<input type="text" name="annualInterestRate"><br>
Number of Years <input type="text" name="numberOfYears"
size="3"></p>
<p><input type="submit" name="Submit" value="Compute Loan
Payment">
<input type="reset" value="Reset"></p>
</form>
</body>
</html>

30
<!-- ComputeLoan.jsp -->
<html>
<head>
<title>ComputeLoan</title>
Predefined
</head>
variable
<body>
<% double loanAmount = Double.parseDouble(
request.getParameter("loanAmount"));
double annualInterestRate = Double.parseDouble(
request.getParameter("annualInterestRate"));
double numberOfYears = Integer.parseInt(
request.getParameter("numberOfYears"));
double monthlyInterestRate = annualInterestRate / 1200;
double monthlyPayment = loanAmount * monthlyInterestRate /
(1 - 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12));
double totalPayment = monthlyPayment * numberOfYears * 12; %>
Loan Amount: <%= loanAmount %><br>
Annual Interest Rate: <%= annualInterestRate %><br>
Number of Years: <%= numberOfYears %><br>
<b>Monthly Payment: <%= monthlyPayment %><br>
Total Payment: <%= totalPayment %><br></b>
</body>
</html>

31
JSP Directives
A JSP directive is a statement that gives the JSP engine information about the
JSP page. For example, if your JSP page uses a Java class from a package other
than the java.lang package, you have to use a directive to import this package.
The general syntax for a JSP directive is as follows:
<%@ directive attribute="value" %>, or
<%@ directive attribute1="value1"
attribute2="value2"
...
attribute="vlauen" %>

32
Three JSP Directives
Three possible directives are the following: page, include, and taglib.

page lets you provide information for the page,


include such as importing classes and setting up content
taglib type. The page directive can appear anywhere in
the JSP file.

33
Three JSP Directives
Three possible directives are the following: page, include, and taglib.
page
include lets you insert a file to the servlet when
the page is translated to a servlet. The include
taglib directive must be placed where you want the file
to be inserted.

34
Three JSP Directives
Three possible directives are the following: page, include, and taglib

page tablib lets you define custom tags.


include

35
<!-- ComputeLoan.jsp -->
<html> Example: Computing
<head>
Loan Using the Loan
<title>ComputeLoan Using the Loan Class</title>
</head>
Class
<body> Use the Loan class to simplify
<%@ page import = "chapter40.Loan" %> Example 40.2. You can create
<% double loanAmount = Double.parseDouble( an object of Loan class and use
request.getParameter("loanAmount")); its monthlyPayment() and
double annualInterestRate = Double.parseDouble( totalPayment() methods to
request.getParameter("annualInterestRate")); compute the monthly payment
int numberOfYears = Integer.parseInt( and total payment.
request.getParameter("numberOfYears"));
Loan loan = new Loan(annualInterestRate, numberOfYears, Import a class. The class must be
loanAmount); placed in a package (e.g. package
%> chapter40.
Loan Amount: <%= loanAmount %><br>
Annual Interest Rate: <%= annualInterestRate %><br>
Number of Years: <%= numberOfYears %><br>
<b>Monthly Payment: <%= loan.monthlyPayment() %><br>
Total Payment: <%= loan.totalPayment() %><br></b>
</body>
</html>

36
What is JSTL?
JSTL (JSP Standard Tag Libraries) is a collection of JSP
custom tags developed by Java Community Process,
www.jcp.org. The reference implementation is developed
by the Jakarta project, jakarta.apache.org.

JSTL allows you to program your JSP pages using tags,


rather than the scriptlet code that most JSP programmers
are already accustomed to. JSTL can do nearly everything
that regular JSP scriptlet code can do.

37
JSTL Tags

<%@ taglib prefix="c"


uri="http://java.sun.com/jstl/core" %>
...
<c:out value="${1 + 2 + 3}" />
JSP Standard Tag Library
Built on custom tag infrastructure
<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<html>
<head>
<title>JSP is Easy</title>
</head>
<body bgcolor="white">
<h1>JSP is as easy as ...</h1>
1 + 2 + 3 = <c:out value="${1 + 2 + 3}" />
</body>
</html>
JSTL Control Tags

<%@ page contentType="text/html" %>


<%@ taglib prefix="c “uri="http://java.sun.com/jstl/core" %>

<c:if test="${2>0}">
It's true that (2>0)!
</c:if>

<c:forEach items="${paramValues.food}" var="current">


<c:out value="${current}" />&nbsp;
</c:forEach>
COUNT TO TEN EXAMPLE USING SCRIPTLET:
JSTL was introduced to allow JSP programmers to program using tags rather than Java code.
To show why this is preferable, a quick example is in order. We will examine a very simple JSP page
that counts to ten.
We will examine this page both as regular scriptlet-based JSP, and then as JSTL. When the count to
ten example is programmed using scriptlet based JSP, the JSP page appears as follows.`

<html>
<head>
<title>Count to 10 in JSP scriptlet</title>
</head>
<body>
<% for(int i=1;i<=10;i++)
{%>
<%=i%><br/>
<%
}
%>
</body>
</html>

41
COUNT TO TEN EXAMPLE USING JSTL:
The following code shows how the count to ten example would be written
using JSTL. As you can see, this code listing is much more constant, as only
tags are used. HTML and JSTL tags are mixed to produce the example.

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>


<html>
<head>
<title>Count to 10 Example (using JSTL)</title>
</head>

<body>
<c:forEach var="i" begin="1" end="10" step="1">
<c:out value="${i}" />

<br />
</c:forEach>
</body>
</html>

42
COUNT TO TEN EXAMPLE USING
JSTL:
When you examine the preceding source code, you can
see that the JSP page consists entirely of tags.

The code makes use of HTML tags such as <head> and


<br>. The use of tags is not confined just to HTML tags. We
can modify that code to print numbers from 1 to n. The
code is given in the next slide.

This code also makes use of JSTL tags such as <c:forEach>


and <c:out>. In this article you will be introduced to some of
the basics of JSTL.

43
THE JSTL TAG LIBRARIES
The JSTL tags can be classified, according to their functions,
into following JSTL tag library groups that can be used when
creating a JSP page:
◦ Core Tags
◦ Formatting tags
◦ SQL tags
◦ XML tags
◦ JSTL Functions
44
Core Tags:
The core group of tags are the most frequently used JSTL tags. Following is
the syntax to include JSTL Core library in your JSP:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
There are following Core JSTL Tags:

Tag Description

<c:out > Like <%= ... >, but for expressions.

<c:set > Sets the result of an expression evaluation in a 'scope'

<c:remove > Removes a scoped variable (from a particular scope, if specified).

<c:catch> Catches any Throwable that occurs in its body and optionally exposes it.

<c:if> Simple conditional tag which evalutes its body if the supplied condition is true.

<c:choose> Simple conditional tag that establishes a context for mutually exclusive conditional
operations, marked by <when> and <otherwise>

<c:when> Subtag of <choose> that includes its body if its condition evalutes to 'true'.

<c:otherwise > Subtag of <choose> that follows <when> tags and runs only if all of the prior
conditions evaluated to 'false'.

<c:import> Retrieves an absolute or relative URL and exposes its contents to either the page,
a String in 'var', or a Reader in 'varReader'.

<c:forEach > The basic iteration tag, accepting many different collection types and supporting
subsetting and other functionality .

<c:forTokens> Iterates over tokens, separated by the supplied delimeters.

<c:param> Adds a parameter to a containing 'import' tag's URL.

<c:redirect > Redirects to a new URL.


45
Formatting tags:
The JSTL formatting tags are used to format and display text, the date, the
time, and numbers for internationalized Web sites. Following is the syntax to
include Formatting library in your JSP:
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
Following is the list of Formatting JSTL Tags:

Tag Description
<fmt:formatNumber> To render numerical value with specific precision or format.
<fmt:parseNumber> Parses the string representation of a number, currency, or
percentage.
<fmt:formatDate> Formats a date and/or time using the supplied styles and pattern
<fmt:parseDate> Parses the string representation of a date and/or time

<fmt:bundle> Loads a resource bundle to be used by its tag body.


<fmt:setLocale> Stores the given locale in the locale configuration variable.

<fmt:setBundle> Loads a resource bundle and stores it in the named scoped variable or
the bundle configuration variable.
<fmt:timeZone> Specifies the time zone for any time formatting or parsing actions
nested in its body.
<fmt:setTimeZone> Stores the given time zone in the time zone configuration variable
<fmt:message> To display an internationalized message.
<fmt:requestEncoding> Sets the request character encoding
46
SQL tags:
The JSTL SQL tag library provides tags for interacting with relational databases (RDBMSs) such
as Oracle, mySQL, or Microsoft SQL Server.
Following is the syntax to include JSTL SQL library in your JSP:
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
Following is the list of SQL JSTL Tags:

Tag Description
<sql:setDataSource> Creates a simple DataSource suitable only for
prototyping
<sql:query> Executes the SQL query defined in its body or
through the sql attribute.
<sql:update> Executes the SQL update defined in its body or
through the sql attribute.
<sql:param> Sets a parameter in an SQL statement to the
specified value.
<sql:dateParam> Sets a parameter in an SQL statement to the
specified java.util.Date value.
<sql:transaction > Provides nested database action elements with a
shared Connection, set up to execute all
statements as one transaction. 47
XML TAGS:
The JSTL XML tags provide a JSP-centric way of creating and manipulating XML
documents. Following is the syntax to include JSTL XML library in your JSP:

<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>

The JSTL XML tag library has custom tags for interacting with XML data. This
includes parsing XML, transforming XML data, and flow control based on XPath
expressions.

Before you proceed with the examples, you would need to copy following two
XML and XPath related libraries into your <Tomcat Installation Directory>\lib:

◦ XercesImpl.jar: Download it from http://www.apache.org/dist/xerces/j/


◦ xalan.jar: Download it from http://xml.apache.org/xalan-j/index.html

48
Following is the list of XML JSTL Tags:

Tag Description
<x:out> Like <%= ... >, but for XPath expressions.
Use to parse XML data specified either via an
<x:parse>
attribute or in the tag body.
<x:set > Sets a variable to the value of an XPath expression.
Evaluates a test XPath expression and if it is true, it
<x:if > processes its body. If the test condition is false, the
body is ignored.
<x:forEach> To loop over nodes in an XML document.
Simple conditional tag that establishes a context for
<x:choose> mutually exclusive conditional operations, marked by
<when> and <otherwise>
Subtag of <choose> that includes its body if its
<x:when >
expression evalutes to 'true'

Subtag of <choose> that follows <when> tags and runs


<x:otherwise >
only if all of the prior conditions evaluated to 'false'

<x:transform > Applies an XSL transformation on a XML document


Use along with the transform tag to set a parameter in
<x:param >
the XSLT stylesheet 49
JSTL Functions:
JSTL includes a number of standard functions, most of which are common string
manipulation functions. Following is the syntax to include JSTL Functions library in your
JSP:
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
Following is the list of JSTL Functions:
Function Description
fn:contains() Tests if an input string contains the specified substring.
fn:containsIgnoreCase( Tests if an input string contains the specified substring
) in a case insensitive way.
fn:endsWith() Tests if an input string ends with the specified suffix.
fn:escapeXml() Escapes characters that could be interpreted as XML
markup.
fn:indexOf() Returns the index within a string of the first occurrence
of a specified substring.
fn:join() Joins all elements of an array into a string.
fn:length() Returns the number of items in a collection, or the
number of characters in a string.
fn:replace() Returns a string resulting from replacing in an input
string all occurrences with a given string.
fn:split() Splits a string into an array of substrings.
50
Example
Loan Calculator

Header.jsp
Gets input to compute loan from user  index.jsp Footer.jsp

Selects the right jsp for calculating loan  controller.jsp

Computes loan based on Header.jsp Computes loan based on


simple interest
simple.jsp Footer.jsp compound.jsp simple interest

error.jsp Calculate loan error.jsp Calculate loan


Handles error if Handles error if
exception is thrown exception is thrown
Loan Calculator
index.jsp
<html> <tr>
<head> <td>Simple:</td>
<title>Include</title> <td><input type="radio" name="type" value="S" /></td>
</head> </tr>
<body style="font-family:verdana;font-size:10pt;">
<tr>
<%@ include file="header.html" %>
<td>Period:</td>
<form action="controller.jsp">
<td><input type="text" name="period"/></td>
<table border="0" style="font-family:verdana;font-size:10pt;">
</tr>
<tr>
</table>
<td>Amount:</td>
<input type="submit" value="Calculate"/>
<td><input type="text" name="amount" />
</form>
</tr>
<jsp:include page="footer.jsp"/>
<tr>
<td>Interest in %:</td> </body>

<td><input type="text" name="interest"/></td> </html>


</tr>
<tr>
<td>Compound:</td>
<td><input type="radio" name="type" value="C" checked/></td>
</tr>
Loan Calculator
Miscelaneous
controller.jsp error.jsp
<% <%@ page isErrorPage="true" %>
String type = request.getParameter("type"); <html>

if(type.equals("S")) { <head>
<title>Simple</title>
%>
</head>
<jsp:forward page="/simple.jsp"/>
<body style="font-family:verdana;font-size:10pt;">
<%
<%@ include file="header.html" %>
} else {
<p style="color=#FF0000"><b><%=
%> exception.getMessage() %></b></p>
<jsp:forward page="/compound.jsp"/> <jsp:include page="footer.jsp"/>

<% </body>

} </html>
header.jsp
%>
<h3>Loan Calculator</h3>
footer.jsp
<%= new java.util.Date() %>
Loan Calculator
simple.jsp
<%@ page errorPage="error.jsp" %> <html>
<%! <head>

public double calculate(double amount, double interest, <title>Simple</title>


int period) { </head>
if(amount <= 0) { <body style="font-family:verdana;font-size:10pt;">
throw new IllegalArgumentException("Amount should <%@ include file="header.html" %>
be greater than 0: " + amount);
<%
} double amount =
if(interest <= 0) { Double.parseDouble(request.getParameter("amount")
);
throw new IllegalArgumentException("Interest should
double interest =
be greater than 0: " + interest);
Double.parseDouble(request.getParameter("interest")
} );

if(period <= 0) { int period =


Integer.parseInt(request.getParameter("period"));
throw new IllegalArgumentException("Period should %>
be greater than 0: " + period);
<b>Pincipal using simple interest:</b>
}
<%= calculate(amount, interest, period) %>
return amount*(1 + period*interest/100);
<br/><br/>
}
<jsp:include page="footer.jsp"/>
%> </body>
</html>
Loan Calculator
compound.jsp
<%@ page errorPage="error.jsp" %> <html>
<%! <head>

public double calculate(double amount, double interest, <title>Compound</title>


int period) { </head>
if(amount <= 0) { <body style="font-family:verdana;font-size:10pt;">
throw new IllegalArgumentException("Amount should <%@ include file="header.html" %>
be greater than 0: " + amount);
<%
} double amount =
if(interest <= 0) { Double.parseDouble(request.getParameter("amount
"));
throw new IllegalArgumentException("Interest should
double interest =
be greater than 0: " + interest);
Double.parseDouble(request.getParameter("interest
} "));

if(period <= 0) { int period =


Integer.parseInt(request.getParameter("period"));
throw new IllegalArgumentException("Period should %>
be greater than 0: " + period);
<b>Pincipal using compound interest:</b>
}
<%= calculate(amount, interest, period) %>
return amount*Math.pow(1 + interest/100, period);
<br/><br/>
}
<jsp:include page="footer.jsp"/>
%> </body>
</html>
Conclusion
JavaServer Pages (JSP) lets you separate the dynamic part of
your pages from the static HTML.

1. One can simply write the regular HTML in the normal


manner, using whatever Web-page-building tools you
normally use.
2. One can enclose then the code for the dynamic parts in
special tags, most of which
start with "<%"
and end with "%>"

56
Review JSP

Open kahoot..
Developing a View Component
Design a view component
Develop a simple HTTP servlet
Configure and deploy a servlet
Developing a controller
component

You might also like