Java Server Pages - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Java Server Pages

Description:

Used to initialize a bean. Only called once jsp:useBean ... h4 The bean is created /h4 ... private String surname; public String getName(){ return name; ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 22
Provided by: fredri9
Category:
Tags: java | pages | server | surname | the | used

less

Transcript and Presenter's Notes

Title: Java Server Pages


1
Java Server Pages
  • An introduction to JSP

2
Containers and Components
  • Several clients one system

3
Java Server Pages (JSP)
  • HTML with embedded Java code
  • Good for presentation
  • No print statements
  • Compiled to a Servlet at runtime
  • Good performance
  • Easy deployment
  • When a JSP is updated its recompiled
  • Possible to compile manually
  • Easy for web designers to use
  • Edited in an ordinary HTML editor
  • Support for Java Beans and Custom tags

4
JSP Directives
  • At the top of a JSP page, the page directive is
    used
  • lt_at_ page namevalue gt

5
JSP Page Directive parameters
  • language
  • Default is java
  • extends
  • Default is the base JSP class, but you could use
    your own
  • importpackage1,package2, class1, class2
  • session
  • True or False, determines if sessions should be
    automatically created. True is default

6
JSP Page Directive parameters
  • buffer
  • The output buffer size
  • autoFlush
  • True or false, determines if the output should be
    flushed to the client. True is default
  • isThreadSafe
  • True or false. True is default
  • info
  • Descriptive text

7
JSP Page Directive parameters
  • errorPage
  • The page to go to in case of an error
  • contentType
  • isErrorPage
  • Defines if this is a error page. False is
    default. If its an error page a couple of extra
    objects are available in the page

8
Other Directives
  • Include directive
  • lt_at_ include filerelative url gt
  • Includes the file BEFORE compilation
  • Only static content

9
JSP Implicit objects
  • In JSPs, a couple of objects are allways
    available
  • request
  • Subclass of ServletRequest
  • getParameter(), getParameterNames()
  • getAttribute()
  • Response
  • Subclass of ServletResponse
  • Not used in normal situations
  • pageContext
  • Used to get configuration variables for this page
  • getAttribute()

10
JSP Implicit objects
  • session
  • The HttpSession
  • Created automatically if session is not false in
    the page directive
  • application
  • javax.servlet.ServletContext
  • Used to get information about the application
  • out
  • Used to print (in scriptlets)
  • clear(), clearBuffer(), flush()
  • config
  • javax.servlet.ServletConfig
  • getInitParameter(), getInitParameterNames()
  • page
  • exception

11
JSP Implicit objects
  • exception
  • Only in error pages
  • getMessage()
  • printStackTrace()

12
Java Code
  • Three ways to use Java Code in your JSP
  • lt! Declaration gt
  • Declares member variables
  • NOT thread safe
  • lt MyClass.printMessage() gt
  • Used to print single expressions

13
Java Code
  • lt any java code gt
  • Can contain the entire application
  • Use with care
  • Can give very hard to read code
  • lt if(status 1)
  • gt
  • lth3gtYolt/h3gt
  • lt
  • Else
  • gt
  • lth3gtOylt/h3gt
  • lt gt

14
JSP Tags
  • There are a couple of tags available and you can
    extend the tag set with your own tags
  • XML Syntax
  • Doesnt distract HTML authoring tools

15
Forward
  • ltjspforward pagerelative URL /gt
  • Forwards the request

16
Include
  • ltjspinclude pagerelative url
    flusthtrue/false /gt
  • Includes the output in runtime
  • Can be dynamic content

17
UseBean
  • ltjspuseBean idinstance name
    scopepagerequestsessionapplication
    beanNamepackage.classclasspackage.class /gt
  • Used to initialize a bean. Only called once
  • ltjspuseBean gt
  • lth4gtThe bean is createdlt/h4gt
  • lt/jspuseBeangt

18
GetProperty
  • Used to call getXXX methods in a bean
  • ltjspgetProperty namebean instance name
    propertyname /gt
  • Will call bean.getName()
  • Name is the same as id in ltjspuseBeangt

19
SetProperty
  • Used to call setXXX methods in a bean
  • ltjspsetProperty nameinstance name
    propertypropertyname valuevalue /gt
  • When is used as property name, a setXXX for
    value in getParameterNames() will be called

20
Example
  • package test1
  • class TestBean
  • private String name
  • private String surname
  • public String getName()
  • return name
  • public String getSurname()
  • return surname
  • public void setSurname(String _surname)
  • surname_surname
  • public void setName(String name_name)
  • name_name

21
Example cont.
  • lt_at_ page langueajava sessiontrue gt
  • ltjspuseBean idtb classtest1.TestBean
    scopesessiongt
  • ltigtThe bean is created lt/igt
  • ltjspgetProperty nametb propertyname /gt
  • ltjspsetProperty nametb propertyname
    valueFredrik /gt
Write a Comment
User Comments (0)
About PowerShow.com