Dispatching, monitoring, filtering

Slides:



Advertisements
Similar presentations
Servlets & JSPs - Sharad Ballepu.
Advertisements

Web Application Development
Internet i jego zastosowania 1 J2EE Servlets. Internet i jego zastosowania 2 Agenda Overview Servlet Interface Servlet Context Request Response Sample.
J0 1 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialitica in Informatica – Università di Trento.
4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets.
6 Copyright © 2005, Oracle. All rights reserved. Using Advanced Techniques in Servlets.
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.
JLab Lattice Portal – Data Grid Web Service Ying Chen, Chip Watson Thomas Jefferson National Accelerator Facility.
1 Servlets Based on Notes by Dave Hollinger & Ethan Cerami Also, the Online Java Tutorial by Sun.
Apache Tomcat as a container for Servlets and JSP
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.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 24: Servlets Outline 24.1 Introduction 24.2 Servlet Overview and Architecture Interface.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 34 Servlets.
Session-02.
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.
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
CSC 2720 Building Web Applications
Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some.
® IBM Software Group © 2007 IBM Corporation Servlet Listeners
Java Servlets & Java Server Pages Lecture July 2013.
JSP Filters 23-Oct-15. JSP - FILTERS A filter is an object that can transform a request or modify a response. Filters are not servlets; they don't actually.
Servlet Communication Other Servlets, HTML pages, objects shared among servlets on same server Servlets on another server with HTTP request of the other.
WebApps (Tomcat configuration). Static pages To let Tomcat serve static pages, we must define a “Web Application”. That is, in the Tomcat Document Root.
CSC 2720 Building Web Applications JavaServer Pages (JSP) The Basics.
20-Nov-15introServlets.ppt Intro to servlets. 20-Nov-15introServlets.ppt typical web page – source Hello Hello.
CSC 2720 Building Web Applications Frameworks for Building Web Applications.
Introduction to Server-Side Web Development Introduction to Server-Side Web Development Session II: Introduction to Server-Side Web Development with Servlets.
® IBM Software Group © 2007 IBM Corporation Servlet Filtering
Servlet Filters import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class TimerFilter implements Filter { /* J2EE v1.3 Filter.
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.
Servlet Filters JAVA Enterprise Edition. Servlet Filters Servlet Filters are Java classes that can be used in Servlet Programming for the following purposes:
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.
Advanced Java Session 6 New York University School of Continuing and Professional Studies.
©SoftMoore ConsultingSlide 1 Filters. Filters can be used in a web application to intercept, examine, and possibly transform requests or responses associated.
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.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg.
® 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,
Introduction To HTML Dr. Magdi AMER. HTML elements.
The Chain of Responsibility Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
1 Lecture 8 George Koutsogiannakis/Summer 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES.
CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles.
Introduction to Servlets
CS3220 Web and Internet Programming Introduction to Java Servlets
Handling Errors in Web Applications
Servlets.
Java Servlets By: Tejashri Udavant..
JavaServer Pages (JSP)
Java Servlets.
Pre assessment Questions
Pre-assessment Questions
Servlet.
Servlet API and Lifecycle
Servlet APIs Every servlet must implement javax.servlet.Servlet interface Most servlets implement the interface by extending one of these classes javax.servlet.GenericServlet.
Knowledge Byte In this section, you will learn about:
CS3220 Web and Internet Programming Introduction to Java Servlets
Introduction to Java Servlets
Presentation transcript:

Dispatching, monitoring, filtering SERVLETS: Dispatching, monitoring, filtering

Dispatching RequestDispatcher dispatch = cntx.getRequestDispatcher("/SecondServlet"); dispatch.forward(req,res); cntx.getRequestDispatcher("/SecondServlet"); dispatch.include(req,res);

Dispatching example package servlets; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServlet; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.ServletContext; import javax.servlet.RequestDispatcher; public class SecondServlet extends HttpServlet { public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException { Printer out=res.getWriter(); System.out.println("Second Servlet Called"); } }

Dispatching example package servlets; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServlet; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.ServletContext; import javax.servlet.RequestDispatcher; public class FirstServlet extends HttpServlet { public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException { Printer out=res.getWriter(); out.println("First Servlet Called"); ServletConfig config = getServletConfig(); ServletContext cntx = config.getServletContext(); RequestDispatcher dispatch = cntx.getRequestDispatcher("/SecondServlet"); dispatch.forward(req,res); } }

Dispatching example <servlet> <servlet-name>FirstServlet</servlet-name> <servlet-class>servlets.FirstServlet</servlet-class> </servlet> <servlet> <servlet-name>SecondServlet</servlet-name> <servlet-class>servlets.SecondServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>FirstServlet</servlet-name> <url-pattern>/firstservlet/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>SecondServlet</servlet-name> <url-pattern>/SecondServlet/*</url-pattern> </servlet-mapping>

Monitoring Servlets Lifecycle Web context Initialization and Destruction ServletContextListener ServletContextEvent Attribute added, removed, or replaced ServletContextAttributeListener ServletContextAttributeEvent Session Creation, invalidation, activation, passivation, and timeout HttpSessionListenerHttpSessionActivationListener HttpSessionEvent HttpSessionAttributeListener HttpSessionBindingEvent Request A servlet request has started being processed by Web components ServletRequestListener ServletRequestEvent ServletRequestAttributeListener ServletRequestAttributeEvent

Monitoring Servlets Lifecycle - Example /* File : ApplicationWatch.java */ import javax.servlet.ServletContextListener; import javax.servlet.ServletContextEvent; public class ApplicationWatch implements ServletContextListener { public static long applicationInitialized = 0L; /* Application Startup Event */ public void contextInitialized(ServletContextEvent ce) { applicationInitialized = System.currentTimeMillis(); } /* Application Shutdown Event */ public void contextDestroyed(ServletContextEvent ce) {} }

Monitoring Servlets Lifecycle - Example /* File : SessionCounter.java */ import javax.servlet.http.HttpSessionListener; import javax.servlet.http.HttpSessionEvent; public class SessionCounter implements HttpSessionListener { private static int activeSessions = 0; /* Session Creation Event */ public void sessionCreated(HttpSessionEvent se) { activeSessions++; } /* Session Invalidation Event */ public void sessionDestroyed(HttpSessionEvent se) { if(activeSessions > 0) activeSessions--; } public static int getActiveSessions() { return activeSessions; } }

Monitoring Servlets Lifecycle - Example <!-- Web.xml --> <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/j2ee/dtds/web-app_2.3.dtd"> <web-app> <!-- Listeners --> <listener> <listener-class> com.stardeveloper.web.listener.SessionCounter </listener-class> </listener> <listener-class> com.stardeveloper.web.listener.ApplicationWatch </listener-class> </web-app>

Scope Objects Main Methods: Object getAttribute(String name) Web context ServletContext Web components within web context servlet.getServletConfig().getServletContext Session HttpSession Web components handling requests that belong to a session Request ServletRequest Web component handling the request Page PageContext Web component in the JSP page Main Methods: Object getAttribute(String name) void setAttribute(String name, Object o) Enumeration getAttributeNames()

AOP The programming paradigms of aspect-oriented programming (AOP), and aspect-oriented software development (AOSD) attempt to aid programmers in the separation of concerns, specifically cross-cutting concerns, as an advance in modularization. Logging and authorization offer two examples of crosscutting concerns: a logging strategy necessarily affects every single logged part of the system. Logging thereby crosscuts all logged classes and methods. Same is true for authorization.

Filters (javax.servlet.filter) Other classes that preprocess/postprocess request/response A filter is an object than perform filtering tasks on either the request to a resource (a servlet or static content), or on the response from a resource, or both. Filters perform filtering in the doFilter method. Every Filter has access to a FilterConfig object from which it can obtain its initialization parameters, a reference to the ServletContext which it can use, for example, to load resources needed for filtering tasks. Filters are configured in the deployment descriptor of a web application Examples that have been identified for this design are 1) Authentication Filters 2) Logging and Auditing Filters 3) Image conversion Filters 4) Data compression Filters 5) Encryption Filters 6) Tokenizing Filters 7) Filters that trigger resource access events 8) XSL/T filters 9) Mime-type chain Filter http://java.sun.com/products/servlet/Filters.html

Filters Filters are important for a number of reasons. First, they provide the ability to encapsulate recurring tasks in reusable units. Second, filters can be used to transform the response from a servlet or a JSP page. A common task for the web application is to format data sent back to the client. Increasingly the clients require formats (for example, WML) other than just HTML.

Filters Filters can perform many different types of functions. * Authentication-Blocking requests based on user identity. * Logging and auditing-Tracking users of a web application. * Image conversion-Scaling maps, and so on. * Data compression-Making downloads smaller. * Localization-Targeting the request and response to a particular locale. * XSL/T transformations of XML content-Targeting web application responses to more that one type of client. There are many more, such as encryption, tokenizing, triggering resource access events, mime-type chaining, and caching.