CS4273: Distributed System Technologies and Programming I - PowerPoint PPT Presentation

About This Presentation
Title:

CS4273: Distributed System Technologies and Programming I

Description:

Title: Wireless Sensor Networks Author: Peng-Jun Wan Last modified by: Prof. JIA Xiaohua Created Date: 12/8/2000 10:08:46 PM Document presentation format – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 17
Provided by: Peng158
Category:

less

Transcript and Presenter's Notes

Title: CS4273: Distributed System Technologies and Programming I


1
CS4273 Distributed System Technologies and
Programming I
  • Lecture 11 JavaServer Pages (JSP)

2
JavaServer Pages (JSP)
  • Why JSP?
  • Get rid of Java programming.
  • Separate presentation from business logic.
  • Reuse predefined components, such as JavaBean,
    tag libraries.
  • How does JSP work?
  • The entire JSP page gets translated into a
    Servlet (once) at the first time it is invoked.
  • It is the Servlet code that gets executed at each
    request. No interpretation of JSP occurs at
    request time. The original JSP page is totally
    ignored at request time.
  • JSP tutorial website
  • http//www.jsptut.com/
  • http//java.sun.com/products/jsp/docs.html

3
Example of a Translated JSP
  • The original JSP
  • ltH1gtA Random Numberlt/H1gt
  • lt Math.random() gt
  • The resulting Servlet code
  • public void _jspService(HttpServletRequest
    request,
  • HttpServletResponse
    response)
  • throws ServletException, IOException
  • response.setContentType("text/html")
  • HttpSession session request.getSession(true
    )
  • JspWriter out response.getWriter()
  • out.println("ltH1gtA Random Numberlt/H1gt")
  • out.println(Math.random())
  • ...

4
JSP Notations
  • JSP can include Java code, applets, servlets,
    JavaBeans, etc.
  • JSP Expressions lt expression gt
  • JSP Scriptlets (for Java code) lt Java-code gt
  • JSP Declarations lt! declarations gt
  • JSP Directives lt_at_ directive gt
  • e.g., lt_at_ page import java.util.gt
  • lt_at_ include file banner.htmlgt

5
The First JSP example
  • JSP files in dir jia/www/java/js/jsp/
  • URL for run JSP http//jserv.cs.cityu.edu.hk8080
    /jia/jsp/jspDate.jsp
  • lt-- file jia/www/java/js/jsp/jspDate.jsp--gt
  • lthtmlgt
  • ltheadgtlttitlegt Simple JSP Test Pagelt/titlegtlt/headgt
  • ltbodygt
  • lth1 aligncentergtSimple JSP Test Pagelt/h1gt
  • lttable aligncentergt
  • lttrgt lttdgt Now, the time is lt/tdgt
  • lttdgt lt new java.util.Date() gt lt/tdgt
  • lt/trgt
  • lt/tablegt
  • lt/bodygtlt/htmlgt

6
An Example of JSP Scriptlets (Java code)
  • ltHTMLgt
  • ltBODYgt
  • The time returned from a java utility function
  • lt
  • // This scriptlet declares and initializes
    "date"
  • java.util.Date date new java.util.Date()
  • out.println( date )
  • out.println( "ltBRgtYour machine's address is
    " )
  • // request in next line is in servlet style
  • out.println( request.getRemoteHost())
  • gt
  • lt/BODYgt
  • lt/HTMLgt

7
An Example of JSP Declaration and Directives
  • lt-- the next line is a JSP directive --gt
  • lt_at_ page import"java.util." gt
  • ltHTMLgt
  • ltBODYgt
  • lt!-- following is a JSP declaration of function
    getDate --gt
  • lt!
  • Date getDate()
  • Date theDate new Date()
  • return theDate
  • gt
  • lt!-- the next line has JSP expression --gt
  • The time returned from java declared function
    is lt getDate() gt
  • lt/BODYgt
  • lt/HTMLgt

8
Another Example of Scriptlet
  • lt-- filename welcome.jsp --gt
  • lthtmlgtltheadgt
  • lttitlegtExample of JSP Scriptletlt/titlegt
  • lt/headgt
  • ltbodygt
  • lt // begin scriptlet
  • String name
  • request.getParameter( "firstName" )
  • if ( name ! null )
  • gt
  • lth1gtHello lt name gt, ltbr /gt
  • Welcome to JavaServer Pages!lt/h1gt
  • lt // continue scriptlet
  • // end if
  • else
  • gt
  • ltform action "welcome.jsp" method "GET"gt
  • ltpgtType your first name and press Submitlt/pgt
  • ltpgtltinput type "text" name "firstName" /gt
  • ltinput type "submit" value
    "Submit"/gt
  • lt/pgt
  • lt/formgt
  • lt // continue scriptlet
  • // end else
  • gt lt-- end scriptlet --gt
  • lt/bodygt
  • lt/htmlgt

9
JSP Directives
  • JSP directives are used for specifying page
    settings at translation time. Three directives
    page, include, and taglib.
  • page directive define the page setting.
  • e.g., lt_at_ page import java.util.gt
  • include directive insert a file at
    translation-time as if it were originally part of
    the JSP (note. jspinclude inserts a file at
    request time!).
  • e.g., lt_at_ include file banner.htmlgt
  • taglib directive specify user-defined tag
    libraries.
  • e.g., lt_at_ taglib prefixblx uri/blx.tld gt

10
JSP Standard Actions
  • JSP actions allow some common tasks in JSP. JSP
    containers process
  • actions at request time
  • ltjspincludegt. Include a file at request time
    (Note. The include directive inserts a file at
    translation time)
  • e.g., ltjspinclude page banner.htmlgt
  • ltjspforwardgt. Forward the processing to another
    JSP or servlet. This terminates the current JSPs
    execution.
  • e.g., ltjspforward page errorProcess.jspgt
  • ltjspplugingt. Allow a browser-object (applets) or
    html element embedded in jsp. Its passed to
    browser without being executed in server!
  • e.g., ltjspplugin type"applet"
    codemyApplet.class
  • width"475" height"350"gtlt/jspplugingt

11
JSP Standard Actions (Cont.)
  • ltjspparamgt. Pass parameters to another JSP or
    servlet (used together with include, forward, and
    plugin).
  • ltjspuseBeangt. Specify scope of a JavaBean and
    assign id.
  • e.g. ltjspuseBean id"numguess"
    class"num.NumberGuessBean" scope"session"/gt
  • The next two actions are used with JavaBean
    instance
  • ltjspsetPropertygt
  • ltjspgetPropertygt

12
Example of jspforward and jspparam
  • lt
  • // end if
  • else
  • gt
  • ltform action "forwardExample.jsp"
  • method "GET"gt
  • ltpgtType your first name and press Submitlt/pgt
  • ltpgtltinput type "text" name "firstName" /gt
  • ltinput type "submit" value "Submit"/gtlt/pgt
  • lt/formgt
  • lt
  • // end else
  • gt
  • lt/bodygt
  • lt/htmlgt
  • lt-- filename forwardExample.jsp --gt
  • lthtmlgtltheadgt
  • lttitlegtForward request to another JSPlt/titlegt
  • lt/headgt
  • ltbodygt
  • lt // begin scriptlet
  • String name request.getParameter(
    "firstName" )
  • if ( name ! null )
  • gt
  • ltjspforward page "forwardJSP.jsp"gt
  • ltjspparam name "date" value
  • lt new java.util.Date() gt/gt
  • lt/jspforwardgt

13
Example of JSP forward and jspparam (Cont.)
  • lt-- filename forwardJSP.jsp --gt
  • lthtmlgt
  • ltheadgt
  • lttitlegtProcessing a jspforward requestlt/titlegt
  • lt/headgt
  • ltbodygt
  • ltpgt Hello lt request.getParameter( "firstName"
    ) gt, ltbr /gt
  • Your request was received ltbr /gt
  • and forwarded at lt request.getParameter(
    "date" ) gt
  • lt/bodygt
  • lt/htmlgt

14
Example of JSP useBean
  • The program numguess.jsp uses a JavaBean object
    NumberGuessBean.class, which is in
    /js/WEB-INF/classes/num. See NumberGuessBean.java
    for lib routines.

lt_at_ page import "num.NumberGuessBean"gt ltjspuseB
ean id"numguess" class"num.NumberGuessBean"
scope"session"/gt ltjspsetProperty
name"numguess" property""/gt lthtmlgt ltheadgtlttitle
gtNumber Guesslt/titlegtlt/headgt ltbody
bgcolor"white"gt lt if (numguess.getSuccess())
gt Congratulations! You got it. And after
just lt numguess.getNumGuesses() gt tries.ltpgt
lt numguess.reset() gt
Would you lta href"numguess.jsp"gt try
againlt/agt? lt else if (numguess.getNumGuesses()
0) gt Welcome to the Number Guess
game.ltpgt Ive a number between 1 and
100.ltpgt ltform method GETgt What's your guess?
ltinput typetext name guessgt ltinput
typesubmit value"Submit"gt lt/formgt .lt/bodygt
lt/htmlgt
15
Example of JSP Tag Library (taglib)
  • Directive taglib allows user defined tags,
    facilitating the use of large amount of Java
    libraries in jsp (separate JSP from Java-code
    implementations). Three files involved for
    taglib
  • JSP file, specifying the use of taglib, e.g.,
    SendMail.jsp
  • lt_at_ taglib prefixsend" uri./taglib.tld"
    gt // send is prefix you can use
  • ltHTMLgtltBODYgt
  • lt String email request.getParameter( "email"
    ) gt
  • ltsendemail host"yoursmtphost.com"
    from"you_at_email.com"gt
  • ltblxemailTogtlt email gtlt/blxemailTogt
  • lt/sendemailgt
  • Tag Library Descriptor file (.tld) in XML format,
    taglib.tld, specify format of lib routines, such
    as routine names, parameters, etc.
  • lttaggtltnamegtSendmaillt/namegt
  • lttagclassgtcom.cj.smtp.Sendmaillt/tagclassgt
  • ltteiclassgtcom.cj.smtp.BoolVariablelt/teiclassgt
  • Library routines (java class file), sendmail.jar
    in WEB-INF/lib (for you only) or
    jserv/usr/jt/lib (for all users). See dir
    setting in http//www.jsptube.com/servlet-tutorial
    s/web-application-directory-structure.html
  • Use jar tvf blazix.jar cmd to see the list of
    routines in class file.

16
JSP JDBC Example
  • lt_at_ page contentType"text/htmlcharsetutf-8"gt
  • lt_at_ page import"java.sql." gt
  • ltHTMLgtltHEADgt
  • ltTITLEgtJSP JDBClt/TITLEgtlt/HEADgt
  • ltBODYgt
  • ltcentergtJSP JDBClt/centergt
  • ltBRgtltBRgt
  • lttable border3 aligncenter gt
  • lt
  • Class.forName("com.mysql.jdbc.Driver")
  • String url jdbcmysql//jserv.cs.cityu.edu.hk3
    306/db_jdemo
  • Connection con DriverManager.getConnection(url,
    "jdemo","apple1")
  • Statement stmtcon.createStatement()
  • String sql"select COF_NAME, PRICE from COFFEES"
  • ResultSet rsstmt.executeQuery(sql)
  • while(rs.next())
  • out.print("ltTRgtltTDgt"
  • rs.getString("COF_NAME")"lt/TDgt")
  • out.print("ltTDgt"
  • rs.getString("PRICE")"lt/TDgtlt/TRgt"
    )
  • gt
  • lt/tablegt
  • ltBRgtltHRgt
  • lt rs.close() stmt.close() con.close() gt
  • lt/BODYgt
  • lt/HTMLgt
Write a Comment
User Comments (0)
About PowerShow.com