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

1 / 14
About This Presentation
Title:

CS4273: Distributed System Technologies and Programming I

Description:

put the cgi program in the same directory as the webpage (it can be in other dir, ... The cgi program reads the form data from the stdin and its stdout has been ... – PowerPoint PPT presentation

Number of Views:190
Avg rating:3.0/5.0
Slides: 15
Provided by: PengJ
Category:

less

Transcript and Presenter's Notes

Title: CS4273: Distributed System Technologies and Programming I


1
CS4273 Distributed System Technologies and
Programming I
  • Lecture 7 Java Networking

2
Web Networking in Java
  • Applets Send Data back to Web Servers
  • In many situations applets need to send data back
    to the server for processing. Generally there are
    two ways for applets to send data back to their
    home sites (note applets can only communicate to
    their home sites)
  • Launch the application server on the web site and
    applets communicate with it via sockets. But
    youre usually not allowed to launch your server
    on web site.
  • Use CGI (common gateway interface). It is well
    defined in HTTP protocol and most commonly used.

3
A simple ASP example
  • lt_at_ Language "VBScript" gt
  • lthtmlgtltheadgtlttitle gt
  • An ASP Example
  • lt/titlegtlt/headgt
  • ltbody bgcolorwhitegt
  • ltform name"form1" methodPOST"gt
  • ltinput name"TextBox1" type"text" id"TextBox1"
    /gt
  • lt
  • ss Request.Form("TextBox1")
  • gt
  • ltinput name"TextBox2" type"text" id"TextBox2"
    value"lt ss gt" /gt
  • ltinput type"submit" name"Button1"
    value"Button" id"Button1" /gt
  • lt/formgt
  • lt/bodygtlt/htmlgt

4
CGI Program working with HTML forms
  • lthtmlgtltheadgt lttitlegt Order Form lt/titlegt lt/headgt
  • ltbodygt
  • lth1 aligncentergt Order Form lt/h1gt
  • ltform action "http//www.cs.cityu.edu.hk/jia/fo
    rmcgi.cgi"
  • methodPOSTgt
  • Name ltinput name"customer" size47gt ltpgt
  • Street Address ltinput name"address" size40gt ltpgt
  • Credit Card No. ltinput name"cardno" size10gt
  • Expire Date ltinput name"expire" size6gt
  • M/C ltinput name"cc" typeradio
    value"mastercard"gt
  • Visa ltinput name"cc" typeradio
    value"visacard"gt
  • Ship by express ltinput name"express"
    typecheckboxgt
  • ltinput typesubmit value"submit address"gt ltpgt
  • lt/bodygt lt/htmlgt

5
A simple example of a CGI program
  • main(argc, argv)
  • int argc char argv
  • char data FILE fp
  • // header required by http protocol
  • printf("Content-type text/plain\n")
  • printf("\n")
  • scanf("s", data) // read from browser
  • // write data to a file
  • fp fopen("form.dat", "w")
  • fprintf(fp, "s\n", data)
  • // send reply back to browser
  • printf(Your order is processed. Thanks!\n)

6
Data format of HTML forms
  • Data received from the above example is sent back
    to the originating web server of the form as a
    single line string, where fields are separated by
    , space are replaced by and other symbols
    by ASCII code
  • customerXiaohuaJiaaddressCSdept2CCityUof
    HKcardno12345667expire012F97ccvisacardexp
    resson

7
CGI Program for HTML forms
  • cgi programs can be developed in any language.
    After being compiled, name the executable file
    with the extension .cgi.
  • put the cgi program in the same directory as the
    webpage (it can be in other dir, so long as you
    specify correctly in the form). This program will
    be started by the http server when a client
    clicks submit button of the form.
  • The cgi program reads the form data from the
    stdin and its stdout has been redirected to the
    client browser.
  • It processes the form data and sends replies back
    to the web browser (the replies is usually in
    HTML format, so that the browser can display the
    response nicely).

8
Applets Communicate with CGI Programs
  • applets can take inputs from the user, send
    request to and recv replies from a cgi program
    via the web server using HTTP protocol.
  • applets create a socket connecting back to the
    originating http server. Then they use POST
    method of http protocol to send data back
  • out.println("POST " "/jia /cgi/appletcgi.cgi
    HTTP/1.0")
  • out.println("Content-type plain/text")
  • out.println("Content-length " data.length())
  • out.println("") // header ends with \r
  • out.println(data) // data must be packed into
    one line
  • http server strips the header from the packet
    recved from applets and passes only the data to
    cgi program.
  • cgi program (in the same format as that for HTML
    forms) recv data from applets through stdin and
    sends replies back through stdout.
  • cgi program will be started by the http server
    each time when an applet POSTs data.

9
Example Of Applet Interacts With CGI Program
  • // one run of this protocol sends one line
    str
  • out.println("POST /jia/cgi/appletcgi.cgi
    HTTP/1.0")
  • out.println("Content-type plain/text")
  • out.println("Content-length "
    (sdata.length()1))
  • out.println("")
  • out.println(sdata i)
  • while ((line in.readLine()) ! null)
  • sourceArea.append(line"\n")
  • out.close() in.close()
  • public class appletCGI extends Applet implements
    ActionListener
  • public void init()
  • ..
  • add("North", send new JButton("send"))
  • send.addActionListener(this)
  • sourceArea new TextArea(30, 80)
  • public void public void actionPerformed
  • (ActionEvent e)
  • String sdata "HelloJava"
  • if (e.getSource() send)
  • s new Socket("personal.cs.cityu.edu.hk",80
    )
  • in new BufferedReader(new
  • InputStreamReader(s.getInputStream()))
  • out new PrintWriter(s.getOutputStream(),
    true)

10
A CGI program working with Applet
  • main(argc, argv)
  • int argc char argv
  • char sdata256, rdata256 FILE fp
  • printf("Content-type text/plain\n")
  • printf("\n")
  • fp fopen("form.dat", "a") // append mode
  • scanf("s\r", rdata)
  • fprintf(fp, "s\n", rdata)
  • printf(Echoed from CGI s\n", rdata)
  • fclose (fp)

11
What A CGI Program Can Do
  • After a CGI program is started by the http
    server, it receives data from applets (or HTML
    forms). It processes users inputs by
  • connect to database servers to retrieve client
    requested information (convert client query into
    database query commands), or
  • save the processed data to a local file, or
  • connect to mail server to send emails to clients.
  • Note CGI is an ordinary application server. It
    can communicate with any other servers running
    anywhere.
  • CGI is a gateway to bridge applets
  • to the outside of the world!

12
Implementation of Email Order
  • cgi program can request a mail server to send the
    data received from an applet to an email-account
    as a mail.
  • cgi program interacts with the mail server via
    SMTP (simple mail transfer protocol) to send a
    mail. Applet cant access mail svr directly due
    to security.
  • SMTP is an ASCII text protocol. You can find the
    format of SMTP commands by using telnet to
    connect to an email server.
  • Important telnet is a great debugging
  • tool for Internet programming!!!

13
Implementation of Email Order (Cont.)
  • class mailOrder
  • public static void main(String args )
  • boolean more true
  • String str null
  • s new Socket("mars.cs.cityu.edu.hk",25)
  • in new BufferedReader(
  • new InputStreamReader(s.getInputStream()))
  • out new PrintWriter(s.getOutputStream(),
    true)
  • str in.readLine() // read reply from
    svr
  • System.out.println(str) // for debug
  • // followings are SMTP commands
  • out.println("HELO ss4e.cs.ust.hk\r" )
  • str in.readLine() // read reply from svr
  • System.out.println(str)
  • out.println("MAIL FROM jcao\r" )
  • // read reply from mail svr
  • out.println("RCPT TO eechan_at_cityu.edu.hk\r" )
  • // read reply from mail svr
  • out.println( "DATA\r" ) // start of data
  • out.println( "this is 2nd test!\r" ) // email
    body
  • out.println( ".\r" ) // end of email body
  • out.println( "QUIT\r" ) // quit SMTP
  • .

14
CGI program in Java
  • Since the above cgi program (mailOrder) is
    written in Java and a Java program needs the Java
    interpreter to execute it, a cgi shell script is
    needed to execute the Java code
  • !/bin/sh
  • echo Content-type text/plain
  • echo
  • /usr/local/jdk/bin/java mailOrder
  • echo QUERY_STRING
Write a Comment
User Comments (0)
About PowerShow.com