Title: XML
1DB3009N Week 7 Java Servlet Pages Part 1
CCTM Course material developed by James King
(james.king_at_londonmet.ac.uk)
2Resources
- http//www.apl.jhu.edu/hall/java/Servlet-Tutorial
/ contains a good tutorial on JSP
CCTM Course material developed by James King
(james.king_at_londonmet.ac.uk)
3What are JSPs?
- Servlets contain standard Java code and send HTML
to the web browser using a printWriter and
println - JSPs are built on top of servlets and converted
into servlets when loaded by the web server - JSPs look much more like standard HTML pages
- Code is contained within lt gt markers and are
referred to a scriptlets - Each JSP page says which programming language is
contained within its scriplets and therefore
allows the use of multiple programming languages
in a project. In practice the language is
normally Java
CCTM Course material developed by James King
(james.king_at_londonmet.ac.uk)
4Simple JSP Example
Specifies which language the scriptlets are
written in
Java import statement
lt_at_page language"java" import"java.util.Date"
gt ltHTMLgt ltBODYgt ltH1gtWelcome to JSPlt/H1gt ltBgt
Current Time is lt new Date().toString() gt
lt/Bgt lt/BODYgt lt/HTMLgt
Java scriptlet
Welcome to JSP Current Time is Tue April 24
190055 GMT0000 2001
CCTM Course material developed by James King
(james.king_at_londonmet.ac.uk)
5Equivalent Servlet
- import java.io.
- import javax.servlet.
- import javax.servlet.http.
- Import java.util.Date
- public class ServWelcome extends HttpServlet
-
- public void doGet(HttpServletRequest request,
HttpServletResponse response) throws
IOException, ServletException -
- response.setContentType("text/html")
- PrintWriter out response.getWriter()
- out.println("ltHTMLgt")
- out.println("ltBODYgt")
- out.println("ltH1gtWelcome to
Servletslt/H1gt") - out.println(" ltBgtCurrent Time is " new
Date().toString()"lt/Bgt) - out.println("lt/BODYgt")
- out.println("lt/HTMLgt")
- out.close()
-
CCTM Course material developed by James King
(james.king_at_londonmet.ac.uk)
6JSP vs. Servlets
- To understand JSP you need to understand Servlets
first - You also need to understand how to control the
conversion process form JSP to Servlet using
directives - Simple things are easier to do in JSP than
Servlets because Servlets require more
initialisation code - Using JSP avoids the need to write lots of
out.println("..") each time you want to send
HTML to the browser - JSP mixes code into the HML, Large JSP pages can
be difficult to find and read the code - Servlets mix HTML into the code, Large servlets
can be difficult to find and read the HTML - If there is little HTML output and lots of Java
code servlets may be simpler - Debugging JSP is difficult because the code is
translated into another form (servlet) before
being run
CCTM Course material developed by James King
(james.king_at_londonmet.ac.uk)
7JSP vs Existing Technologies
- vs CGI (A simple method to load and execute
code) - CGI loads the script and creates a process for
each request - JSP only loads a script if it is not already in
memory. Each request is handled by a thread - CGI runs as a separate entity from the web
browser - JSP runs in a Java Virtual Machine as an
extension to the Web server - With CGI portability depends on if the code
loaded is portable - JSP is naturally portable
- vs ASP
- ASP is not portable
- Often tied to using Microsoft IIS which is very
insecure - Code compiled for each request Java only compiles
again if the code has changed since the last
request - vs .NET ASP (large framework over the top of the
internet) - Little support for portability some windows
versions don't even support it - Choice of .NET language such as C or VB stops
portability - VB is out of date and nasty, C is new and not
well established
CCTM Course material developed by James King
(james.king_at_londonmet.ac.uk)
8How do JSP Work?
- When a Web server detects .jsp extension in the
URL, it delegates the request to JSP engine. - The JSP page is translated into a Java Servlet
- The generated Servlet is loaded by the Servlet
engine and handles the request - Note
- The translation and compilation takes place only
when the JSP is called first time or when it is
modified. All subsequent requests are handled by
the Servlet generated - There is a slight delay in response first time
due to translation and compilation phase - If there are any changes, Java/HTML page
recompiles automatically
CCTM Course material developed by James King
(james.king_at_londonmet.ac.uk)
9JSP Translation and Compilation
CCTM Course material developed by James King
(james.king_at_londonmet.ac.uk)
10Processing a JSP Document
CCTM Course material developed by James King
(james.king_at_londonmet.ac.uk)
11JSP Scripting Elements The Types
lt_at_page language"java" gt ltHTMLgt ltBODYgt ltH1gt
Counter lt/H1gt lt! int i 0 gt lt i
gt ltBgtCounter Value lt i gt lt/Bgt lt_at_
include file"cpRightTime.jsp" gt
lt/BODYgt lt/HTMLgt
CCTM Course material developed by James King
(james.king_at_londonmet.ac.uk)
12Scriptlets
- Scriptlets are Java code which is executed when a
request from the web browser is received. - They are enclosed within lt ... gt
- lt i gt
- You can output HTML within the code by finishing
the Scriptlet, writing the HTML and then starting
a new Scriptlet - lt if (ilt10)
- gt
- ltBgt I is less than 10 lt/Bgt
- lt
- else
- gt
- ltBgt I is equal or greater than 10 lt/Bgt
- lt gt
CCTM Course material developed by James King
(james.king_at_londonmet.ac.uk)
13Scriptlets
- Notice the structure of the java code. The first
part of the if is split between two Scriptlets as
is the else part. This is perfectly legal - lt if (ilt10)
- gt
- ltBgt i is less than 10 lt/Bgt
- lt
- else
- gt
- ltBgt i is equal or greater than 10 lt/Bgt
- lt gt
- After translation the code in the Scriptlets
becomes part of _jspService() method which
functions in the same way as service() method in
Servlets.
CCTM Course material developed by James King
(james.king_at_londonmet.ac.uk)
14Expressions
- An expression is evaluated when the request is
made and the result is converted into a String
and send to the client via the Servlets'
PrintWriter - They are enclosed within lt ... gt
- They can be inserted directly into the html
- ltBgt i is less than 10 in fact i is lt i gt lt/Bgt
- Expressions can contain calculations. This result
of the calculation is sent to the client. - ltBgt i times 10 is lt i10 gt lt/Bgt
- Any expression (but not text strings) that you
can put inside a System.out.println() in Java can
be placed inside a JSP expression.
CCTM Course material developed by James King
(james.king_at_londonmet.ac.uk)
15Declarations
- Declarations allow you to create objects and
simple variables when a request is made which can
be used by expressions and Scriptlets in the same
JSP page - Declarations are enclosed within lt! ... gt
- Declarations become attributes of the Servlet
the JSP is translated into. - lt! int i gt
- You can use the normal access modifiers public,
protected and private to reduce the scope of the
attribute - lt! private int salary5000 gt
CCTM Course material developed by James King
(james.king_at_londonmet.ac.uk)
16Directives
- Directives control the translation process from
JSP to a Servlet and are not executed during a
client request - Directives are enclosed inside lt_at_ ... gt
- Directives inside a JSP page only effect that
page - There are three types of directive
- page. Allows you to import packages and classes
and control inheritance. The page directive can
be placed anywhere in the JSP page. However, like
Java imports and inheritance it is a good idea to
place it near the start of the page - include. Allows you to insert an entire JSP page
from another file into the JSP page. The include
directive should be placed where you want the
second JSP page to be inserted - taglib. Allows you to create new tags ( lt gt is
a tag ). This is not covered in this course
CCTM Course material developed by James King
(james.king_at_londonmet.ac.uk)
17Directives
- All directives have the form
- lt_at_ name-of-directive attribute-name"value-of-att
ribute" gt - The value of the attribute must be inside ""
- lt_at_ page import"java.util.Date" gt
- lt_at_ page import"java.util.Date" gt
CCTM Course material developed by James King
(james.king_at_londonmet.ac.uk)
18Directives page import
- Importing a package
- Don't forget the package name has to be inside ""
- lt_at_ page import"java.util.Date." gt
- Importing several packages
- List all the packages inside a single set of ""
with a , between each package name - lt_at_ page import"java.util.Date.,mypackage." gt
CCTM Course material developed by James King
(james.king_at_londonmet.ac.uk)
19Directives page language
- The language directive tells the system what
programming language your JSP page is written in. - The default is Java
- lt_at_ page language"java" gt
CCTM Course material developed by James King
(james.king_at_londonmet.ac.uk)
20Implicit Objects
- JSP is built on top of Servlets and provides
objects to access many of the things a Servlet
can access. - request access to the servlets'
HttpServletRequest - response access to the servlets'
HttpServletResponse - out access to the Servlets' out PrintWriter to
send HTML to the client - session access to the session created for the
Servlet allowing access to per client data - application access to the Servlets'
ServletContext allowing access to per application
data - lt out.println("why am I doing this?") gt
- ltBgtThe machine connected to me is lt
request.getRemoteHost() gtltBgt
CCTM Course material developed by James King
(james.king_at_londonmet.ac.uk)
21Implicit Objects Getting Parameters
- A Servlet gets parameters from the request object
which is passed to the doGet or doPost methods.
JSP does not have these methods so instead we can
use the implicit object request to get at the
parameters. - ltHTMLgt
- ltBODYgt
- the value of parameter param1 is lt
response.getParameter("param1") gt
- lt/BODYgt
- lt/HTMLgt
- The static HRTML page to invoke this JSP page is
almost identical to the page which invokes the
equivalent Servlet - ltHTMLgt
- ltBODYgt
- lta href"http//www3.unl.ac.uk8186/kingj1/param.j
sp?param1hello world"gtJSP parameter examplelt/agt - lt/BODYgt
- lt/HTMLgt
CCTM Course material developed by James King
(james.king_at_londonmet.ac.uk)
22Implicit Objects cookies
- Since the JSP page also has a response object we
can add cookies - ltHTMLgt
- ltBODYgt
- lt! Cookie cnew Cookie("mycookie","myvalue") gt
- lt response.addCookie(c) gt
- lt/BODYgt
- lt/HTMLgt
CCTM Course material developed by James King
(james.king_at_londonmet.ac.uk)
23Directives page session
- If session is "true" then during the conversion
from JSP to Servlet Java adds code so that a
session called session is created and maintained
each time the JSP page is requested. - lt_at_ page session"true" gt
- lt_at_ page session"false" gt
- session "true" is the default.
- If session "false" then Java does not insert
code to initialise the session and you will get
an error if you try to access the session
attribute.
CCTM Course material developed by James King
(james.king_at_londonmet.ac.uk)
24Directives page errorPage
- A JSP page can generate exceptions.
- You can handle exceptions by inserting Java
exception handlers into your Scriptlets - lt
- try
-
- i10/0
-
- catch (Exception e)
-
-
- gt
CCTM Course material developed by James King
(james.king_at_londonmet.ac.uk)
25Directives page errorPage, isErrorPage
- Or you can arrange for the web server to execute
a special JSP page if there is an error - lt_at_page errorPage"error.jsp" gt
- The variable exception is made available to the
error page so that it can determine what was the
error - Note the output from the JSP page that caused the
exception is lost. Only output from the error
page is displayed by the client - The page that handles the error should have a
special page directive called isErrorPage set to
true - lt_at_ page isErrorPage"true"gt
CCTM Course material developed by James King
(james.king_at_londonmet.ac.uk)
26Directives page errorPage
- lt_at_ page errorPage"error.jsp"gt
- ltHTMLgt
- ltBODYgt
- ltBgt one divided by zero is lt 1/0 gt ltBgt
- lt/BODYgt
- lt/HTMLgt
- In error.jsp you can use the exception variable
to determine the error - lt_at_ page isErrorPage"true"gt
- ltHTMLgt
- ltBODYgt
- ltBgt sorry an error of type lt exception gt has
occurred ltBgt - lt/BODYgt
- lt/HTMLgt
CCTM Course material developed by James King
(james.king_at_londonmet.ac.uk)
27Directives include
- The include directive inserts the entire file
into the JSP page at the point where the include
direct is - This insert is done as part of the translation
process from JSP to a Servlet - The insert is not done at request time
ltHTMLgt ltBODYgt lt_at_ include file "Blah.jsp"
gt lt/BODYgt lt/HTMLgt
ltBgt hello world ltBgt
ltBgt hello world ltBgt
CCTM Course material developed by James King
(james.king_at_londonmet.ac.uk)
28Directives include advantages and Disadvantages
- The problem is that if you change the included
file the JSP standard does not state that the web
server should reinsert it into the main file and
recompile! - So if you change any included files the web
server may still run the JSP main page with the
out-of-date code from the old included JSP page. - you should make sure you recompile the main JSP
page when you change any included JSP! - This makes management of large multiple JSP page
projects difficult - The advantage of include is that because the
included file is inserted into the main JSP page - it can access anything declared in the main JSP
page - Anything declared in the included JSP page is
accessible inside the main page
CCTM Course material developed by James King
(james.king_at_londonmet.ac.uk)
29Directives jspinclude
- Just when you thought you understood the JSP
syntax here comes a new format - lt jspinclude page"blah.jsp"/gt
- This version of the include creates a separate
Servlet for the included JSP page. - At request time it executes the included JSP page
and the HTML output of that page is merged into
the output of the main page.
CCTM Course material developed by James King
(james.king_at_londonmet.ac.uk)
30Directives jspinclude
HTML output of main page
Client web browser
lt/BODYgt lt/HTMLgt lt jspinclude file "Blah.jsp"
/gt lt/BODYgt lt/HTMLgt
ltBgt hello world ltBgt
HTML output of jspincluded page
CCTM Course material developed by James King
(james.king_at_londonmet.ac.uk)
31Directives jspinclude advantages and
Disadvantages
- The included file is a separate JSP page
- Therefore the included page can not access
declarations in the main JSP page - The main JSP page can not access declarations in
the jspincluded page - Since the jspincluded page is executed at
request time any changes to it cause recompile
when the next request is made. The output of the
recompiled JSP page is then merged with the
output of the main JSP page - Main page does not need to be recompiled.
- Easier multi JSP page management
CCTM Course material developed by James King
(james.king_at_londonmet.ac.uk)
32Directives jspforward
- Jspforward works like jspinclude except that
the output of the main page is thrown away! (Some
books even say the main page must not generate
any output at all) - lt jspforward page"blah.jsp"/gt
- At request time it executes the forwarded JSP
page and the HTML output of that page replaces
the output of the main page.
CCTM Course material developed by James King
(james.king_at_londonmet.ac.uk)