Basic HTML version of Foils prepared June 5 99

Foil 66 Network Connections with URL's

From Java Language in the Computer Science Curriculum ADMI Tutorial Duluth Minnesota -- June 3 99. by Geoffrey C. Fox


A Java application can create and use URL's to anywhere; applets are restricted to URL's to their own web server.
  • String urlStr = "http://www.npac.syr.edu/"; try { URL url = new URL( urlStr ); } catch ( MalformedURLException e ) { System.err.println( "Bad URL: " + urlStr );}
A method URL.openConnection() returns an instance of class URLConnection:
  • URLConnection conn = url.openConnection();
  • conn.connect(); // open a connection
  • Now use this URLConnection object to open a stream:
  • new InputStreamReader( conn.getInputStream() ) );
Applications can also use conn.getOutputStream(), but not applets.
Note that one can connect not just to HTML files but also to CGI scripts and other web documents.



© Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Sat Jun 5 1999