The class java.net.URL
is designed to make it easier
to handle fetching url data. You don't have to open sockets yourself
/**
* SimpleURL.java
*/
import java.net.*;
import java.io.*;
public class SimpleURL{
public SimpleURL (){
}
public static void main(String[] args){
if (args.length != 1) {
System.err.println("Usage: java SimpleURL url");
System.exit(1);
}
URL url = null;
try {
url = new URL(args[0]);
} catch(MalformedURLException e) {
e.printStackTrace();
System.exit(1);
}
Object content = null;
try {
content = url.getContent();
} catch(IOException e) {
e.printStackTrace();
System.exit(2);
}
// The content is actually a subclass of InputStream
BufferedReader reader = null;
reader = new BufferedReader(new InputStreamReader((InputStream) content));
String line;
try {
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
System.exit(3);
}
}
} // SimpleURL
If you want to get extra information about the url, the class
URLConnection
can be used
/**
* URLInfo.java
*/
import java.net.*;
import java.io.*;
public class URLInfo{
public URLInfo (){
}
public static void main(String[] args){
URL url = null;
try {
url = new URL("http://localhost/ecommerce/cgiapps.gif");
} catch(MalformedURLException e) {
e.printStackTrace();
System.exit(1);
}
URLConnection connection = null;
InputStream str = null;
try {
connection = url.openConnection();
connection.connect();
} catch(IOException e) {
e.printStackTrace();
System.exit(1);
}
System.out.println("Type " + connection.getContentType());
System.out.println("Length " + connection.getContentLength());
}
} // URLInfo
HTTP connections write information from client to HTTP server and read
the reply. This can be used to fetch static URLs (as above).
It can also be used to post form data and read the reply.
The URLConnection
can be used for this
/**
* CGI.java
*/
import java.net.*;
import java.io.*;
public class CGI{
public CGI (){
}
public static void main(String[] args){
URL url = null;
try {
url = new URL("http://localhost/cgi-bin/test-cgi");
} catch(MalformedURLException e) {
e.printStackTrace();
System.exit(1);
}
URLConnection connection = null;
InputStream istr = null;
OutputStream ostr = null;
try {
connection = url.openConnection();
// doOutput must be set before connecting
connection.setDoOutput(true);
connection.connect();
ostr = connection.getOutputStream();
} catch(IOException e) {
e.printStackTrace();
System.exit(1);
}
DataOutputStream writer = new DataOutputStream(ostr);
try {
writer.writeBytes("name=jan\n");
writer.flush();
writer.close();
} catch(IOException e) {
e.printStackTrace();
System.exit(1);
}
// After writing is over, we can read the reply
try {
istr = connection.getInputStream();
} catch(IOException e) {
e.printStackTrace();
System.exit(1);
}
BufferedReader reader = new BufferedReader(new InputStreamReader(istr));
String line;
try {
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
System.exit(3);
}
}
} // CGI
telnet
:
telnet proxy.monash.edu.au 8080
GET http://jan.newmarch.name/index.html HTTP/1.0
java -DproxySet=true \
-DproxyHost=proxy.monash.edu.au \
-DproxyPort=8080 \
SimpleURL http://jan.newmarch.name/index.html
String password = "newmarch:xxxxxxx";
String encPasswd = new sun.misc.BASE64Encoder().encode(password.getBytes
());
connect.setRequestProperty("Proxy-Authorization", "Basic " + encPasswd);
Some classes that are useful on the client side for displaying documents
JLabel
can be used to display images such as GIF images
JEditorPane
can be used to display HTML
documents using HTML 3.2
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JEditorPane;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.HTMLDocument;
import java.io.StringReader;
/**
* Browser.java
*
*
* Created: Mon Aug 21 22:30:28 2006
*
* @author <a href="mailto:jan.newmarch@infotech.monash.edu.au">Jan Newmarch</a>
* @version 1.0
*/
public class Browser extends JFrame{
public static void main(String[] args) throws java.io.IOException {
Browser f = new Browser();
f.show();
}
public Browser() throws java.io.IOException {
super("Browser");
setSize(300, 300);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
public void windowOpened(WindowEvent e) {}
});
JEditorPane jep = new JEditorPane();
jep.setEditable(false);
HTMLEditorKit htmlKit = (HTMLEditorKit) (jep.getEditorKitForContentType("text/html"));
HTMLDocument doc = (HTMLDocument) htmlKit.createDefaultDocument();
jep.setEditorKit(htmlKit);
StringReader sr = new StringReader("<h1>HTML document</h1> <p> text </p>");
jep.read(sr, doc);
getContentPane().add(new JScrollPane(jep));
}
} // Browser
import java.net.*;
import java.io.*;
public class EchoServer {
final static protected int PORT = 8001;
public static void main(String[] argv) {
try {
new EchoServer();
} catch(IOException e) {
e.printStackTrace();
}
}
public EchoServer() throws IOException {
ServerSocket server = new ServerSocket(PORT);
Socket socket = server.accept();
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
DataOutputStream out = new DataOutputStream(os);
BufferedReader in = new BufferedReader(new InputStreamReader(is));
out.writeBytes("HTTP/1.1 404 OK\n");
out.writeBytes("Content-Type: text/plain\n\n");
String line;
line = in.readline();
// check that line is GET url HTTP/1.0
// \extract url
// find the file for that url
// open the file
// write it to the out stream
while ((line = in.readLine()) != null &&
(line.length() != 0)) {
out.writeBytes(line + "\n");
}
out.close();
socket.close();
}
}
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SomeServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// Use "request" to read incoming HTTP headers (e.g. cookies)
// and HTML form data (e.g. data the user entered and submitted)
// Use "response" to specify the HTTP response line and headers
// (e.g. specifying the content type, setting cookies).
PrintWriter out = response.getWriter();
// Use "out" to send content to browser
}
}
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("Hello World");
}
}
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWWW extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String docType =
"<!DOCTYPE HTML PUBLIC \"-//W3C/DTD HTML 4.0 " +
"Transitional//EN\"\n";
out.println(docType +
"<HTML>\n" +
"<head><title>Hello WWW</title></head>\n" +
"<body>\n" +
"<h1> Hello WWW</h1>" +
"</body></html>");
}
}
request.getParameter(String name)
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class ThreeParams extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Reading Three Request Parameters";
out.println(ServletUtilities.headWithTitle(title) +
"<BODY>\n" +
"<H1 ALIGN=CENTER>" + title + "</H1>\n" +
"<UL>\n" +
" <LI>param1: "
+ request.getParameter("param1") + "\n" +
" <LI>param2: "
+ request.getParameter("param2") + "\n" +
" <LI>param3: "
+ request.getParameter("param3") + "\n" +
"</UL>\n" +
"</BODY></HTML>");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
Cookie cookie = new Cookie("sessionID", "1234");
response.addCookie(cookie);
Cookie[] cookies = request.getCookies();
HttpSession session = request.getSession(true);
This creates a session object if one did not exist before,
or returns one if it did exist before
session.putValue(key, value);
session.getValue(key);