Notes 18 - servlet

Notes 18 - servlet - Lecture Java Servlets Copyright Ellis...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Java Servlets Copyright © Ellis Horowitz 1998-2010 1 Lecture Java Servlets
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Java Servlets Copyright © Ellis Horowitz 1998-2010 2 What are Java Servlets • An alternate form of server-side computation that uses Java. • The Web server is extended to support an API, and then Java programs use the API to create dynamic web pages. • Using Java servlets provides a platform- independent replacement for CGI scripts. • Web servers that can host servlets are Java- enabled servers, e.g. Apache 1.3 and higher, with Tomcat Servlet/JSP engine • Servlets can be embedded in many different servers because the servlet API, which you use to write servlets, assumes nothing about the server's environment or protocol.
Background image of page 2
Java Servlets Copyright © Ellis Horowitz 1998-2010 3 Simple Example of Servlets http://cs-server.usc.edu:7651/index.jsp
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Java Servlets Copyright © Ellis Horowitz 1998-2010 4 Source Code for Simple Example import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html><head>"); out.println("<title>Hello World!</title>"); out.println("</head><body>"); out.println("<h1>Hello World!</h1>"); out.println("</body></html>");}}
Background image of page 4
Java Servlets Copyright © Ellis Horowitz 1998-2010 5 Explanation of Simple Servlet • The classes in the Servlet Package section are: – HttpServletRequest, HttpServletResponse, response.setContentType, response.getWriter() • HelloWorld extends the HttpServlet class, which implements the Servlet interface. • HelloWorld overrides the doGet method in the HttpServlet class. The doGet method is called when a client makes a GET request (the default HTTP request method), and results in the simple HTML page being returned to the client. • Within the doGet method, – The user's request is represented by an HttpServletRequest object. – The response to the user is represented by an HttpServletResponse object. – Because text data is returned to the client, the reply is sent using the PrintWriter object obtained from the HttpServletResponse object.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Java Servlets Copyright © Ellis Horowitz 1998-2010 6 Interacting with Clients • The methods to which the HttpServlet class delegates HTTP requests include: – doGet, for handling GET, conditional GET, and HEAD requests – doPost, for handling POST requests – doPut, for handling PUT requests – doDelete, for handling DELETE requests • HTTP servlets are typically capable of serving multiple clients concurrently. If the methods in your servlet do work for clients by accessing a shared resource, then you must either: – Synchronize access to that resource, or – Create a servlet that handles only one client request at a time
Background image of page 6
Java Servlets Copyright © Ellis Horowitz 1998-2010 7 The Servlet Life Cycle • Each servlet has the same life cycle:
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 46

Notes 18 - servlet - Lecture Java Servlets Copyright Ellis...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online