CS320 Web and Internet Programming JSP Scripting Elements and Page Directives PowerPoint PPT Presentation

presentation player overlay
1 / 19
About This Presentation
Transcript and Presenter's Notes

Title: CS320 Web and Internet Programming JSP Scripting Elements and Page Directives


1
CS320 Web and Internet ProgrammingJSP Scripting
Elements and Page Directives
  • Chengyu Sun
  • California State University, Los Angeles

2
Java Server Page (JSP)
  • Why?
  • Its tedious to generate HTML using println()
  • Separate presentation from processing
  • How?
  • Java code embedded in HTML documents

3
HelloJSP1.jsp
lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
Transitional//EN"gt ltHTMLgt ltHEADgtltTITLEgtHello JSP
1lt/TITLEgtlt/HEADgt ltBODYgtA JSP without J or
S.lt/BODYgt lt/HTMLgt
4
HelloJSP2.jsp
lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
Transitional//EN"gt ltHTMLgt ltHEADgtltTITLEgtJSP Hello
Worldlt/TITLEgtlt/HEADgt ltBODYgtHello World on lt new
java.util.Date() gt. lt/BODYgt lt/HTMLgt
5
How Does JSP Work?
convert
compile
execute
JSP
Servlet
ByteCode
automatically done by server
  • Look under CATALINA_HOME/work/Catalina/localhost/
    context_name

6
Some Simple Observations about the JSP/Servlets
  • In package org.apache.jsp
  • _jspService() handles everything
  • replaces service() in HttpServlet
  • What happened to doGet(), doPost(), ...??
  • HTML text ? out.write(...)

7
Pre-defined Variables
  • request, response, out
  • session, application
  • config, pageContext
  • page

The same request, response, session etc. that are
used in servlets see the Servlet API
documentation for what you can do with them.
8
JSP Components
  • HTML template text
  • Code elements of Java
  • Scripting elements
  • Directives
  • Beans
  • Expression language
  • Custom tag libraries

9
Comments
  • lt-- Hidden Comments --gt
  • lt!-- Output (HTML) Comments --gt

10
JSP Expressions
  • lt Java expression gt
  • Whats an expression??

lt expression gt in JSP
out.write( expression ) in _jspService()
11
Example Add.jsp
  • Add two integer parameters and display the sum

12
JSP Scriptlets
  • lt Java code gt
  • All code goes inside _jspService()

13
Example Add.jsp Again
  • Add the code to validate the parameters

14
Another Scriptlet Example
lt if( Math.random() lt 0.5) gt ltH1gtHave a
ltIgtnicelt/Igt day!lt/H1gt lt else gt ltH1gtHave a
ltIgtlousylt/Igt day!lt/H1gt lt gt
15
JSP Declarations
  • lt! class variables or methods gt
  • All code goes outside _jspService()

16
Example RequestCounter.jsp
  • Initialize a counter as a class variable
  • Increment the counter each time the JSP is
    requested

17
JSP Scripting Elements
  • JSP Expressions
  • JSP Scriptlets
  • JSP Declarations

18
Directives
  • Affect the overall structure of the JSP
  • lt_at_ type attrvalue ... gt
  • Three type of directives
  • page
  • include
  • taglib

19
Some Page Directives
lt_at_ page importjava.util., java.util.zip.
gt lt_at_ page contentTypetext/html gt lt_at_ page
pageEncodingShift_JIS gt lt_at_ page
sessionfalse gt lt-- default is true --gt
  • isELIgnored, errorPage, isErrorPage
Write a Comment
User Comments (0)
About PowerShow.com