Servlets Compiled by Dr. Billy B. L. Lim. Servlets Servlets are Java programs which are invoked to service client requests on a Web server. Servlets extend.

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10 Servlets and Java Server Pages.
Advertisements

1 Servlets Based on Notes by Dave Hollinger & Ethan Cerami Also, the Online Java Tutorial by Sun.
 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
J.Sant Servlets Joseph Sant Sheridan Institute of Technology.
Objectives Ch. D - 1 At the end of this chapter students will: Know the general architecture and purpose of servlets Understand how to create a basic servlet.
Servlets and a little bit of Web Services Russell Beale.
An introduction to Java Servlet Programming
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 34 Servlets.
2/16/2004 Dynamic Content February 16, /16/2004 Assignments Due – Message of the Day Part 1 Due – Reading and Warmup Work on Message of the Day.
1 CS6320 – Servlet Structure and Lifecycle L. Grewe.
Servlets Replace Common Gateway Interface Scripts Extend Server Functionality Modules (software components) Like applets to browsers No GUI.
COMP201 Java Programming Part III: Advanced Features Topic 15: Servlets Materials Based on The Java Tutorial
Comp2513 Java Servlet Basics Daniel L. Silver, Ph.D.
Java Server and Servlet CS616 Team 9 Kim Doyle, Susan Kroha, Arunima Palchowdhury, Wei Xu.
COMP201 Java Programming Part III: Advanced Features Topic 14: Servlets Servlets and JavaServer Pages (JSP) 1.0: A Tutorial
Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET.
Definition Servlet: Servlet is a java class which extends the functionality of web server by dynamically generating web pages. Web server: It is a server.
Servlets. Our Project 3-tier application Develop our own multi-threaded server Socket level communication.
Michael Brockway Application Integration Servlets l Introduction & Overview l HTTP Servlets l HTTP get Requests l HTTP post Requests l Multi-tier Applications.
SE-2840 Dr. Mark L. Hornick1 Java Servlet-based web apps Servlet Architecture.
Java Servlets. What Are Servlets? Basically, a java program that runs on the server Basically, a java program that runs on the server Creates dynamic.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
Java Server Pages B.Ramamurthy. Topics for Discussion 8/20/20152 Inheritance and Polymorphism Develop an example for inheritance and polymorphism JSP.
1 Servlet How can a HTML page, displayed using a browser, cause a program on a server to be executed?
Servlets, Sessions, and Cookies Lecture 8 cs193i – Internet Technologies Summer 2004 Kelly Shaw, Stanford University.
Java Servlet Technology. Introduction Servlets are Java programs that run on a Web server, handle HTTP requests and build Web pages Servlet specification.
Java Servelets. What Is a Servlet? A servlet is a Java programming language class used to extend the capabilities of servers that host applications accessed.
Servlets Pranav Maydeo. What is a Servlet ? Servlets are modules of Java code that run in a server application to answer client requests. Servlets are.
Servlets. - Java technology for Common Gateway Interface (CGI) programming. - It is a Java class that dynamically extends the function of a web server.
Objectives Java Servlet Web Components
Java support for WWW Babak Esfandiari (sources: Qusay Mahmoud, Roger Impey, textbook)
CSC 2720 Building Web Applications
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems Web based Applications,
COMP201 Java Programming Part III: Advanced Features Topic 14: Servlets Servlets and JavaServer Pages (JSP) 1.0: A Tutorial
Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some.
Chapter 3 Servlet Basics. 1.Recall the Servlet Role 2.Basic Servlet Structure 3.A simple servlet that generates plain text 4.A servlet that generates.
Java Servlets & Java Server Pages Lecture July 2013.
Saving Client State Session Tracking: Maintain state about series of requests from same client over time Using Cookies: Clients hold small amount of their.
Java Servlet API CGI / HTTP Concepts Java Servlet API.
@2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING.
Introduction to Server-Side Web Development Introduction to Server-Side Web Development Session II: Introduction to Server-Side Web Development with Servlets.
1 Java Servlets l Servlets : programs that run within the context of a server, analogous to applets that run within the context of a browser. l Used to.
Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection.
Servlets.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, Responds oriented other.
1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.
JS (Java Servlets). Internet evolution [1] The internet Internet started of as a static content dispersal and delivery mechanism, where files residing.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, written in Java code, that.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 21 Java Servlets Wed. 11/22/00 based on material.
HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,
SERVLET THETOPPERSWAY.COM
How CGI and Java Servlets are Run By David Stein 14 November 2006.
S ERVLETS Form Data 19-Mar-16. F ORM P ROCESSING You must have come across many situations when you need to pass some information from your browser to.
Java Servlets References: Karen Anewalt, Mary Washington College.
Introduction to Servlets
Servlets.
Servlet Fudamentals.
Java Servlets By: Tejashri Udavant..
Java Servlets 9/18/2018.
Distributed Computing, M. L. Liu
Java Servlets 9/21/2018.
Servlet.
Chapter 26 Servlets.
Distributed Computing, M. L. Liu
Introduction to Servlets
Servlet APIs Every servlet must implement javax.servlet.Servlet interface Most servlets implement the interface by extending one of these classes javax.servlet.GenericServlet.
J2EE Lecture 1:Servlet and JSP
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
Presentation transcript:

Servlets Compiled by Dr. Billy B. L. Lim

Servlets Servlets are Java programs which are invoked to service client requests on a Web server. Servlets extend the functionality of the Web server. Servlets can: –generate HTML dynamically to be returned to the client in response to some request –act as a communication gateway to other resources on the server, e.g. databases, other applications, etc.

Servlets (2) –coordinate connections from a number of clients –open a separate connection from the server to an applet on the browser using a "state-full" protocol, e.g. IIOP, RMI, TCP/IP –provide additional (customized) processing for the server's standard routines –filter data to be delivered to client applet

Servlets (3) Servlets are a superior solution over alternative technologies, e.g. CGI and server specific APIs: –Better performance (same address space); no, per request, process startup time –Written in Java -- better performance than scripting languages, e.g. PERL or Unix shell scripts –More portable (write-once run-on-any-server) –Stronger security model

Servlet Lifecycle The lifecycle of a servlet begins when it is loaded into a Web server and ends when it is terminated or is reloaded. When a servlet is loaded, the server creates an instance of the servlet and calls the servlet init() method. A servlet may be loaded: –automatically at server startup –at first client request –when a servlet is reloaded

Servlet Lifecycle (2) When a client request arrives at the server, the server creates a Request object and a Response object that are specific to that client request. The server then invokes the servlet's service() method, passing both the Request and Response objects as parameters. The servlet gets information about the request from the Request object, processes the request, and uses methods of the Response object to return a response to the client. The service() method may invoke other methods during the processing of the request, e.g. doGet(), doPost(), or additional methods you write. When the server no longer needs the servlet, the server invokes the servlet's destroy() method.

Servlets and Java SDK API The basic JavaServer API is provided in two Java packages, javax.servlet and javax.servlet.http. To create a servlet, extend javax.servlet.http.HttpServlet. (The HttpServlet class is a subclass of GenericServlet and has specialized methods for interacting with data transferred by the Web browser.) There are two methods for passing information to the server from the browser. –These methods, GET and POST, differ primarily in the manner in which the data is supplied. For the GET method, data is encoded and sent along as part of the URL. In the POST method, the data is also encoded but is supplied as a separate data "packet", not part of the URL.

Servlets and Java SDK API (2) The default behavior of the service() method of HttpServlet determines whether the information sent with the request is being provided by GET or POST. If the information is being provided by GET, the service() method invokes the doGet() method. If the information is being provided by POST, the service() method invokes the doPost() method.

HttpServletRequest Objects Provides access to HTTP header data (e.g., cookies found, HTTP method, arguments that the client sent as part of the request). To access client data: –The getParameter method returns the value of a named parameter. If your parameter could have more than one value, use getParameterValues instead. –The getParameterValues method returns an array of values for the named parameter. (The method getParameterNames provides the names of the parameters.)

HttpServletRequest Objects (2) –For HTTP GET requests, the getQueryString method returns a String of raw data from the client. You must parse this data yourself to obtain the parameters and values. –For HTTP POST, PUT, and DELETE requests, If you expect text data, the getReader method returns a BufferedReader for you to use to read the raw data. If you expect binary data, the getInputStream method returns a ServletInputStream for you to use to read the raw data

HttpServletResponse Objects An HttpServletResponse object provides two ways of returning data to the user: –The getWriter method returns a Writer Use the getWriter method to return text data to the user –The getOutputStream method returns a ServletOutputStream Use the getOutputStream method for binary data. Closing the Writer or ServletOutputStream after you send the response allows the server to know when the response is complete.

Handling GET and POST Requests To handle HTTP requests in a servlet, extend the HttpServlet class and override the servlet methods that handle the HTTP requests that your servlet supports. –Handling GET requests involves overriding the doGet method. –Handling POST requests involves overriding the doPost method.

Handling GET Request public class BookDetailServlet extends HttpServlet { public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {... // set content-type header before accessing the Writer response.setContentType("text/html"); PrintWriter out = response.getWriter(); // then write the response out.println(" " + " Book Description " +...); //Get the identifier of the book to display String bookId = request.getParameter("bookId"); if (bookId != null) { // and the information about the book and print it... } out.println(" "); out.close(); }... }

Handling POST Request public class ReceiptServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {... // set content type header before accessing the Writer response.setContentType("text/html"); PrintWriter out = response.getWriter(); // then write the response out.println(" " + " Receipt " +...); out.println(" Thank you for purchasing your books from us " + request.getParameter("cardname") +...); out.close(); }... }

Session Tracking Session tracking is a mechanism that servlets use to maintain state about a series of requests from the same user (that is, requests originating from the same browser) across some period of time. Sessions are shared among the servlets accessed by a client. This is convenient for applications made up of multiple servlets. To use session tracking, –Get a session (an HttpSession object) for a user. –Store or get data from the HttpSession object. –Invalidate the session (optional).

Obtaining a Session The getSession method of the HttpServletRequest object returns a user's session. When you call the method with its create argument as true, the implementation creates a session if necessary. To properly maintain the session, you must call getSession before any output is written to the response.

Storing/Getting Data from a Session The HttpSession interface provides methods that store and return: –Standard session properties, such as a session identifier –Application data, which is stored as a name- value pair, where the name is a String and the value is an object in the Java programming language. (This is like java.util.Dictionary.)

Storing/Getting Data from a Session (2) Because multiple servlets have access to a user's session, you should adopt a naming convention for organizing the names associated with application data. This avoids servlets accidentally overwriting each other's values in the session. One such convention is servletname.name where servletname is the full name of the servlet, including its packages (e.g., com.acme.WidgetServlet.state is a cookie with the servletname com.acme.WidgetServlet and the name state).

Tracking Session: Example public class CatalogServlet extends HttpServlet { public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Get the user's session and shopping cart HttpSession session = request.getSession(true); ShoppingCart cart = (ShoppingCart)session.getValue(session.getId()); // If the user has no cart, create a new one if (cart == null) { cart = new ShoppingCart(); session.putValue(session.getId(), cart); }... }

References Mary Campione, Kathy Walrath, The Java Tutorial Second Edition: Object-Oriented Programming for the Internet, Addison- Wesley, IBM Introduction to Servlets, Online tutorial, 1999.