y.j.f.cartwrightstaffs.ac.uk - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

y.j.f.cartwrightstaffs.ac.uk

Description:

We will use a (free) commercial server called Tomcat which is a part of the open ... They are HTML documents that are interleaved with Java which provides the ... – PowerPoint PPT presentation

Number of Views:125
Avg rating:3.0/5.0
Slides: 29
Provided by: yvancar
Category:

less

Transcript and Presenter's Notes

Title: y.j.f.cartwrightstaffs.ac.uk


1
MSc Object Oriented Software Engineering
  • Java Server Pages A Recap...

2
Java Server Pages
  • Introduction
  • What are Java Server Pages? (JSPs)
  • What do you need to run JSPs?

3
Evolution of Applications
  • One-tier - mainframe based/dumb terminal/central
    application source.
  • Standalone/PC/Mini computers/multi copy of
    application.
  • Two tier - client-server - distributed part of
    the application on the client(GUI) and part on
    the server.
  • Early versions of client-server required
    installation of GUI and business logic.
  • Recently, Java applets downloaded on demand. With
    server side Servlets and JSP there is no download
    and the browser is used as the interface
  • This is a good solution!

4
Evolution of Applications
  • Three tier/n-tier - user interface(UI) layer the
    business layer(BL) and the database layer(DL)
  • UI - presentation of information browsers such as
    Netscape or Explorer on the client
  • BL - logic of procedures about the business
  • DL - where the data is stored - usually a
    relational database but could be an object
    oriented database

5
Benefits of Web-Enabledn-tier Applications
  • Web applications can be called self-service
    applications - they enable things to be done
    from a web browser - e.g. online banking.
  • Run from anywhere - installation free.
  • Cost reduction
  • Browser based interface is easy to use - little
    training required.
  • Development costs lower as using a standard
    interface.
  • No installation.
  • Use of open standards
  • Maintenance easier.
  • Enhancement of applications easier.

6
Using Java Server Pages
  • In this module, we are going to create
    web-enabled applications using Java Server Pages
    (JSP).
  • Just like when using ASP, we need a server that
    understands our JSP code.
  • We need an http server which supports JSP and
    Java Beans as well as other more advanced
    technologies.
  • We will use a (free) commercial server called
    Tomcat which is a part of the open-source Jakarta
    project run by the Apache Software Foundation.
  • http//jakarta.apache.org/site/downloads/downloads
    _tomcat-5.cgi
  • The current production release is version 5.5.7

7
What are Java Server Pages?
  • They are HTML documents that are interleaved with
    Java which provides the dynamic content.
  • JSPs are server-side - they accept a request and
    generate a response (an HTML document).
  • JSP is the next generation on from Servlets - an
    extension of Servlets with more power and
    flexibility.
  • JSPs are the equivalent (and forerunner) of
    Microsofts Active Server Pages .NET (ASP.NET)
    technology.
  • Unlike ASP.NET (for the time being), JSPs are
    portable.

8
JSPs Servlets ???
  • JSPs are an extension of Java Servlet technology.
  • JSPs automatically generate Servlets which are
    Java class files that produce HTML code and are
    run on the server.
  • It is possible to code your own Servlets and
    include them in with your JSP web application.
  • A useful source of information for JSPs and
    Servlet technologies is the Java Server Pages
    homepage at Sun
  • http//java.sun.com/products/jsp/index.html

9
SimpleGuestList.jsp
  • ltHTMLgt
  • ltHEADgtltTITLEgtAn Example of a Simple JSP Accessing
    a Database Modellt/TITLEgtlt/HEADgt
  • ltBODYgt
  • ltCENTERgt
  • ltFONT COLOR"blue" SIZE"6"gt
  • A Listing Of Guests
  • lt/FONTgt
  • lt/CENTERgt
  • ltBRgtltBRgt
  • lt_at_ page import"java.sql." gt
  • ltTABLE WIDTH"100" BORDER"2" BGCOLOR"silver"gt
  • ltTRgtltTH WIDTH"50"gtEmaillt/THgt
  • ltTH WIDTH"25"gtFirstnamelt/THgt
  • ltTH WIDTH"25"gtLastnamelt/THgt
  • lt/TRgt

10
SimpleGuestList.jsp
  • lt
  • try
  • Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newI
    nstance()
  • Connection conn DriverManager.getConnection("jdb
    codbcDriverMicrosoft Access Driver
    (.mdb)DBQ//trentdev/wwwdatajsp/oose/youruserna
    me/GuestBook.mdb")
  • Statement statement conn.createStatement()
  • String sql "SELECT FROM GuestBook"
  • ResultSet rs statement.executeQuery(sql)
  • while (rs.next())
  • gt
  • ltTRgtltTDgtlt rs.getString("Email") gtlt/TDgt
  • ltTDgtlt rs.getString("FirstName") gtlt/TDgt
  • ltTDgtlt rs.getString("LastName") gtlt/TDgt
  • lt/TRgt
  • lt
  • gt
  • lt/TABLEgt

11
SimpleGuestList.jsp
  • lt
  • if (statement ! null)
  • statement.close()
  • if (conn ! null)
  • conn.close()
  • catch (Exception e) out.print(e)
  • gt
  • lt/BODYgt
  • lt/HTMLgt

12
Using Tomcat at University
  • The Tomcat server has been setup here and will be
    made available once your student cards have been
    issued and your student ID noted.
  • If your student ID was noted last week, then your
    accounts are already prepared.
  • To test your account, do the following
  • Map a network folder to \\trentdev\wwwdatajsp\oos
    e\yourstudentid
  • This is the web folder where your .jsp files go.
  • Using any available text editor, type in
    SimpleGuestList.jsp and save it to your web
    folder.
  • Access the page by going to http//trentdev8080/o
    ose/yourstudentid/SimpleGuestList.jsp

13
Using Tomcat at Home
  • What do you need to develop and run JSPs?
  • JDK1.2
  • Tomcat HTTP server
  • For database interaction we will start using MS
    Access and move on to MySQL later.
  • Follow the guide for setting up Tomcat on a PC.
  • Tomcat will run on Windows 98 (XP is better) and
    upwards.
  • You can also run it on a Linux box but the guide
    only shows how to install it on a Windows OS.

14
JSP Translation Process
Request JSP translated into a Servlet .java file
and then compiled into a Servlet ,class
Query for data content
HTTP Server TOMCAT
.jsp file (HTML interleaved with Java)
Backend database usually on the server
Response HTML document
RequestBrowser page link to call .jsp(either
via locator or a link on an HTML input page
Response HTML for output sent from Server
15
Sample Output
16
Quick Overview
  • A .jsp file is essentially HTML with Java code
    inserts.
  • When the .jsp file is called via a browser a
    .java file is created to represent Servlet source
    code and then the .java file is compiled into a
    bytecode file with a .class extension.
  • Both the .java and .class file can be viewed via
    a text editor but MUST not be edited.
  • The compilation is only done once - subsequent
    calls to the JSP use the already compiled .class
    file (fast!).

17
JSP Syntax Scriptlet Tags
  • An initial selection of tags to get to know
    (Scriptlet Tags v. important!!!).
  • lt //embed Java code gt
  • Used for embedding blocks of Java code into JSPs.
  • Scriptlets provide control structures like
    decision (if and switch) and iteration control
    structures (for and while).

18
Declaration Tags
  • Declaration Tags also important.
  • lt! // Variable declaration - method declaration
    gt
  • Used for declaring variables and methods. For
    example
  • lt! HttpSession ses null gt
  • lt! public String whereFrom(HttpServletRequest
    req)
  • ses req.getSession()
  • return req.getRemoteHost()
  • gt
  • lt out.print("Hi there, I see that you are
    coming in from ") gt
  • lt whereFrom(request) gt

19
Expression Tags
  • Expression Tags.
  • lt java expression gt
  • Used for inserting the value of a Java expression
    into JSPs.
  • e.g.
  • lttdgtlt rs.getString("LastName") gtlt/tdgt

20
Directive Tags
  • Directive Tags used to provide information to
    the JSP server.
  • There are three directives
  • page
  • include
  • taglib
  • We will only review the page and include
    directive today.
  • Page Directive - used to set properties of the
    JSP. E.g. what packages it imports from the Java
    library or is an error page.
  • lt_at_ page page_attributes gt e.g.
  • lt_at_ page import"java.sql." gt
  • lt_at_ page errorPagemyErrorPage.jsp" gt

21
Catching Errors myErrorPage.jsp
  • lt_at_ page isErrorPage"true" gt
  • ltHTMLgt
  • ltHEADgtltTITLEgtMy Error Pagelt/TITLEgtlt/HEADgt
  • ltBODYgt
  • ltH2gtException Informationlt/H2gt
  • ltTABLEgt
  • lttrgtlttdgtException Class lt/tdgtlttdgtlt
    exception.getClass() gtlt/tdgtlt/trgt
    lttrgtlttdgtMessage lt/tdgtlttdgtlt exception.getMessage
    () gtlt/tdgtlt/trgt lt/TABLEgt
  • lt/BODYgt
  • lt/HTMLgt

22
Action Tags
  • Action tags are used to perform actions when a
    JSP page is requested by a browser.
  • There are six JSP action tags
  • useBean
  • setProperty
  • getProperty
  • Include
  • Forward
  • plugin
  • We will review two of them today
  • include
  • forward

23
Include Action and Directive
  • Actions and Directives there is a difference
  • Actions are elements that can create and access
    programming language objects and affect the
    output stream.
  • include Action.
  • ltjspinclude page Relative_URL /gt
  • Used to include the output of another JSP.
  • include Directive.
  • lt_at_ include fileRelative_URL_to_file gt
  • Used to include the text of another file.

24
Include Example
  • lthtmlgt
  • ltbody bgcolor"white"gt
  • ltfont color"red"gt
  • lt_at_ page buffer"5" autoFlush"false" gt
  • ltpgtThis line adds the incsource.jsp code to this
    pages source code before compilation.
  • lt_at_ include filefoo.jsp" gt
  • lt/pgt
  • ltpgtThis line adds the output of foo.jsp to this
    pages response buffer.
  • ltjspinclude pagefoo.jsp" flush"true"/gt
  • lt/pgt
  • lt/htmlgt

25
Action or Directive?
  • Well both have their pros and cons and, as
    always, it just depends on what your requirements
    are. The benefits of using the ltjspinclude/gt
    are
  • Guarantees automatic recompilation,
  • Smaller class sizes,
  • Can make use of parameters (treated like form
    parameters)
  • JSP expressions can be used in attribute values
    and
  • Can include text from any source.
  • One of the main benefits of using the include
    directive is that local page variables can be
    shared between the two files. It also has
    slightly better run time efficiency and doesn't
    restrict output buffering.

26
Forward Tag
  • ltjspforward pageRelative_URL /gt
  • Used to forward the request to another JSP
  • This will forward the current request object to
    the specified file
  • When a forward tag is encountered, the JSP engine
    doesn't process the remainder of the current JSP
    file
  • Anything that has been written into the output
    buffer by the current JSP file will be lost
  • Used to simply redirect the users request to
    another page.

27
Forward Tag Example
  • lt
  • double freeMem Runtime.getRuntime().freeMemor
    y()
  • double totlMem Runtime.getRuntime().totalMemo
    ry()
  • double percent freeMem/totlMem
  • if (percent lt 0.5)
  • gt
  • ltjspforward page"/jsp/forward/one.jsp"/gt
  • lt else gt
  • ltjspforward page"two.html"/gt
  • lt gt

28
Summary
  • Weve seen how JSPs work.
  • We know what we need to run JSPs on a PC.
  • Weve seen most of the standard scriptlet tags.
  • It is important now to be able try out simple JSP
    pages (more examples on the website by end of
    week).
  • We can use JSP to create web-enabled
    object-oriented applications that utilise a
    backend database.
  • This is something we will be learning how to do
    on this module.
Write a Comment
User Comments (0)
About PowerShow.com