java servlet api cgi / http concepts java servlet api
TRANSCRIPT
Java Servlet API
CGI / HTTP Concepts
Java Servlet API
Java programs
• Standalone ApplicationsNormal executable files under any O.S.
• AppletsClient-side applications run from a web-client
• ServletsServer side applets
Getting Started…
• Download JSDK 2.2 fromhttp://java.sun.com/products/servlet/download.html
• Download a web-server such as: – Java Web Server from
http://www.sun.com/software/jwebserver/index.html
– W3C’s Jigsaw from http://www.w3.org/Jigsaw
– JServ from Java-Apache at http://java.apache.org
Common Gateway Interface (CGI)
CGI based web server
Main Processresponse for CGI 2CGI Request 2
CGI Request 3
response for CGI 1CGI Request 1
response for CGI 3
The CGI Life Cycle
Java Servlet Life Cycle
Java Servlet-based web server
Main Process
JVM
Servlet 1
Servlet 2Servlet 2
Servlet Request 1
Servlet Request 2
Servlet Request 1
HTTP
• HyperText Transfer Protocol– simple stateless protocol
• RFC2616 states that HTTP is a “application-level protocol for distributed, collaborative, hypermedia information systems”
• Request/response protocol (RFC2616)
HTTP Request/Response Cycle
HTTP Client HTTP 1.0/1.1
REQUEST
RESPONSE
GET /home.html HTTP/1.1
HTTP/1.1 200 OK
Requests and Headers
• Client sends request through a method specifying the action to be performed
• First line syntax:– GET /<html page> <protocol version>– e.g. GET /home.html HTTP/1.1
The GET method
• literally for getting information
• information passed as sequence of characters appended to the request URL– e.g. http://www.google.com/search?q=jdbc
• pages can be book marked
• only for sending small amounts of information (typically < 240 characters)
The POST method
• literally for posting information (e.g. credit card #, SSN, etc.) that should be sent only once
• data length unlimited• data passed directly over socket connection
as a part of HTTP request body• URL remains the same; transfer invisible• can’t be book marked
Other HTTP methods
• HEAD– sent when it needs to see the response headers
• PUT (not properly supported)– place documents directly over a server
• DELETE (not properly supported)– remove documents from the server
• TRACE– returns exact contents of request to the client
The Servlet API
• javax.servlet package– support for generic and protocol-independent servlets
• javax.servlet.Servlet interface– every servlet must implement this
• javax.servlet.GenericServlet class– used for protocol independent servlets
• javax.servlet.http.HttpServlet class– used for HTTP-based servlets
– extended from GenericServlet with HTTP support
Model View of handling requests
HttpServlet subclass
service()service()
doGet()doGet()
doPost()doPost()
Web Server
GET req
GET res
POST req
POST res
Simple “HelloWorld” Servletimport java.io.*;import javax.servlet.*;import javax.servlet.http.*;public class HelloWorld HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
res.setContentType(“text/html”);PrintWriter out = res.getWriter();out.println(“<HTML>”);out.println(“<HEAD><TITLE>Hello, world.</TITLE></HEAD>”);out.println(“<BODY>”);out.println(“<B> Hello, world. </B>”);out.println(“</BODY> </HTML>”);
}}
Compiling & Running
• Include javax.servlet and javax.servlet.http packages in the CLASSPATH
• Use javac to compile HelloWorld.java• Copy HelloWorld.class to servlets directory• Start httpd• Enter the URL
http://localhost:8080/servlet/HelloWorld