The class java.net.URL
is designed to make it easier
to handle fetching url data. You don't have to open sockets yourself
If you want to get extra information about the url, the class
URLConnection
can be used
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
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.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
}
}
request.getParameter(String name)
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);