CST252 Network Software Design - PowerPoint PPT Presentation

1 / 40
About This Presentation
Title:

CST252 Network Software Design

Description:

CST252. Network Software Design. Lecture 10: Web Applications. Admin. Results on noticeboard. 1st Coursework (30%) 1st Phase Test (20%) Second Phase Test ... – PowerPoint PPT presentation

Number of Views:146
Avg rating:3.0/5.0
Slides: 41
Provided by: Davi825
Category:

less

Transcript and Presenter's Notes

Title: CST252 Network Software Design


1
CST252 Network Software Design
  • Lecture 10 Web Applications

2
Admin
  • Results on noticeboard
  • 1st Coursework (30)
  • 1st Phase Test (20)
  • Second Phase Test
  • Monday 20th May 1300 - 1400
  • 4th Floor of CS building
  • Second coursework due 13th May

3
Resources
  • Java Tutorial (java.sun.com)
  • Specialised Trail Servlets
  • Course Website
  • users.wmin.ac.uk/lancasd/CCTM91
  • Book
  • Cay Horstmann and Gary Cornell. Core Java
  • Volume 2 chapter 3
  • Books specifically on JSP/Servlets

4
Last Lecture Review
  • Applets
  • browser capabilities
  • Simple graphics
  • rectangles and ellipses
  • colours
  • Security
  • Jar files
  • More Graphics and Events

5
Lecture 10 Overview
  • Web Applications
  • Sending Data to a Web Server
  • GET/POST methods
  • Web html Form
  • Server-Side Processing
  • Servlets
  • JSP

6
Web Applications
  • For many network applications it is better to use
    HTTP rather than to invent an application
    protocol and work at socket level
  • eg the On-line quiz of your coursework
  • But not always - eg peer to peer
  • A web application is a dynamic extension of a web
    server

7
Dynamic Web Server
  • In the beginning - web pages were static, the web
    server just returned fixed html files
  • Dynamical pages
  • response page depends on data sent
  • A Web Form
  • the way of gathering and sending data to server
  • Servlets
  • programs on the server computer that use the data
    returned to create a new web page

8
Sending Data to a Web Server
  • A Web Form
  • History - a hack on top of CERN system, NCSAs
    Mosaic 2.0 CGI scripts
  • How to attach the data
  • GET and POST methods for sending data
  • in URL
  • in body

9
html form
  • ltHTMLgt
  • ltHEADgt
  • ltTITLEgtThree Parameter Testlt/TITLEgt
  • lt/HEADgt
  • ltBODYgt
  • ltFORM METHODGET ACTION"/cgi-bin/docalc"gt
  • First Parameter ltINPUT TYPE"TEXT"
    NAME"param1"gtltBRgt
  • Second Parameter ltINPUT TYPE"TEXT"
    NAME"param2"gtltBRgt
  • Third Parameter ltINPUT TYPE"TEXT"
    NAME"param3"gtltBRgt
  • ltCENTERgt ltINPUT TYPE"SUBMIT"gt lt/CENTERgt
  • lt/FORMgt
  • lt/BODYgt
  • lt/HTMLgt

10
Form as web page
11
GET method
  • Attach the data as parameters to the URL
  • http//host/script?params
  • Parameters must be encoded (avoid spaces)
  • Simple, but limited in length
  • METHODGET is default

12
POST Method
  • Parameters are sent in the body of an http
    request after the header and blank line
  • Still need to encode them
  • More manageable if there are many parameters
  • METHODPOST is not default - must specify

13
POST from a Java program
  • Open a URLConnection and write name/value pairs
    to the output stream
  • URL url new URL(http/host/script)
  • URLConnection ucon url.openConnection()
  • ucon.setDoOutput(true)
  • PrintWriter out new PrintWriter(ucon.getOutputSt
    ream())
  • out.print(name1URLEncoder.encode(value1))
  • out.print(name2URLEncoder.encode(value2)\n
    )
  • out.close()

14
Custom Clients
  • Could write a java client that acts as a custom
    client for a web applications
  • Sends data using POST
  • Receives HTTP
  • Server is implemented through a dynamic web
    server
  • eg Census data

15
Server Side Processing
  • Old style CGI
  • often in Perl
  • Servlets
  • servlets are to web servers as applets are to
    browsers
  • JSP
  • simplified technique for pages that are mostly
    static with small dynamic parts

16
Servlets vs CGI
  • Efficient and Scalable
  • new threads not processes (JVM permanently
    running)
  • Convenient
  • sessions - store data between requests
  • Secure
  • CGI is known to have loopholes
  • Portable
  • Java with standard Servlet API

17
DateServlet.java
  • import java.io.
  • import javax.servlet.
  • import javax.servlet.http.
  • public class HelloWWW extends HttpServlet
  • public void doGet(HttpServletRequest request,
  • HttpServletResponse response)
  • throws ServeletException, IOException
  • response.setContentType(text/html)
  • PrintWriter out response.getWriter()
  • String now new Date().toString()
  • out.println( ltHTMLgt\n
  • ltHEADgtltTITLEgtDate Servletlt/TITLEgtlt/HEADgt\n
  • ltBODYgt\n
  • ltH1gtltpgtThe current time is now
  • lt/pgtlt/H1gt\n
  • lt/BODYgtlt/HTMLgt)

18
DateServlet notes
  • Just creates a web page with the date
  • Includes, and extends
  • Only need to code a single method doGet()
  • HttpServletRequest and HttpServletResponse
    parameters
  • setContentType
  • Create html as String, and pass to out
  • Here the returned information does not depend on
    any user input so ignore request
  • overrides abstract method in httpServlet

19
Installing Servlets
  • Run by the web server
  • Different Web Servers require different setup
    (now mainly Tomcat)
  • Tomcat (Apache)
  • Need a deployment descriptor web.xml
  • Can package to a .war file
  • Not covered here

20
JWSDP
  • Java WebServer Development Package
  • Convenient package of utilities for web services
    development
  • tomcat
  • ant
  • XML support
  • SOAP support

21
Servlet Life Cycle
  • init() called when servlet is first created,
    but not on further requests
  • one off initialisation, eg connection pools
  • service() checks what kind of request it is and
    passes to doPost(), doGet()..
  • dont touch, put code into doXXX()
  • destroy() before removing the servlet from
    memory

22
getParameter()
  • A method to get the parameters sent from an Html
    form
  • ltFORM ACTION/servlet/ParamTestgt
  • First Parameter ltINPUT TYPETEXT
    NAMEparam1gtltBRgt
  • Second Parameter ltINPUT TYPETEXT
    NAMEparam2gtltBRgt
  • Third Parameter ltINPUT TYPETEXT
    NAMEparam3gtltBRgt
  • ltCENTERgt ltINPUT TYPESUBMITgt lt/CENTERgt
  • lt/FORMgt
  • Access each parameter using the name field
    appearing in the form
  • String p1 request.getParameter(param1)

23
Proxy Servlet
  • To allow an applet to obtain information from
    other sources
  • Recall applet security
  • Applets can only call home
  • A solution, is to provide a forwarding service or
    proxy running on the web server machine
  • Implement as a servlet ProxySvr.java
  • Compare C and Perl versions

24
ProxySvr.java
  • Applet makes a get request
  • http//www.yourserver.com/proxySvr?URLhttp//www
    .wmin.ac.uk/
  • index.html
  • public class ProxySvr extends HttpServlet
  • public void doGet(HttpServletRequest request,
  • HttpServletResponse response)
  • throws ServletException, IOException
  • PrintWriter out response.getWriter()
  • String query request.getParameter(URL)
  • query URLDecoder(query)
  • URL url new URL(query)
  • BufferedReader in new BufferedReader(
  • new InputStreamReader(url.openStream()))
  • String line
  • while( (linein.readLine()) ! null )
  • out.println(line)
  • out.flush()
  • // All exception handling ignored

25
JavaServer Pages (JSP)
  • Servlets are a powerful way of dynamically
    creating web pages
  • But tedious, if the most of the page is static,
    with just one line changed (eg customer name)
  • JSP allows you to use ordinary web authoring
    tools to create a static page, and embed a few
    dynamic lines
  • Other competing technologies ASP,PHP..

26
date.jsp
  • lthtmlgt
  • ltheadgt
  • lttitlegtData JSPlt/titlegt
  • lt/headgt
  • ltbodygt
  • lth1gtDate JSPlt/h1gt
  • ltpgtThe current time is
  • lt new java.util.Date() gt
  • lt/pgt
  • lt/bodygt
  • lt/htmlgt

27
Using JSP
  • The places where dynamic content is to be added
    are marked with special HTML-like tags lt...gt
  • Easier to deploy than servlets
  • Server automatically changes .jsp files into
    regular servlets
  • translation and compilation stages
  • eg Tomcat - inspect the intermediate java code

28
JSP Elements
  • Expressions lt expression gt evaluated and
    placed in the servlet output
  • Scriptlets lt code gt inserted into a method
    called by the servlet service() method
  • Declarations lt! code gt inserted into the body
    of the servlet

29
Separate Computation (.java) and Presentation
(.html)
  • date.jsp contained a bit of java code, this is
    rarely a good idea, input from
  • programmer
  • graphic designer
  • Better to perform computations in a separate java
    class
  • Easiest if these java classes are JavaBeans

30
JavaBean Components
  • JavaBeans are ordinary java classes that follow a
    particular naming convention for methods
    accessing properties
  • getpropertyname()
  • setpropertyname(Type newValue)
  • JSP has shortcuts for using this kind of java
    object and accessing methods
  • ltjspuseBean ...../gt

31
time.jsp
  • ltjspuseBean idformatter classTimeFormatterBe
    an/gt
  • ltjspsetProperty nameformatter propertydate
  • valuelt new java.util.Date() gt/gt
  • lthtmlgt
  • ltheadgt
  • lttitlegtData JSPlt/titlegt
  • lt/headgt
  • ltbodygt
  • lth1gtDate JSPlt/h1gt
  • ltpgtThe current time is
  • ltjsp.getProperty nameformatter
  • propertytime /gt
  • lt/pgt
  • lt/bodygt
  • lt/htmlgt

32
TimeFormatterBean.java
  • import java.text.DateFormat
  • import java.util.Date
  • public class TimeFormatterBean
  • public TimeFormatterBean()
  • timeFormatter DateFormat.getTimeInstance()
  • public void setDate(Date aDate)
  • theDate aDate
  • public String getTime()
  • String timeStr timeFormatter.format(theDate)
  • return timeStr
  • private DateFormat timeFormatter
  • private Date theDate

33
Notes
  • time.jsp
  • first two lines construct the bean and initialise
    the Date property
  • jsp.getProperty directive calls the get method
    for the time property
  • TimeFormatterBean.java
  • two parameters
  • date (set method to initialise)
  • time (get method)
  • Uses DateFormat class to get just the time as a
    String, not the whole Date

34
Handling Request Parameters in JSP
  • Find out what the user typed into the input
    fields of an html form using
  • lt request.getParameter(paramname) gt
  • Doesnt matter if it was POST or GET
  • Convenient shorthand for passing request input to
    JavaBean set methods

35
Sessions
  • HTTP is a stateless protocol
  • browser sends request to web server
  • web server sends reply then disconnects
  • In many cases (eg shopping cart), would like the
    system to remember what was added on previous
    pages
  • JSP scopesession uses the same JavaBean for
    all pages rather than a new one for each page

36
Information from the Web
  • Harvesting information
  • Parsing html
  • At the mercy of changes of page format
  • SOAP
  • XML
  • A modern way of storing content
  • platform independent
  • text based

37
Web Services
  • Request for information via a web application,
    typically BtoB
  • eg Stock price
  • Access using SOAP which transfers the information
    as XML
  • Allows communication even if using different
    information systems and data formats

38
XML
  • Extensible Markup Language
  • To transmit data from one program to another in a
    structured form
  • Encoding independent of any particular
    programming language
  • Text comprehensible to humans

39
XML
  • Strict formatting rules
  • ltcoingt
  • ltvaluegt0.5lt/valuegt
  • ltnamegtHalf Dollarlt/namegt
  • lt/coingt
  • Java API to manipulate XML JAXP (1.4)
  • Parsing and analysing
  • SAX line by line calling (user defined) methods
    as each construct is encountered
  • DOM create whole tree in memory, then analyse

40
Simple Object Access Protocol
  • SOAP
  • messages with immediate reply/ delayed reply
  • Java API
  • JAXM
  • JAX-RPC
  • Business registry access

41
Summary
  • HTML forms GET and POST
  • Java programs that do this
  • Dynamic Server
  • Servlets
  • JSP
Write a Comment
User Comments (0)
About PowerShow.com