Title: Internet
1Internet Communications
- Server-side programming for
- the WWW (lecture 9)
- CGI Programming Calling Java programs
2CGI Programs
- A few lectures back we introduced CGI.
- Can run programs in any language as a CGI
program. - Weve used shell scripts - but rather limited.
- Most common to use PERL - A simple scripting
language. - But also possible to call Java programs.
3CGI and Java
- We can write a Java program to write out HTML (or
just text) - Compile this, make executable by all then..
public class test public static void
main(String argv) System.out.println(
Content-type text/plain\n)
System.out.println(Hello World!)
4CGI and Java
- Now write a tiny shell script just to run your
Java program - Lets say its called testjava and executable by
others.. - Now use URLwww. macs./cgi-bin/cgiwrap/yourlogin
/testjava
!/bin/sh java test
5Using Java to Handle Form Data
- Remember that data from an HTML form is available
in the QUERY_STRING variable, e.g., - namealisonaddressEdinburgh
- We can pass that data to Java prog as follows
- Our Java can then write out this data
!/bin/sh java -Dquery.stringQUERY_STRING test
String data System.getProperty(query.string)
System.out.println(Data data)
6Can also write out data to a file
FileWriter file2 new FileWriter(data) PrintWr
iter outputFile new PrintWriter(file2) outputFi
le.println(Data data)
7Example
public class test public static void
main(String argv) throws IOException
FileWriter file1 new FileWriter("mydata")
PrintWriter outputFile new PrintWriter(file1)
System.out.println("Content-type
text/plain\n" ) String s System.getProperty(
"query") System.out.println("Your data "
s) outputFile.println("Your data " s)
outputFile.close()
Download script
Try It
Download java
8Java and Form Data
- Want to break query string into useful parts..
- Use a class CGI to do this (provided)
- Can then get hold of values from our form
- Would get address, as vector of lines of text
import java.util. public class test
public static void main(String argv)
System.out.println(Content-type
text/plain\n) CGI cgi new CGI()
Vector v cgi.getValues(address)
9Writing out data..
- We can now write out the users address.
- Note that the data is in a vector - but usually
youll just want the first element.
- import java.util.
- public class test
- public static void main(String argv)
- System.out.println(Content-type
text/plain\n) - CGI cgi new CGI()
- Vector v cgi.getValues(address)
- System.out.println(Your address is)
- System.out.println(v.elementAt(0))
-
Try It
Download
Download Script
10Incorporating CGI class
- We provide CGI.java
- To compile your program so that the CGI class is
available use - javac CGI.java Yourfile.java
- Yourfile.class will now contain what you want.
11Writing it all out..
- Can write out all the form data
CGI cgi new CGI() Enumeration e
cgi.getNames() while(e.hasMoreElements())
String name (String) e.nextElement() Vector
v cgi.getValues(name) System.out.println(nam
e v.elementAt(0))
Example
Download
12Writing out HTML response page
- We can of course write out HTML
System.out.println(Content-type
text/html\n) CGI cgi new CGI()
Vector v cgi.getValues(address)
System.out.println(lthtmlgtltheadgt lt/headgt
ltbodygt) System.out.println(lth1gt
Addresslt/h1gt) System.out.println(v.elementA
t(0)) System.out.println(lt/bodygt
lt/htmlgt)
13Testing CGI
- You dont need to load a web page to test a CGI
program - just try running your script. - It may not have all the form data there, but
something like following should be output - Content-type text/plainYour address is
14Activities
- Try and get the Java examples in this handout
working. - Link it to your form - try to create a response
page which says, say Thanks Alison. Your
order will be delivered to 13 Nosuchstreet
Edinburgh any day now.
15Summary CGI
- CGI programs can be written in any language.
- Perversely, we choose Java.
- Call java program from tiny shell script.
- Java can contain code to get at different parts
of the HTML form data. - This can be saved to file, or incorporated into
response page.
16Comparison CGI vs servlets
- Coding Java CGI vs Java Servlets is probably
about the same complexity. - Java CGI needs launching via script.
- In both we can use special methods to access
input data from forms. - In both we need to take care in compilation.
- CGI is a little more robust - doesnt depend on
Tomcat server being up. - Servlets are a little more efficient.
- JSP is easier for small examples, but awkward
when amount of code is significant.