Presentation is loading. Please wait.

Presentation is loading. Please wait.

Servlets. Our Project 3-tier application Develop our own multi-threaded server Socket level communication.

Similar presentations


Presentation on theme: "Servlets. Our Project 3-tier application Develop our own multi-threaded server Socket level communication."— Presentation transcript:

1 Servlets

2 Our Project 3-tier application Develop our own multi-threaded server Socket level communication

3 Web Application Shift Perspective use web server Develop servlet to extend web server to allow dynamic contents

4 Web Server listens on a standard port and handles http protocol. A browser requests for documents (mainly html files) + upload of small amounts of data Two most important http protocol elements: (hidden from you by browser) – GET (request document, may upload data) – POST (request document, upload data). 4

5 Early Web Servers Early web server were static, acted more like file servers: – Browser requests page – Server hands over page – Browser interprets html and displays to user – Might contain gif or jpeg images or simple animations 5

6 Modern Web Servers Need to support dynamic page E-Commerce became popular Need web pages to act more like programs that could interact with user. 6

7 Running an Applet 7 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

8 Running A Servlet Copyright © 2010 Pearson Addison-Wesley. All rights reserved. 8

9 Servlets Java’s answer to CGI, – very simple – Relatively high-level Requirements: a servlet-enabled web server When specified by your web page, web page passes http requests to java method (assuming everything is setup properly) 9

10 What do Servlets do? Read data sent by the user (e.g., form data) Look up other information about request in the HTTP request (e.g., headers, cookies, etc.) Generate the results (may do this by talking to a database, file system, etc.) Format the results in a document (e.g., make it into HTML Set the appropriate HTTP response parameters (e.g., cookies, content-type, etc.) Send the document to the user 10

11 Supporting Servlets The Web server must support servlets (since it must run the servlets): – Apache Tomcat – Sun’s JavaServer Web Development Kit (JSWDK) – Other web servers 11

12 Writing Servlets, cont. All servlets extend the Servlet class. All http servlets (by far most typical) should extend the HttpServlet class. In extending HttpServlet, you typically override the following methods: – init, service or doGet/doPost, destroy (very common) – doPut, doDelete, doOptions, doTrace (rare) Note: there is NO main() for Servlets! 12

13 Main HttpServlet Methods init() – called once when servlet is loaded by server. Contains any initializations that are common to all requests. doGet(HttpServletRequest, HttpServletResponse) – Called each time the servlet receives an http GET request posted by a client. Passes two objects, one representing the information of the request, the other used to configure a response. We’ll study these methods soon. 13

14 Main HttpServlet Methods, cont. doPost(HttpServletRequest, HttpServletResponse) – Same as doGet but for an http POST request. destroy() – Called before servlet is unloaded from memory. Performs any final cleanup, freeing memory, closing connections, etc. 14

15 Service Method Important: The method service(HttpServletRequest, HttpServletResponse) is also called for each servlet invocation. Service() in turn calls doGet and doPost, etc. for an HttpServlet. It is best not to override service even if you want to handle doGet and doPost identically. Simply have one call the other. 15

16 HttpServletRequest Object Passed when browser calls doGet and doPost. Most import methods for beginning servlet programming (in HttpServletRequest class): – String getParameter(String paramName) – String[] getParameterNames() – String[] getParameterValues() Makes getting data from web pages very simple. Many other methods for images, cookies, etc. 16

17 HttpServletResponse Object Passed when browser calls doGet or doPost Most import methods for beginning servlet programming: – PrintWriter getWriter(); Get Writer for communicating back to client – setContentType(String); Typically use “text/html”, indicating that html will be sent back to the browser 17

18 Examples import javax.servlet.http.*; import javax.servlet.*; import java.io.*; public class SimpleServlet extends HttpServlet { /*Handle the HTTP GET method by building a simple web page.*/ public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriterout; Stringtitle = "Simple Servlet Output”; response.setContentType("text/html"); // set content type out = response.getWriter(); // then write the data of the response out.println(" "); out.println(title); out.println(" "); out.println(" " + title + " "); out.println(" This is output from SimpleServlet."); out.println(" "); out.close(); }

19 The Page Test Simple Servelet Example Test Simple Servelet Example


Download ppt "Servlets. Our Project 3-tier application Develop our own multi-threaded server Socket level communication."

Similar presentations


Ads by Google