Introduction to Network Programming

  • java.net.URL
  • java.net.URLConnection
    • Fine grained control over URL
    • Abstract superclass that represents a communications link between an application and a URL.
  • Protocols
    • http - Hypertext Transfer Protocol
    • ftp - File Transfer Protocol
    • File - file protocol on local machine
    • mailto - Simple Mail Transport Protocol (smtp)

History of Web Applications

To summarize...

Servlets CGI
Resident in server
Initialized Once
Multithreaded
Data persistent between invocations
Cant crash server
Downloaded as needed

HTTP Basics

  • Stateless Protocol
  • HTTP GET
    • Designated for reading information from the server. Can pass additional information describing what to get
    • Limited URL and query length string
  • HTTP POST
    • Designated for sending information to the server
    • Unlimited length as it is a socket connection to the server

HTTP Request Headers

  • User-Agent

    The client making the request
  • Host

    The server were the request is being made
  • Accept

    Specifies the types of files (mime-types) the client will accept in a response
  • Connection

    The type of connection

HTTP Response Headers

  • Date

    Response date in Greenwich Mean Time (GMT)
  • Server

    http server offering the response, may be different then that of the server in the http request
  • Content-Type

    Describes the mime-type of the content
  • Content-Length

    Optionally gives the number of bytes in the response
See GetResponses.java

Java Servlets

  • What is a Servlet
    • Runnable Java Programs that execute within the HTTP process on the web server.
    • Loaded when the server is started or the first time they are invoked from the client.
    • Executes some logic on the server and create dynamic content to be sent back to the client based on the outcome of the logic execution.
  • Current Specification Level

    Java Servlet 2.4 and JSP 1.2 specifications.
  • http://java.sun.com/products/servlet/

There are numerous JavaTM Servlet containers. Following is a short list pulled from java.sun.com:

Product Type of Product Support for Java Servlets Support for JSP Pages
Apache Tomcat Server and Add-on Engine
3.3.1 2.2 1.1
4.0.3 2.3 1.2
Apache Web Server Jserv Server and Add-on Engine 2.0 No
ATG Dynamo Application Server Server 2.2 1.1
Bajie Server Server 2.3 1.2
BEA WebLogic Server 7.0 Server 2.3 1.2
Borland Enterprise Server, AppServer Edition Server 2.3 1.2
Caucho Technology Resin Add-on Engine 2.3 1.2
Computer Associates Advantage Joe Server 2.3 1.2
EasyThings Web Server Server 2.2 1.1
Fujitsu INTERSTAGE Server 2.3 1.2
Gefion Software LiteWebServer Server 2.3 1.2
Hewlett Packard Total-e-Server Server 2.2 1.1
IBM WebSphere Technology for Developers Server 2.3 1.2
IBM WebSphere Application Server 4.0 Server 2.2 1.1
IONA Orbix E2A Application Server Platform, J2EE Edition Server 2.3 1.2
Jetty Server
3.1 2.2 1.1
4.0 2.3 1.2
Macromedia JRun 4 Server and Add-on Engine 2.3 1.2

Building a JavaTM Servlet is not unlike building any other kind of JavaTM object. Just implement the appropriate model as follows:


/* $Id: servlets.xml,v 1.3 2003/09/12 04:10:11 mconneen Exp $
 *
 */

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 * The simplest possible servlet.
 *
 * @author James Duncan Davidson
 */
public class HelloWorldExample 
      extends HttpServlet 
{

  public void doGet(HttpServletRequest request,
        HttpServletResponse response)
        throws IOException, ServletException
  {
      ResourceBundle rb =
       ResourceBundle.getBundle("LocalStrings"
       ,request.getLocale());
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();

      out.println("<html>");
      out.println("<head>");

	   String title = rb.getString("helloworld.title");

	   out.println("<title>" + title + "</title>");
      out.println("</head>");
      out.println("<body bgcolor=\"white\">");

	// note that all links are created to be relative. this
	// ensures that we can move the web application that this
	// servlet belongs to a different place in the url
	// tree and not have any harmful side effects.

   // making these absolute till we work out the
   // addition of a PathInfo issue

	   out.println("<a href=\"/examples/servlets/helloworld.html\">");
      out.println("<img src=\"/examples/images/code.gif\" height=24 " +
       "width=24 align=right border=0 alt=\"view code\"></a>");
      out.println("<a href=\"/examples/servlets/index.html\">");
      out.println("<img src=\"/examples/images/return.gif\" height=24 " +
       "width=24 align=right border=0 alt=\"return\"></a>");
      out.println("<h1>" + title + "</h1>");
      out.println("</body>");
      out.println("</html>");
  }
}
   

Servlet Life Cycle

Submitting a request to a URL of..

http://mconneen:8080/examples/servlet/CalcServlet?x=5&y=3



Yields the following interactions:

  1. Servlet created and initialized
  2. Handle zero or more service calls from clients
  3. Destroy the servlet and then gc it.

Posting Information to a Servlet

  • HTTP Post is for sending information TO a server.
  • Most often accomplished via HTML FORM method=post
  • Virtually unlimited url length
  • http://localhost:8080/examples/servlet/RequestParamExample

Java Server Pages

http://mconneen:8080/examples/jsp/calc.jsp?x=5&y=4

Deploying Servlets

Remember, Servlets are JavaTM objects. As such, the Servlet container (such as Tomcat) needs to be able to find and load the servlet and associated classes.