Chapter 10 - SaigonTech

Download Report

Transcript Chapter 10 - SaigonTech

Chapter 10
Overview of JSP
Technology
Understanding the need for
JSP




JSP technology enables to mix regular, static
HTML with dynamically generated content.
Simply write the regular HTML in the normal
manner, using familiar Web-page-building
tools.
Then enclose the code for the dynamic parts
in special tags, most of which start with <%
and end with %>.
Example: Listing 10.1
Understanding the need for
JSP





Servlets as Java code with HTML inside
JSP as HTML with Java code inside
JSP pages and servlets, they are the same
thing.
JSP pages are translated into servlets, the
servlets are compiled.
At request time it is the compiled servlets
that execute
Understanding the need for
JSP


Writing JSP pages is really just another way
of writing servlets
Even though servlets and JSP pages are
equivalent behind the scenes, they are not
equally useful in all situations
The Need for JSP

Servlets have the following deficiencies
when it comes to generating the output



It is hard to write and maintain the HTML
You cannot use standard HTML tools
The HTML is inaccessible to non-Java
developers
Benefits of JSP



It is easier to write and maintain the
HTML
You can use standard Web-site
development tools
You can divide up your development
team
Installation of JSP Pages





Servlets require you to set your
CLASSPATH
Not so with JSP pages
JSP pages can be placed in the same
directories as normal HTML pages,
images, and style sheets
JSP Directories for Tomcat (Default
Web Application)
install_dir/webapps/ROOT
Basic Syntax

JSP Comment


JSP Expression


<%= Java Value %>
JSP Scriptlet


<%-- Blah --%>
<% Java Statement %>
JSP Declaration


<%! Field Definition %>
<%! Method Definition %>
Practice Exercise
Write program to calculate Internet
Service Fee.





Normal Customer: Discount 0%
Special Customer: Discount 10%
The Internet Service Open from 6 AM to 22
PM
Before 16PM charge per hour is 2000 VND.
After 16 PM charge per hour is 3000.
If customer use internet greater than 5
hours, he will receive discount 5%