j'c'westlakestaffs'ac'uk - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

j'c'westlakestaffs'ac'uk

Description:

... created (the response on the website) when a particular query is executed. ... This weeks Guestbook examples are available from the module schedule ... – PowerPoint PPT presentation

Number of Views:236
Avg rating:3.0/5.0
Slides: 22
Provided by: jonathan76
Category:

less

Transcript and Presenter's Notes

Title: j'c'westlakestaffs'ac'uk


1
Java Server Pages
  • Format of lecture.
  • Benefits of JSPs.
  • Structure of JSPs
  • Review of selected tags used in JSPs.
  • Walkthrough of example using tags mentioned.

2
Recap - Main benefits of JSPs
  • They can use tag libraries - it is possible to
    import a tag library i.e. import custom
    functionality (this has an XML feel?).
  • Separation of roles with development personnel
  • Graphical content can be performed by an HTML
    developer.
  • Dynamic content can be inserted by a Java
    developer - dynamic means the content created
    (the response on the website) when a particular
    query is executed.
  • It follows then that when developing JSPs it is
    appropriate to create the HTML first and then
    insert Java code later.
  • The following diagram depicts the request and
    response lifecycle.

3
Recap - JSP Translation process
JSP translated into a Servlet .java file and
then compiled into a Servlet ,class
Query for data content
.jsp file (HTML interleaved with Java)
HTTP Server Tomcat
Browser
Response HTML document Displayed on browser
Backend database usually on the server
Browser page link to call the .jsp (either via
URlocator or a URL link on an HTML page
4
JSP Built-in Objects
  • JSPs have access to several objects that we can
    use when processing a request from a browser and
    then generate a dynamic response.
  • There are eight in all
  • request out
  • response pagecontext
  • session application
  • config page

5
Built-in objects
  • You will see these objects being used in the
    examples provided during the module coverage of
    JSPs.
  • Request objects methods allow us to access
    information about
  • The user
  • HTTP headers
  • Clients machine
  • Request URL and parameters.

6
Built-in objects
  • Response objects methods allow access to
  • Setting HTTP headers.
  • Set cookies.
  • Encode session information into URLs.

7
Built-in objects
  • Out object - has methods to generate and control
    output to the browser.
  • PageContext object - has page attributes.
  • Page object - represents the servlet instance
    i.e. the object generated from having compiled
    the JSP.
  • Application object - used for extracting
    information about the lifecycle of the Servlet.

8
The Structure of JSPs
  • 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).

9
Structure of JSPs
  • 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

10
Structure of JSPs
  • 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

11
Structure of JSPs
  • 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.
  • 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

12
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

13
JSP 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 in during the module
  • include
  • forward

14
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
  • There are examples of both on the Tomcat server

15
include example
  • ltbody bgcolor"white"gt
  • ltfont color"red"gt
  • lt_at_ page buffer"5" autoFlush"false" gt
  • ltpgtIn place evaluation of another JSP which gives
    you the current time
  • lt_at_ include file"foo.jsp" gt
  • ltpgt ltjspinclude page"/jsp/include/foo.html"
    flush"true"/gt by including the output of another
    JSP
  • ltjspinclude page"foo.jsp" flush"true"/gt
  • -)
  • lt/htmlgt

include Action
include Directive
16
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.

17
forward Action
  • 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
  • The following example tests the virtual memory
    and either forwards to another .jsp or another
    html page - see code screen grab on next page

18
forward Action 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
  • lt/htmlgt

19
Further Guestbook demos
  • CRUD examples this week and next
    Create(add)Retrieve(view/get)Update and Delete
  • Adding information to the Guestbook
  • Demo of a add JSP (addtoGuestBook.jsp)
  • Retrieving (getting) information from the
    Guestbook database with JSPs
  • Demo of a simple Get JSP (SimpleGetGuestexample.js
    p)
  • Demo of a user input version of get
    (getusinginputexample.jsp)

20
Demos
  • First a demo of SimpleGetGuestexample.jsp and
    then a look at the source code notice the use
    of the SQL statement
  • Secondly a demo of getusinginputexample.jsp and
    then a look at the source code again notice the
    use of the SQL statement
  • Finally a demo of addtoGuestbook.jsp

21
Summary
  • You have learnt more about the structure of JSPs
  • Learning from examples is good
  • You have seen an Add something JSP
  • You have seen a more sophisticated view something
    JSP (compared to week1 of the module)
  • You are now in a position to apply this knowledge
    to the assignment task
  • This weeks Guestbook examples are available from
    the module schedule
Write a Comment
User Comments (0)
About PowerShow.com