Internet i jego zastosowania 1 J2EE Servlets. Internet i jego zastosowania 2 Agenda Overview Servlet Interface Servlet Context Request Response Sample.

Slides:



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

Servlets & JSPs - Sharad Ballepu.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10 Servlets and Java Server Pages.
7 Copyright © 2005, Oracle. All rights reserved. Maintaining State in J2EE Applications.
4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets.
11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.
6 Copyright © 2005, Oracle. All rights reserved. Using Advanced Techniques in Servlets.
1 Copyright © 2005, Oracle. All rights reserved. Introducing the Java and Oracle Platforms.
8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages.
JSP and Servelets.
CGI programming. Common Gateway Interface interface between web server and other programs (cgi scripts) information passed as environment variables passed.
M-V-C for web applications. Model for Web Applications model consists of data and system state database tables –persistent data session information –current.
1 Web Search Interfaces. 2 Web Search Interface Web search engines of course need a web-based interface. Search page must accept a query string and submit.
Servlets, JSP and JavaBeans Joshua Scotton.  Getting Started  Servlets  JSP  JavaBeans  MVC  Conclusion.
Dispatching, monitoring, filtering
1 Servlets Based on Notes by Dave Hollinger & Ethan Cerami Also, the Online Java Tutorial by Sun.
Java Server Pages (JSP)
 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
An introduction to Java Servlet Programming
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 34 Servlets.
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.
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.
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.
Java Server Pages B.Ramamurthy. Topics for Discussion 8/20/20152 Inheritance and Polymorphism Develop an example for inheritance and polymorphism JSP.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 8 Servlets (Based on Møller and Schwartzbach,
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
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.
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,
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.
Server-side Programming The combination of –HTML –JavaScript –DOM is sometimes referred to as Dynamic HTML (DHTML) Web pages that include scripting are.
Introduction to Server-Side Web Development Introduction to Server-Side Web Development Session II: Introduction to Server-Side Web Development with Servlets.
Copyright © 2002 ProsoftTraining. All rights reserved. Java 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.
ORIGINAL BY WENDY LIU CURRENT API: Java Servlet and JSP 1.
Advanced Java Session 6 New York University School of Continuing and Professional Studies.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
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.
® IBM Software Group © 2007 IBM Corporation Servlet API (Part II)
Advanced Java Session 6 New York University School of Continuing and Professional Studies.
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,
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
Chapter 4 Request and Response. Servlets are controlled by the container.
Bayu Priyambadha, S.Kom. Static content  Web Server delivers contents of a file (html) 1. Browser sends request to Web Server 3. Web Server sends HTML.
CS122B: Projects in Databases and Web Applications Spring 2017
Introduction to Servlets
Servlets.
Servlets.
Servlet Fudamentals.
Java Servlets By: Tejashri Udavant..
Pre-assessment Questions
Servlet API and Lifecycle
Chapter 26 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.
CS122B: Projects in Databases and Web Applications Winter 2019
Java Chapter 7 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Internet i jego zastosowania 1 J2EE Servlets

Internet i jego zastosowania 2 Agenda Overview Servlet Interface Servlet Context Request Response Sample Servlet Sessions Dispatching Request Web Applications Deployment Descriptor

Internet i jego zastosowania 3 What is a Sevlet? Web component managed by servlet container Generates dynamic content, usually HTML Small, platform-independent classes compiled to bytecode Loaded dynamically and run by a container in conjunction with a web server Interacts with a web client via request - response model based on the the HTTP protocol Can access databases and other storage systems

Internet i jego zastosowania 4 What is a Servlet Container? Execution context of the servlet Provides network services over which requests and responses are exchanged Contains and manages servlets through their lifecycle Can be either built-in into a web server or web enabled application server or installed as an add-on component Must support the HTTP Protocol (at minimum HTTP/1.0) May place security restrictions on the environment that a servlet executes in (e.g. Number of simultenously running threads)

Internet i jego zastosowania 5 Benefits of Servlets Performance Scalability Distributed environment (distributable web applications) Multithreaded environment Portability Platform independence Ease of development Part of J2EE (Servlet API is a required API of J2EE) Standard supported by many vendors and web servers

Internet i jego zastosowania 6 Typical Scenario Request is submitted from a web browser Request is converted to a name-value list by the servlet container The service() method of a servlet is called with the name- value list together with the response as arguments The servlet processes the request by invoking EJBs, performing other business logic, connecting to databases or application server extensions The servlet prepares HTML code to return either directly printing to the response stream

Internet i jego zastosowania 7 The Servlet Interface void init(ServletConfig config); void service(ServletRequest req, ServletResponse res); void destroy(); ServletConfig getServletConfig(); java.lang.String getServletInfo(); Central abstraction of the Servlet API Interface implemented either directly or by extending

Internet i jego zastosowania 8 Servlet Life Cycle Loading and instantiating At start time Delayed until the service method of the servlet is requested The servlet container load the servlet class (file system, remote file system, other network services) The Servlet Container instantiates an object instance There can be more than one instance (SingleThreadModel) of a given servlet (several servlet definitions which point to the same class) or only one instance (MultipleThreadModel) Initialization Point where servlet can read any configuration data, persistent data, make a connection to a database and other costly resources, perform one-time activities The Servlet Container calls init method of the servlet and passes ServletConfig - access to name-value initialization parameters On error throw ServletException or UnavailableException

Internet i jego zastosowania 9 Servlet Life Cycle End of service A servlet instance may be kept active in a container for any amount of time The container allow any threads executing service method to complete The container executes destroy method - point where a servlet may release any resources and save its state to any persistance storage No requests can be routed to this instance of the servlet The instance is released so that it can be garbage collected

Internet i jego zastosowania 10 Servlet Life Cycle Request handling Each request represented by ServletRequest object Servlet can create the response by using object of type ServletResponse ServletRequest and ServletResponse are provided as parameters to service HTTP - HttpServletRequest, HttpServletResponse Errors during request handling ServletException UnavailableException Permanent - servlet should be removed from the service (destroy is called) Temporary - SERVICE_UNAVAILABLE (503) is returned with Retry- After header

Internet i jego zastosowania 11 Multithreading Requests MultipleThreadModel (default) One instance of a servlet class per servlet definition SingleThreadModel (marker interface) Multiple instances of the servlet. Ensures that servlets handle only one request at a time. No two threads will execute concurrently in the servlet's service method. The servlet container can make this guarantee by synchronizing access to a single instance of the servlet, or by maintaining a pool of servlet instances and dispatching each new request to a free servlet. The servlet is thread safe. SingleThreadedModel does not prevent synchronization problems that result from servlets accessing shared resources (static class variables, classes outside the scope of the servlet).

Internet i jego zastosowania 12 The Servlet Context A servlet's view of the web application within which it is running One instance per web application (per VM for distributed applications) The container implements the ServletContext interface ServletContext allows a servlet to set and store attributes, log events, obtain URL references to resources provides a set of methods that a servlet uses to communicate with its servlet container Context attributes - available to any other servlet of the web application (exits only locally in the VM) Resources - direct access to static documents (HTML, GIF, JPEG)

Internet i jego zastosowania 13 The Request Encapsulates information from the client HttpServletRequest - HTTP headers and message body Parameters getParameter, getParameterNames, getParameterValues String parameters stored as name-value pairs Possible multiple parameter values Attributes getAttribute, getAttributeNames, getAttributeValues Objects associated with a request set by the container or a servlet to communicate with another servlet Single values Cookies getCookies

Internet i jego zastosowania 14 Path Elements & Translation Request path is composed of Context PathgetContextPath Servlet PathgetServletPath Following is always true RequestURI = contextPath + servletPath Pattern:/garden Servlet:GardenServlet /catalog/garden/implements->ContextPath: /catalog ServletPath: /garden

Internet i jego zastosowania 15 The Response Encapsulates information to be returned to the client HttpServletResponse - HTTP headers and message body Cookies Helper methods sendRedirect sendError Typical use Either getOutputStream (binary data) or getWriter (text)

Internet i jego zastosowania 16 Sample Servlet public class SampleServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(" "); out.println("Hello World!"); out.println(" "); }

Internet i jego zastosowania 17 Sessions HTTP - stateless protocol Different requests from the same client should be associated with each other Strategies for session tracking Hidden variables URL rewriting Cookies In servlet environment Session tracking implemented by servlet container HttpSession provided as an convenient interface A session persists for a specified period of time

Internet i jego zastosowania 18 HttpSession View and manipulate information about a session, such as the session identifier, creation time, last accessed time Bind objects to sessions, allowing user information to persist across multiple user connections Session information is scoped only to the current web application (ServletContext) Binding attributes into a session getAttribute, setAttribute, removeAttribute HttpSessionBindingListener - causes an object to be notified when it is bound to or unbound from a session Session timeouts because there is no explicit client termination

Internet i jego zastosowania 19 Session Semantics Multiple servlets may have access to a single session object Synchronization is the responsibility of a developer For applications marked as distributable session objects must implement java.io.Serializable interface Scalability - in distributed environment session objects can be moved from any active node to some other node of the system All windows of a client are participating in the same session

Internet i jego zastosowania 20 Dispatching Requests forward - pass processing to another servlet Including output of another servlet in the response RequestDispatcher; can be obtained from ServletContext include Can be called at any time Included servlet cannot set headers or call any method that affects the headers of the response forward Can only be called if no output has been commited to a client

Internet i jego zastosowania 21 Web Applications Collection of servlets, JSPs, HTML pages and other resources bundled and run on possibly multiple containers Rooted at a specific path eg. By default one instance must only be run on one VM Distributable web applications - components distributed across multiple containers 1:1 mapping between web application and ServletContext Representation Structured hierarchy of directories Archive file

Internet i jego zastosowania 22 Open Directory Structure Root directory eg. /catalog Special WEB-INF directory - not a part of public document tree Sample directory structure: /index.html /howto.jsp /feedback.jsp /images/logo.gif /WEB-INF/web.xml /WEB-INF/lib/jspbeans.jar /WEB-INF/classes/com/dsrg/servlets/Test.class /WEB-INF/classes/com/dsrg/util/Helper.class

Internet i jego zastosowania 23 Deployment Descriptor Elements and configuration information of a web application Contents of a deployment descriptor ServletContext init parameters Session configuration Servlet/JSP definitions Servlet/JSP mappings Mime Type mappings Welcome file list Error pages Security info XML document