Basic JSP - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Basic JSP

Description:

changing look and feel or a new client type require changes to the Servlet code ... extends org.apache.jasper.runtime.HttpJspBase. example_jsp.java ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 26
Provided by: tklak
Category:
Tags: jsp | basic | jasper

less

Transcript and Presenter's Notes

Title: Basic JSP


1
Basic JSP
Celsina Bignoli bignolic_at_smccd.net
2
Problems with Servlets
  • Servlets contain
  • request processing,
  • business logic
  • response generation
  • all lumped together!
  • Problems
  • good Java knowledge is needed to develop and
    maintain the application
  • changing look and feel or a new client type
    require changes to the Servlet code
  • cannot use web page development tools

3
Advantages of JSP
  • separates request processing and business logic
    from the presentation
  • places all static HTML in a JSP page and adds a
    few JSP elements to generate the dynamic content
  • allows the separation of tasks between Java
    programmers and WEB page authors
  • makes it easier to change presentation without
    affecting business layer and viceversa

4
A Simple JSP Page
JSP tag
lt_at_ page importjava.text., java.util.gt
lthtmlgt
ltheadgtlttitlegtltDate Examplelt/titlegtlt/headgt
HTML
ltbodygt
Scriptlet
Today is ltemgtlttodaygtlt/emgt lt/bodygt lt/htmlgt
Example.jsp
  • Static HTML/XML elements
  • Dynamic scriptlets
  • Special JSP elements

5
JSP Processing- Translation Phase
request
response
JSP Page (.jsp)
JSP Translator
Text Buffer
Java Compiler
Servlet Source (.java)
Servlet Class (.class)
JVM
Servlet Container
Translation Phase
Request Processing Phase
6
JSP Processing- Request Processing Phase
request
response
JSP Page (.jsp)
JSP Translator
Text Buffer
Java Compiler
Servlet Source (.java)
Servlet Class (.class)
JVM
Servlet Container
Translation Phase
Request Processing Phase
7
JSP Translation - Example
  • package jsp
  • import javax.servlet.
  • import javax.servlet.http.
  • import java.text.
  • import java.util.
  • public class example_jsp
  • extends org.apache.jasper.runtime.HttpJs
    pBase

example_jsp.java
extends javax.servlet.http.HttpServlet
8
service() method - Example
  • public void _jspService(HttpServletRequest
    request,
  • HttpServletResponse
    response)
  • throws IOException,
    ServletException
  • try
  • out pageContext.getOut()
  • out.write("lthtmlgt\r\n")
  • out.write("ltheadgtlttitlegtltDate
    Examplelt/titlegtlt/headgt\r\n")
  • out.write("ltbodygt\r\n")
  • Date dnew Date()
  • String today DateFormat.getInstance().form
    at(d)
  • out.write("\r\n")
  • out.write("Today is\r\n")
  • out.write("ltemgt")
  • out.print(today)
  • out.write("lt/emgt\r\n")
  • out.write("lt/bodygt\r\n")

9
JSP Elements
  • Directives
  • Comments
  • Actions
  • standard actions
  • custom actions and JSTL
  • Expression Language (EL)
  • Scripting
  • JavaBean components

10
JSP Directive Elements
  • Specify attribute of the page itself
  • type of content
  • buffering requirements
  • resources used
  • how runtime errors should be handled
  • Do not affect content of a response but how the
    container should handle it.
  • Enclosed between lt_at_ and and gt
  • Have a name and one or more attribute/value pairs
  • attribute and values are case-sensitive
  • values must be enclosed in single or double quotes

11
Page Directive
  • lt_at_page contentTypetext/html gt
  • other possible values for contentType
  • text/plain
  • text/xml
  • text/vnd.wap.wml
  • other possible attributes
  • errorPage
  • isErrorPage
  • session
  • pageEncoding
  • buffer
  • autoFlush

12
Taglib Directive
  • lt_at_taglib prefixc urihttp//java.sun.com/jsp/
    jstl/core gt
  • declares a JSTL custom tag library used in the
    page
  • the prefix is the name used for the library in
    the JSP page
  • the uri uniquely identifies the library

13
JSP Comments
  • lt-- comment goes here --gt
  • delimited by lt-- and --gt
  • ignored when the page is processed
  • never sent to the browser

14
JSP Actions
  • represent dynamic actions to be performed at
    runtime
  • ltprefix action-name attr1value1
    attr2value2gt
  • action_body
  • lt/brefixaction-namegt
  • or
  • ltprefix action-name attr1value1
    attr2value2gt
  • allows dor actions in different libraries to have
    the same name
  • allow contaienr to determine which library an
    action belongs

15
Standard Actions
  • ltispuseBeangt
  • makes a java bean component available to a page
  • ltjspgetPropertygt
  • gets a property value from a JavaBean components
    and adds it to the response
  • ltjspsetPropertygt
  • Set a JavaBean property value
  • ltjspincludegt
  • Include the response from a servlet or JSP page
    during the request processing phase
  • ltjspforwardgt
  • Forwards the processing of a request to a servlet
    or JSP page

16
Standard Actions (2)
  • ltispparamgt
  • adds a parameter to a request handed over to
    another servlet or JSP page
  • ltjspplugingt
  • Used to run applets on the browser
  • ltjspattributegt
  • Set the value of an action attribute
  • ltjspbodygt
  • Sets the action element body
  • ltjspelementgt
  • Dynamically generate an XML element
  • ltjsptextgt
  • to encapsulate text verbatim

17
JSP Standard Tag Library (JSTL)
  • group of libraries each containing related
    actions
  • Core
  • XML processing
  • Internationalization
  • Relational Database Access
  • Functions

18
JSP Expression Language
  • based on both ECMAScript and XPath
  • built-in support for JavaBean access and
    manipulation, collection of objects, automatic
    type conversion etc
  • EL expressions enclosed within and
  • anObject.aProperty
  • ltcif testuser.salary gt 10000gt
  • lt/Cifgt

19
Scripting Elements
  • fragments of java code embedded in a JSP page
  • Declarations
  • Expressions
  • Scriptlets
  • heavily used in early JSP pages but rarely in
    newer developments (and discouraged)

20
Declarations
  • used to insert methods, constants and variable
    declarations in JSP pages
  • lt!
  • private static String EXAMPLE/example2
  • private static String SHOP_PAGE/estore.jsp
  • private static String CART_PAGE/shopcart.jsp
  • private String dispPrice(String price)
  • // method body
  • gt

21
Expressions
  • an embedded Java expression that is evaluated and
    converted to a text String
  • The text string is placed in the JSP output at
    the location in which the element appears
  • lttablegt
  • lttrgt
  • lttdgt lt curItem.getName() gt lt/tdgt
  • lttdgt lt String.valueOf(dispPrice(curItem.getPrice
    ())) gt
  • lt/tdgt
  • lt/trgt
  • lt/tablegt

22
Scriptlets
  • used to include complete fragments of java code
    in a JSP page
  • cau use the out implicit object (of type
    javax.servlet.jsp.JspWriter) to write output
  • lt
  • if (state.isLocal())
  • out.print(totalCost localTax)
  • else
  • out.print(totalCost)
  • gt

23
Problems with Scriptlets
  • discouraged since they do not promote separation
    of presentation from data/logic
  • look as a Servlet inside-out
  • make JSP page as hard to write/maintain as
    corresponding Servlet

24
Model-View-Controller Design
  • Separation of
  • data and business logic (Model)
  • data presentation (View)
  • data interaction (Controller)
  • The user interacts with the Controller to ask for
    things to be done.
  • Controller forward the request to the Model
  • the result of the request is displayed by the View

25
Model-View-Controller Design
Controller (Servlet)
Data
Request
Model (JavaBean)
Response
View (JSP)
Data
Data Tier
JSP/Servlet Container
Browser
Write a Comment
User Comments (0)
About PowerShow.com