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.

Slides:



Advertisements
Similar presentations
Chapter 6 Server-side Programming: Java Servlets
Advertisements

Internet i jego zastosowania 1 J2EE Servlets. Internet i jego zastosowania 2 Agenda Overview Servlet Interface Servlet Context Request Response Sample.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10 Servlets and Java Server Pages.
4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets.
Servlets, JSP and JavaBeans Joshua Scotton.  Getting Started  Servlets  JSP  JavaBeans  MVC  Conclusion.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 34 Servlets.
1 CS6320 – Servlet Structure and Lifecycle L. Grewe.
Servlets CS-328 Dick Steflik. What is a servlet A Java application run on a thread of the webserver in response to an HTTP GET or POST request. The servlet.
Servlets Replace Common Gateway Interface Scripts Extend Server Functionality Modules (software components) Like applets to browsers No GUI.
Comp2513 Java Servlet Basics Daniel L. Silver, Ph.D.
Java Server and Servlet CS616 Team 9 Kim Doyle, Susan Kroha, Arunima Palchowdhury, Wei Xu.
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.
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.
Robinson_CIS285Winter2005 Servlets CIS January 2005 Mary Robinson.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
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. - Java technology for Common Gateway Interface (CGI) programming. - It is a Java class that dynamically extends the function of a web server.
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,
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.
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.
Servlets Server-Side Software. Objective What is a servlet? Where do servlets fit in? What can servlets do? Why are servlets better than CGI? Servlet.
Server-side Programming The combination of –HTML –JavaScript –DOM is sometimes referred to as Dynamic HTML (DHTML) Web pages that include scripting are.
Java Servlet API CGI / HTTP Concepts Java Servlet API.
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
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.
SERVLET THETOPPERSWAY.COM
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
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.
CSE 403: Servlet Technology
Servlet API and Lifecycle
Servlet.
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.
Objectives In this lesson you will learn about: Need for servlets
J2EE Lecture 1:Servlet and JSP
CS122B: Projects in Databases and Web Applications Winter 2019
Presentation transcript:

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 implement a service easily and efficiently. –Easily: programmer may take advantage of support provided by Java Web Server: l parsing HTTP requests l handling connections to Web clients. –Efficiently, run as separate light weight threads of the Java Server process rather than separate heavy weight processes l load and execute much faster than CGI programs. l Servlets are part of a larger Java Server Architecture. – first implemented in a server built by Sun that was called Jeeves, later renamed the Java Web Server java.sun.com/products/java_server/webserver.

2 Servlets l Servlets have several advantages: –Compiled –Crash-resistant –Capable of running in-process –Cross-platform –Durable –Dynamically loadable across the network –Extensible –Multi-threaded –Protocol independent –Secure –Written in Java

3 l Servlets are protocol- and platform-independent server-side components, written in Java, to dynamically extend Java-enabled servers. –run inside servers, don’t need a GUI. –downloaded on demand to system that requests them. Client 1 -Applet Client 2 -HTML form Java Server Call Servlet Servlet1Servlet2

4 Life Cycle l A servlet is accessed through a URL, just as if it were a file or a CGI program. l When a request for a particular servlet is received, the Java Server invokes the servlet as a separate thread of execution. l Once started, it can receive environment information as well as client query information similar to that provided through the CGI. It processes the request and returns its data, formatted as HTML, to be included in the body of the HTTP response generated by the Java Server. l The servlet is then destroyed by the Java Server.

5 Servlet uses l can process data which was POSTed over HTTPS using an HTML FORM, passing data such as a purchase order (with credit card data). l servlets handle multiple requests concurrently: requests can be synchronized with each other to support collaborative applications such as on-line conferencing. l servlet can forward requests other servers. – balance load among several servers which mirror same content. l ….. –cgi servlet can handle cgi requests –file loader servlet can handle file loading requests –...

6 Class Hierarchy l Servlets are implemented using the Java Servlet API. l All servlets implement the Servlet interface. –extend either the GenericServlet class, which implements the Servlet interface, or its subclass, HttpServlet. l 2 packages: javax.servlet and javax.servlet.http. l not part of the core Java framework, so do not begin with java. part of the Standard Java Extension API, begin with the keyword javax. l Create a servlet as an extension of HttpServlet (which is a subclass of GenericServlet, which implements Servlet interface).

7 The Basic Framework For a Servlet class Extend and use the default functionality of one of two servlet classes – GenericServlet or HTTPServlet. - Override at least one of the methods – init(), service(), doGet(), doPost(), doPut() to provide custom functionality. import javax.servlet.*; import javax.servlet.http.*; import java.io.*; /** * A Basic Framework for a Servlet **/ public class FrameServlet extends HttpServlet { public void init() { // servlet initialization code goes here } public void service(){ //The custom code to offer the service goes here } public void destroy(){ // free resources here }

8 Example 1 – Class SampleServlet l Refer to SampleServlet.java l After being loaded, servlet life cycle includes three methods: l init() - server activates servlet –perform potentially costly (usually, I/O intensive) setup only once, rather than once per request. l initializing sessions with other network services or l getting access to their persistent data (stored in a database or file). l service() - servlet handles many requests. Each client request generates one service() call. –requests may be concurrent; allows servlets to coordinate activities among many clients. –Class-static state may be used to share data between requests. l destroy() - requests processed until servlet is explicitly shut down by the web server, by calling the destroy method. –Servlet's class may become eligible for garbage collection.

9 HTTPServletRequest and HTTPServletResponse l When a servlet accepts a call from a client, it receives two objects: –An object of type HTTPServletRequest, encapsulates communication from client to the server. –An HttpServletRequest object provides access to HTTP header data, such as any cookies found in the request and the HTTP method with which the request was made. –The HttpServletRequest object also allows you to obtain the arguments that the client sent as part of the request. To access client data: l 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.) l 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. l 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 –

10 l A HTTPServletResponse, encapsulates communication from servlet back to the client. –Allows servlet to set the content length and MIME type of the reply. –Provides an output stream, ServletOutputStream, and a Writer through which the servlet can send the reply data. –Use the getWriter method to return text data to the user, and the getOutputStream method for binary data. l Closing the Writer or ServletOutputStream after you send the response allows the server to know when the response is complete. l ServletRequest and ServletResponse are interfaces defined by the javax.servlet package.

11 Servlet Model l Servlets are java objects, so they: l Have instance-specific data l Encapsulate user sessions –each servlet instantiated inside server is a separate entity l Can access environment through servletcontext object –allow inter-servlet sharing of data

12 l The service method is provided with Request and Response parameters. –Servlets retrieve data through an input stream, and send responses using an output stream: l ServletInputStream in = request.getInputStream (); l ServletOutputStream out = response.getOutputStream (); l Input and output streams may be used with data in whatever format is appropriate. –object serialization –HTML – image formats

13 Example 2: Class VisitorInfoServlet l Refer to VisitorInfoServlet.java