CS4273: Distributed System Technologies and Programming ILecture 7: Java Networking
CS4273: Distributed System Technologies and Programming I Lecture 7: Java Networking
Web Networking in JavaApplets SendDatabacktoWeb ServersIn many situations applets need to send dataBrowser SiteWeb Server Siteback to the server for processing. Generallythere are two ways for appletsto send databrowsingwebnttpservelback to their home sites (note: applets canpage or appletonlycommunicatetotheirhomesites):cgi scripts1. Launch the application server on web siteAppletscommunicatewithitvia socketshomepagesBut you're usually not allowed to launchdata filesyour server on web site.DISK2.Use CGI (common gateway interface). It isADDWell defined in HTTP protocol and mostservercommonly used
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
A simple ASP example<%@ Language ="VBScript" %><html><head><title>An ASP Example</title></head><bodybgcolor=white><formname="form1"method="POST"><inputname="TextBox1"type="text"id="TextBox1"/><%ss = Request.Form("TextBox1")%><inputname="TextBox2"type="text"id="TextBox2"value="<%=ss%>"/><input type="submit" name="Buttonl" value="Button" id="Button1"/></form></body></html>
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>
CGI Program working with HTML forms<html><head><title>OrderForm</title></head><body><h1 align=center> Order Form </h1> Netscape - [Order Form]COXFleEdtViewGoBookmarksOptionsWindowHelpDitectory<form action = "http://www.cs.cityu.edu.hk/~jia/cgi/ACHerReloaformcgi.cgi" method=POST>http:.//www.cs.cilyuedu.hk/~ja/formtest htmlLocalionTNName<inputname="customer"size=47><p>What's Cool!HardbookNer SearchNet DiectoryWhat'sNev!Street Address <input name="address" size=40><p>AOrder FormCredit Card No.<input name="cardno" size=-10>ExpireDate<inputname="expire"size=6>NameXiaohua JiaM/C<inputname-"cc"type=radiovalue="mastercard">Street Addresscs dept, CityU ofHKVisa <input name="cc" type=radio value="visacard">ExpireDate 01/97M/CCVisaCredit CardNo.12345667Ship by express <input name="express" type=checkbox>Ship by express P<input type=submit value="submit address"><p>submitaddress</body></html>s0]Document Done
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>
A simple example of a CGI programmain(argc, argv)int argc; char *argvl;char *data; FILE *fp:I/ header: required by http protocolprintf("Content-type: text/plainln"):printf("In");scanf("%s",data);//read frombrowserWebServerbrowser// write data to a fileHTTPfp=fopen("form.dat","w+");HTMLserverformfprintf(fp, "%sln", data);//sendreplybacktobrowserCGLprintf("Your order is processed. Thanks!In);
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