What's New in Java Networking

What's New in Java Networking

Elliotte Rusty Harold

Internet World

Friday, October 27, 2000

elharo@metalab.unc.edu

http://www.ibiblio.org/xml/


New I/O Model


Java Secure Sockets Extension


HTTPS Client

import java.net.*;
import java.io.*;
import java.security.*;
import javax.net.ssl.*;

public class HTTPSClient {
    
  public static void main(String[] args) {
    
    if (args.length == 0) {
      System.out.println("Usage: java HTTPSClient host");
      return;
    }       
    
    int port = 443; // default https port
    String host = args[0];
    
    try {     
      
     Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
      SSLSocketFactory factory 
       = (SSLSocketFactory) SSLSocketFactory.getDefault();
       
      SSLSocket socket = (SSLSocket) factory.createSocket(host, port);

      Writer out = new OutputStreamWriter(socket.getOutputStream());
      // https requires the full URL in the GET line
      out.write("GET http://" + host + "/ HTTP/1.1\r\n");
      out.write("\r\n");
      out.flush(); 

      // read response
      BufferedReader in = new BufferedReader(
       new InputStreamReader(socket.getInputStream()));
      int c;
      while ((c = in.read()) != -1) {
        System.out.write(c);
      }
      
      out.close();                  
      in.close();
      socket.close();
      
    } 
    catch (IOException e) {
      System.err.println(e);
    }
    
  }
  
}

URLDecoder Class

String input 
 = "http://www.altavista.com/cgi-bin/query?"
 + "pg=q&kl=XX&stype=stext&q=%2B%22Java+I%2FO%22&search.x=38&search.y=3";
 try {
  String output = URLDecoder.decode(input);
  System.out.println(output);
 }

HTML Parsing and Display


Using a JEditorPane to display a web page

import javax.swing.text.*;
import javax.swing.*;
import java.io.*;
import java.awt.*;

public class OReillyHomePage {

  public static void main(String[] args) {
        
     JEditorPane jep = new JEditorPane();
     jep.setEditable(false);   
     
     try {
       jep.setPage("http://www.oreilly.com");
     }
     catch (IOException e) {
       jep.setContentType("text/html");
       jep.setText("<html>Could not load http://www.oreilly.com </html>");
     } 
      
     JScrollPane scrollPane = new JScrollPane(jep);     
     JFrame f = new JFrame("O'Reilly & Associates");
     // Next line requires Java 1.3
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     f.getContentPane().add(scrollPane);
     f.setSize(512, 342);
     f.show();
    
  }

}

JEditorPane displaying a Web Page


What JEditorPane Doesn't Do


Socket Options

Java 1.1 provides methods to set these options:

Java 1.2 adds two more options:

Java 1.3 adds one more:


The Java Mail API

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

public class Assimilator {

  public static void main(String[] args) {

    try {
      Properties props = new Properties();
      props.put("mail.host", "mail.cloud9.net");
       
      Session mailConnection = Session.getInstance(props, null);
      Message msg = new MimeMessage(mailConnection);

      Address bill = new InternetAddress("god@microsoft.com", 
       "Bill Gates");
      Address elliotte = new InternetAddress("elharo@metalab.unc.edu");
    
      msg.setContent("Resistance is futile. You will be assimilated!", 
       "text/plain");
      msg.setFrom(bill);
      msg.setRecipient(Message.RecipientType.TO, elliotte);
      msg.setSubject("You must comply.");
      
      Transport.send(msg);
      
    }
    catch (Exception e) {
      e.printStackTrace(); 
    }
    
  }

}

Still to Come


May Never Get


To Learn More



Questions?


Index | Cafe con Leche

Copyright 2000 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified October 26, 2000