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

advertisement
CS4273: Distributed System Technologies and Programming I
Lecture 7: Java Networking
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
2
A simple ASP example
<%@ Language = "VBScript" %>
<html><head><title >
An ASP Example
</title></head>
<body bgcolor=white>
<form name="form1" method=“POST">
<input name="TextBox1" type="text" id="TextBox1" />
<%
ss = Request.Form("TextBox1")
%>
<input name="TextBox2" type="text" id="TextBox2" value="<%= ss %>" />
<input type="submit" name="Button1" value="Button" id="Button1" />
</form>
</body></html>
3
CGI Program working with HTML forms
<html><head> <title> Order Form </title> </head>
<body>
<h1 align=center> Order Form </h1>
<form action = "http://www.cs.cityu.edu.hk/~jia/cgi/
formcgi.cgi" method=POST>
Name <input name="customer" size=47> <p>
Street Address <input name="address" size=40> <p>
Credit Card No. <input name="cardno" size=10>
Expire Date <input name="expire" size=6>
M/C <input name="cc" type=radio value="mastercard">
Visa <input name="cc" type=radio value="visacard">
Ship by express <input name="express" type=checkbox>
<input type=submit value="submit address"> <p>
</body> </html>
4
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);
Web Server
browser
HTML
form
// send reply back to browser
printf(“Your order is processed. Thanks!\n);
HTTP
server
CGI
}
5
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
6
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).
7
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.
8
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();}
………
}
………
9
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);
}
10
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!
11
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!!!
12
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
……
}
…….
}
13
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
14
Download