Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 1 Chapter 34 Servlets.

Slides:



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

4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets.
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.
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
1 CS6320 – Servlet Structure and Lifecycle L. Grewe.
Comp2513 Java Servlet Basics Daniel L. Silver, Ph.D.
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.
Session-02.
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.
Servlets Life Cycle. The Servlet Life Cycle A servlet life cycle can be defined as the entire process from its creation till the destruction. The following.
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.
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
Chapter 5 Java Servlets. Objectives Explain the nature of a servlet and its operation Use the appropriate servlet methods in a web application Code the.
COMP 321 Week 7. Overview HTML and HTTP Basics Dynamic Web Content ServletsMVC Tomcat in Eclipse Demonstration Lab 7-1 Introduction.
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems Web based Applications,
Copyright, 1996 © Dale Carnegie & Associates, Inc. Presented by Hsiuling Hsieh Christine Liu.
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.
Chapter 6 Server-side Programming: Java Servlets
1 CS122B: Projects in Databases and Web Applications Spring 2015 Notes 03: Web-App Architectures Professor Chen Li Department of Computer Science CS122B.
_______________________________________________________________________________________________________________ E-Commerce: Fundamentals and Applications1.
20-Nov-15introServlets.ppt Intro to servlets. 20-Nov-15introServlets.ppt typical web page – source Hello Hello.
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.
L.MARIA MICHAEL VISUWASAM UNIT-4
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,
How CGI and Java Servlets are Run By David Stein 14 November 2006.
Chapter 4 Request and Response. Servlets are controlled by the container.
Java Servlets References: Karen Anewalt, Mary Washington College.
CS122B: Projects in Databases and Web Applications Spring 2017
Introduction to Servlets
CS122B: Projects in Databases and Web Applications Winter 2017
Servlets.
Servlets.
Servlet Fudamentals.
Java Servlets By: Tejashri Udavant..
CSE 403: Servlet Technology
Chapter 26 Servlets.
CS122B: Projects in Databases and Web Applications Winter 2018
Introduction to Servlets
CS122B: Projects in Databases and Web Applications Spring 2018
Servlet APIs Every servlet must implement javax.servlet.Servlet interface Most servlets implement the interface by extending one of these classes javax.servlet.GenericServlet.
COP 4610L: Applications in the Enterprise Spring 2005
J2EE Lecture 1:Servlet and JSP
CS122B: Projects in Databases and Web Applications Winter 2019
Presentation transcript:

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 34 Servlets

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Objectives F To understand the concept of servlets (§34.2). F To run servlets with Tomcat (§34.3). F To know the servlets API (§34.4). F To create simple servlets (§34.5). F To create and process HTML forms (§34.6). F To develop servlets to access databases (§34.7). F To use hidden fields, cookies, and HttpSession to track sessions (§34.8). F To send images from servlets (§34.9).

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Understand the concept of servlets Servlet technology is primarily designed for use with the HTTP protocol of the Web. Servlets are Java programs that run on a Web server. Java servlets can be used to process client requests or produce dynamic Web pages.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved HTTP and HTML

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved CGI The Common Gateway Interface, or CGI, was proposed to generate dynamic Web contents. The interface provides a standard framework for Web servers to interact with external programs, known as the CGI programs.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved How Does CGI Work?

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved The GET and POST Methods The two most common HTTP requests, also known as methods, are GET and POST. The Web browser issues a request using a URL or an HTML form to trigger the Web server to execute a CGI program. When issuing a CGI request directly from a URL, the GET method is used. This URL is known as a query string.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Query String The URL query string consists of the location of the CGI program, parameters and their values. getBalance.cgi?accountId=scott+smith&password=tiger The ? symbol separates the program from the parameters. The parameter name and value are associated using the = symbol. The parameter pairs are separated using the & symbol. The + symbol denotes a space character.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved HTML Forms HTML forms enable you to submit data to the Web server in a convenient form. The form can contain text fields, text area, check boxes, combo boxes, lists, radio buttons, and buttons.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved From CGI to Java Servlets Java servlets are Java programs. They function like CGI programs. They are executed upon the request from Web browser. All the servlets run inside a servlet container. A servlet container is also referred to as a servlet server, or a servlet engine. A servlet container is a single process that runs a JVM. The JVM creates a thread to handle each servlet. Java threads have much less overhead than full- brown processes. All the threads share the same memory allocated to the JVM.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Creating and Running Servlets from To run Java servlets, you need a servlet container. Many servlet containers are available. Tomcat, developed by Apache ( is a standard reference implementation for Java servlet 2.2 and Java Server Pages 1.1.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved The Servlet API The servlet API provides the interfaces and classes that support servlets. These interfaces and classes are grouped into two packages: javax.servlet, and javax.servlet.http.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved The Servlet Interface /**Invoked for every servlet constructed*/ public void init(ServletConfig p0) throws ServletException; /**Invoked to respond to incoming requests*/ public void service(ServletRequest p0, ServletResponse p1) throws ServletException, IOException; /**Invoked to release resource by the servlet*/ public void destroy(); /**Return information about the servlet*/ public String getServletInfo(); /**Return configuration objects of the servlet*/ public ServletConfig getServletConfig();

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Servlet Life-Cycle 1. The init method is called when the servlet is first created, and is not called again as long as the servlet is not destroyed. This resembles the applet’s init method, which is invoked when the applet is created, and is not invoked again as long as applet is not destroyed. 2. The service method is invoked each time the server receives a request for the servlet. The server spawns a new thread and invokes service. 3. The destroy method is invoked once all threads within the servlet's service method have exited or after a timeout period has passed. This method releases resources for the servlet.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved The HTTPServlet Class The HttpServlet class defines a servlet for the HTTP protocol. It extends GenericServlet and implements the service method. The service method is implemented as a dispatcher of HTTP requests. The HTTP requests are processed in the following methods: doGet, doPost, doDelete, doPut, doOptions, and doTrace. All these methods have the same signature as follows: protected void doXxx(HttpServletRequest req, HttpServletResponse resp) throws ServletException, java.io.IOException

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved The HttpServletRequest Interface Every doXxx method in the HttpServlet class has an argument of the HttpServletRequest type, which is an object that contains HTTP request information including parameter name and values, attributes, and an input stream. HttpServletRequest is a subinterface of ServeletRequest. ServletRequest defines a more general interface to provide information for all kinds of clients.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved The HttpServletResponse Interface Every doXxx method in the HttpServlet class has an argument of the HttpServletResponse type, which is an object that assists a servlet in sending a response to the client. HttpServletResponse is a subinterface of ServeletResponse. ServletRequest defines a more general interface for sending output to the client.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Creating Servlets Servlets are opposites of the Java applets. Java applets run from a Web browser on the client side. To write Java programs, you define classes. To write a Java applet, you define a class that extends the Applet class. The Web browser runs and controls the execution of the applet through the methods defined in the Applet class. Similarly, to write a Java servlet, you define a class that extends the HttpServlet class. First Servlet

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Creating Servlets, cont. The servlet engine controls the servlets using the init, doGet, doPost, destroy, and other methods. By default, the doGet and doPost methods do nothing. To handle the GET request, you need to override the doGet method; to handle the POST request, you need to override the doPost method. Example 34.1 Obtaining Current Time from Server

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Example: Obtaining Current Time Based on Locale and Time Zone Current Time

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Database Programming Using Servlets Many dynamic Web applications use databases to store and manage data. Servlets can connect to any relational database via JDBC. Connecting a servlet to a database is no different from connecting a Java application or applet to a database. If you know Java servlets and JDBC, you can combine them together to develop interesting and practical Web based interactive projects immediately. Example 34.3 Registering Student into a Database

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Example: Registering Student into a Database GetParameters

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Session Tracking Web servers use Hyper-Text Transport Protocol (HTTP). HTTP is a stateless protocol. The HTTP Web server cannot associate requests from a client together. Each request is treated independently by the Web server. This protocol works fine for simple Web browsing, where each request typically results in an HTML file or a text file being sent back to the client. Such simple requests are isolated. However, the requests in interactive Web applications are often related.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved What is a Session ? A session can be defined as a series of related interactions between a single client and the Web server over a period of time. To track data among requests in a session is known as session tracking. Session Tracking Techniques Using hidden values, using cookies, and using the session tracking tools from servlet API.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Session Tracking Using Hidden Values You can track session by passing data from the servlet to the client as hidden value in a dynamically generated HTML form by including a field like this: So the next request will submit the data back to the servlet. The servlet retrieves this hidden value just like any other parameter value using the getParameter method.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Example: Using Hidden Values in the Registration form This example creates a servlet that processes a registration form. The client first submits the form using the GET method, as shown in Figure The server collects the data in the form, displays the data to the client, and asks the client for confirmation, as shown in Figure The client confirms it by submitting the request with the hidden values using the POST method. Finally, the servlet writes the data to a database.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Example: Using Hidden Values in the Registration form, cont. Registration

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Session Tracking Using Cookies You can track sessions using cookies. Cookies are small text files that store sets of name=value pairs on the disk in the client’s computer. Cookies are sent from the server through the instructions in the header of the HTTP response. The instructions tell the browser to create a cookie with a given name and its associated value. If the browser already has the cookie with the key name, the value will be updated. The browser will then send the cookie with any request submitted to the same server. Cookies can have expiration dates set, after which the cookies will not be sent to the server. RegistrationWithCookie

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Session Tracking Using the Servlet API The problems of session tracking with hidden data and cookies are that data are not secured and difficult to deal with large set of data. Java servlet API provides a session tracking tool, which enables tracking of a large set of data. Data can be stored as objects. Data are kept on the server side so they are secure.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved The HttpSession Class To use the Java servlet API for session tracking, first create a session object using the getSession method in the HttpServletRequest interface like this: HttpSession session = request.getSession(true); This obtains the session or creates a new session if the client does not have a session on the server. The HttpSession class provides the methods for reading and storing data to the session, and for manipulating the session. RegistrationWithHttpSession,

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Sending Images From the Servlets Java servlets are not limited to sending text to a browser. Java servlets can return images in GIF, JPEG, or PNG format. This section demonstrates returning images in GIF format. To send contents as a GIF image, the content type must be set to image/gif like this: response.setContentType("image/gif"); Images are binary data. You have to use a binary output stream like this: OutputStream out = response.getOutputStream();

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Example: Mixing Images and Texts imageContent