" />"> " />">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS4273: Distributed System Technologies and Programming I Lecture 7: Java Networking.

Similar presentations


Presentation on theme: "CS4273: Distributed System Technologies and Programming I Lecture 7: Java Networking."— Presentation transcript:

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

2 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): 1.Launch the application server on web site. Applets communicate with it via sockets. But you’re usually not allowed to launch your server on web site. 2.Use CGI (common gateway interface). It is well defined in HTTP protocol and most commonly used. App server

3 3 A simple ASP example An ASP Example <% ss = Request.Form("TextBox1") %> " />

4 4 CGI Program working with HTML forms Order Form Order Form <form action = "http://www.cs.cityu.edu.hk/~jia/cgi/ formcgi.cgi" method=POST> Name Street Address Credit Card No. Expire Date M/C Visa Ship by express

5 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); } HTTP server CGI HTML form browser Web Server

6 6 Data format of HTML forms Data collected from the form is sent back to the web server as a single line of string, where fields are separated by “&”, space are replaced by “+” and other symbols by %ASCII code: customer=Xiaohua+Jia&address=CS+dept%2C+City+U+of+ HK&cardno=12345667&expire=01%2F97&cc=visacard&ex press=on

7 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, but you need 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 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 open a socket connecting back to the originating HTTP server. Then they use “POST” method to send data to the cgi program: 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 received from applets and passes only the data to cgi program. cgi program (in the same format as that for HTML forms) receives data from applets through stdin and sends replies back through stdout. cgi program is started by HTTP server each time when an applet POSTs data.

9 9 Example of Applet Interacts With CGI Program public class appletCGI extends Applet implements ActionListener { public void init() { …………….. add("North", textin = new JtextField("“, 50)); textin.addActionListener(this); sourceArea = new TextArea(30, 80); } public void public void actionPerformed (ActionEvent e) { String sdata = textin.getText(); // no space in data!!! if (e.getSource() == textin) { s = new Socket("personal.cs.cityu.edu.hk",80); in = new BufferedReader(new InputStreamReader(s.getInputStream())); out = new PrintWriter(s.getOutputStream(), true); // 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()); out.println(""); out.println(sdata ); while ((line = in.readLine()) != null) sourceArea.append(line+"\n"); out.close(); in.close();} ……… } ………

10 10 An “Echo” CGI program with Applet main(argc, argv) int argc; char *argv[]; { char sdata[256], rdata[256]; 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); // save to file printf(“Echoed from CGI: %s\n", rdata); fclose (fp); }

11 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 data (convert client query into database query commands), –save the processed data to a local file, or –connect to mail servers to send emails to users for data processing. Note: CGI is an ordinary application server. It can communicate with any servers running anywhere. CGI is a gateway to bridge applets to the outside of the world!

12 12 Implementation of Email Order CGI program can request a mail server to send the data received from an applet (or HTML form) to an email-account as an email. CGI program interacts with the mail server via SMTP (simple mail transfer protocol) to send an email. Applet can’t access mail server directly due to security reason. 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 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@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 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


Download ppt "CS4273: Distributed System Technologies and Programming I Lecture 7: Java Networking."

Similar presentations


Ads by Google