JavaServer Pages

I was first introduce to JavaServer Pages from David’s Malan’s Computer Science E-259 class. I’m not for re-inventing the wheel so I just copied and paste some of the content that can be found with slides from lecture 7 to help explain JSP pages and servlets. If you want to learn more, watch his lecture at cs259.tv

The JavaServer Pages™ Technology
“JavaServer Pages™ is the Java™ 2 Platform, Enterprise Edition
(J2EE) technology for building applications for generating dynamic
web content, such as HTML, DHTML, XHTML and XML.
JSP technology enables the easy authoring of web pages that create
dynamic content with maximum power and flexibility.”

What’s a JSP Page?
“A JSP page is a textual document that describes how to create a
response object from a request object for a given protocol. The
processing of the JSP page may involve creating and/or using other
objects…. HTTP is the default protocol for requests and responses.
Additional request/response protocols may be supported by JSP
containers…. The default request and response objects are of type
HttpServletRequest and HttpServletResponse respectively.”

What’s a Servlet?
“A servlet is a Java™ technology-based Web component, managed
by a container, that generates dynamic content. Like other Javabased components, servlets are platform-independent Java classes
that are compiled to platform-neutral byte code that can be loaded
dynamically into and run by a Java technology-enabled
Web server.”

What’s a Servlet Container?
“The servlet container is a part of a Web server or application
server that provides the network services over which requests and
responses are sent, decodes MIME-based requests, and formats
MIME-based responses. A servlet container also contains and
manages servlets through their lifecycle.”

Typical Sequence of Events
1. A client (e.g., a Web browser) accesses a Web server and makes an
HTTP request.

2. The request is received by the Web server and handed off to the servlet
container. The servlet container can be running in the same process as
the host web server, in a different process on the same host, or on a
different host from the web server for which it processes requests.

3. The servlet container determines which servlet to invoke based on the
configuration of its servlets, and calls it with objects representing the
request and response.

4. The servlet uses the request object to find out who the remote user is,
what HTTP POST parameters may have been sent as part of this
request, and other relevant data. The servlet performs whatever logic it
was programmed with, and generates data to send back to the client. It
sends this data back to the client via the response object.

5. Once the servlet has finished processing the request, the servlet
container ensures that the response is properly flushed, and returns
control back to the host Web server

David Malan’s DiagServer.java source code

import java.io.*;
import java.net.*;
import sun.net.NetworkServer;

/**
 * A program that echoes to standard output an HTTP
 * request, whether GET or POST.
 *
 * Usage: java DiagServer [port]
 *
 * If you spawn the server on a port already in use, you will be informed
 * "Server failed to start: java.net.BindException: Permission denied".
 * Note that the server's default choice of ports is 80.
 *
 * Excerpted from
 * http://hotwired.lycos.com/webmonkey/99/36/index3a_page4.html?tw=backend.
 */

public class DiagServer extends NetworkServer {

  public static void main(String[] args) {
    int port = 80;
    try {
      port = Integer.parseInt(args[0]);
    }
    catch (Exception e) {
      //You might want to catch a NumberFormatException here,
      //or do some more sophisticated command line parsing.
    }
    try {
      DiagServer ds = new DiagServer();
      ds.startServer(port);
      System.err.println("DiagServer started on port " + port);
      System.err.println("");
    }
    catch (IOException ioe) {
      System.err.println("Server failed to start: " + ioe);
    }
  } //main

  public void serviceRequest() throws IOException {
    int byteCount = 0;
    while (clientIsOpen() && (byteCount <1000)) {
      byte b = (byte) clientInput.read();
      byteCount++;
      System.out.write(b);
      System.out.flush();
    }
  } //serviceRequest
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s