Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Servlet API CGI / HTTP Concepts Java Servlet API.

Similar presentations


Presentation on theme: "Java Servlet API CGI / HTTP Concepts Java Servlet API."— Presentation transcript:

1 Java Servlet API CGI / HTTP Concepts Java Servlet API

2 Java programs Standalone Applications Normal executable files under any O.S. Applets Client-side applications run from a web-client Servlets Server side applets

3 Getting Started… Download JSDK 2.2 from http://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 http://www.sun.com/software/jwebserver/index.html –W3C’s Jigsaw from http://www.w3.org/Jigsawhttp://www.w3.org/Jigsaw –JServ from Java-Apache at http://java.apache.orghttp://java.apache.org

4 Common Gateway Interface (CGI) CGI based web server Main Process response for CGI 2 CGI Request 2 CGI Request 3 response for CGI 1 CGI Request 1 response for CGI 3 The CGI Life Cycle

5 Java Servlet Life Cycle Java Servlet-based web server Main Process JVM Servlet 1 Servlet 2 Servlet Request 1 Servlet Request 2 Servlet Request 1

6 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)

7 HTTP Request/Response Cycle HTTP Client HTTP 1.0/1.1 REQUEST RESPONSE GET /home.html HTTP/1.1 HTTP/1.1 200 OK

8 Requests and Headers Client sends request through a method specifying the action to be performed First line syntax: –GET / –e.g. GET /home.html HTTP/1.1

9 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)

10 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

11 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

12 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

13 Model View of handling requests HttpServlet subclass service() doGet() doPost() Web Server GET req GET res POST req POST res

14 Simple “HelloWorld” Servlet import 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(“ ”); out.println(“ Hello, world. ”); out.println(“ ”); out.println(“ Hello, world. ”); out.println(“ ”); }

15 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


Download ppt "Java Servlet API CGI / HTTP Concepts Java Servlet API."

Similar presentations


Ads by Google