java servlet api cgi / http concepts java servlet api

15
Java Servlet API CGI / HTTP Concepts Java Servlet API

Upload: brook-brittney-cain

Post on 14-Dec-2015

244 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Java Servlet API CGI / HTTP Concepts Java Servlet API

Java Servlet API

CGI / HTTP Concepts

Java Servlet API

Page 2: 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

Page 3: Java Servlet API CGI / HTTP Concepts Java Servlet API

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

Page 4: Java Servlet API CGI / HTTP Concepts Java Servlet API

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

Page 5: Java Servlet API CGI / HTTP Concepts Java Servlet API

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

Page 6: Java Servlet API CGI / HTTP Concepts Java Servlet API

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)

Page 7: Java Servlet API CGI / HTTP Concepts Java Servlet API

HTTP Request/Response Cycle

HTTP Client HTTP 1.0/1.1

REQUEST

RESPONSE

GET /home.html HTTP/1.1

HTTP/1.1 200 OK

Page 8: Java Servlet API CGI / HTTP Concepts Java Servlet API

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

Page 9: Java Servlet API CGI / HTTP Concepts Java Servlet API

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)

Page 10: Java Servlet API CGI / HTTP Concepts Java Servlet API

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

Page 11: Java Servlet API CGI / HTTP Concepts Java Servlet API

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

Page 12: Java Servlet API CGI / HTTP Concepts Java Servlet API

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

Page 13: Java Servlet API CGI / HTTP Concepts Java Servlet API

Model View of handling requests

HttpServlet subclass

service()service()

doGet()doGet()

doPost()doPost()

Web Server

GET req

GET res

POST req

POST res

Page 14: Java Servlet API CGI / HTTP Concepts Java Servlet API

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>”);

}}

Page 15: Java Servlet API CGI / HTTP Concepts Java Servlet API

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