1 CS6320 – Servlet Request Dispatcher L. Grewe 2 What is the purpose Forward a request from one servlet to another (or jsp). Forward a request from one.

Slides:



Advertisements
Similar presentations
4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets.
Advertisements

6 Copyright © 2005, Oracle. All rights reserved. Using Advanced Techniques in Servlets.
Servlets, JSP and JavaBeans Joshua Scotton.  Getting Started  Servlets  JSP  JavaBeans  MVC  Conclusion.
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.
Server Side Programming Common Gateway Interface (CGI): Scripts generate Web pages or other files dynamically by processing form data and returning documents.
COMP 321 Week 8. Overview MVC Example Servlet Lifecycle Servlet Mechanics MVC Lab 5-2 Solution Lab 8-1 Introduction.
Introduction to Servlets Based on: Hall, Brown, Core Servlets and JavaServer Pages.
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 Stoney Jackson
1 Servlets Part 2 Representation and Management of Data on the Web.
J2EE Servlets and JSP Advanced topics Presented by Bartosz Sakowicz.
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
JSP Java Server Pages Reference:
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 10 Object Oriented Programming in Java Advanced Topics Servlets.
Servlets. A form The HTML source Chapter 1 Please enter your name and password then press start Name: Password: In Netbeans you can graphically create.
1 CS6320 – Servlet Structure and Lifecycle L. Grewe.
1 Servlets Part 2 Representation and Management of Data on the Web.
1 CS6320 – Servlet Cookies L. Grewe 2 What is a cookie? Name-value bindings sent by a server to a web browser and then sent back unchanged by the browser.
Servlets. Our Project 3-tier application Develop our own multi-threaded server Socket level communication.
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.
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.
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.
111 Java Servlets Dynamic Web Pages (Program Files) Servlets versus Java Server Pages Implementing Servlets Example: F15 Warranty Registration Tomcat Configuration.
Chapter 7 Java Server Pages. Objectives Explain how the separation of concerns principle applies to JSP Describe the operation and life-cycle of a JSP.
CGS – 4854 Summer 2012 Web Site Construction and Management Instructor: Francisco R. Ortega Chapter 2.
Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some.
Servlet Lifecycle Lec 28. Servlet Life Cycle  Initialize  Service  Destroy Time.
Servlet Communication Other Servlets, HTML pages, objects shared among servlets on same server Servlets on another server with HTTP request of the other.
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
20-Nov-15introServlets.ppt Intro to servlets. 20-Nov-15introServlets.ppt typical web page – source Hello Hello.
 Obtaining a RequestDispatcher Forwarding requests from servlets to dynamic resources  Forwarding requests from servlets to static resources  Using.
Java Servlet API CGI / HTTP Concepts Java Servlet API.
Representation and Management of Data on the Internet, 2007
Introduction to Server-Side Web Development Introduction to Server-Side Web Development Session II: Introduction to Server-Side Web Development with Servlets.
Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection.
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.
Introduction to Servlets. Introduction Servlet is a language to develop the server side applications, and it is also server side component. It can develop.
CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles.
©SoftMoore ConsultingSlide 1 Filters. Filters can be used in a web application to intercept, examine, and possibly transform requests or responses associated.
® IBM Software Group © 2007 IBM Corporation Servlet API (Part II)
Java and the Web CSE 3330 Southern Methodist University.
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.
1 Servlets Part 2 Representation and Management of Data on the Web.
How CGI and Java Servlets are Run By David Stein 14 November 2006.
 Java Server Pages (JSP) By Offir Golan. What is JSP?  A technology that allows for the creation of dynamically generated web pages based on HTML, XML,
1 Servlets – Part 2 Representation and Management of Data on the Web.
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.
CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles.
Introduction to Servlets
Servlet Fudamentals.
Net-centric Computing
JDBC & Servlet CSE 4504/6504 Lab.
HTTP Servlet Overview Servlets are modules that extend request/response-oriented servers, such as Java-enabled web servers. For example, a servlet might.
Servlets.
Generating the Server Response: HTTP Status Codes
Servlets and Java Server Pages
In Class Assg 2 - solution
Servlet APIs Every servlet must implement javax.servlet.Servlet interface Most servlets implement the interface by extending one of these classes javax.servlet.GenericServlet.
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.
Modified slides from Dr.Sagiv
Presentation transcript:

1 CS6320 – Servlet Request Dispatcher L. Grewe

2 What is the purpose Forward a request from one servlet to another (or jsp). Forward a request from one servlet to another (or jsp). Have first servlet do some of the work and then pass on to another. Have first servlet do some of the work and then pass on to another. Can even forward on to a static source like html Can even forward on to a static source like html

3 The Request Dispather The RequestDispatcher object is used to send a a client request to any resource on the server The RequestDispatcher object is used to send a a client request to any resource on the server Such a resource may be dynamic (e.g. a Servlet or a JSP file) or static (e.g. a HTML document) Such a resource may be dynamic (e.g. a Servlet or a JSP file) or static (e.g. a HTML document) To send a request to a resource x, use: To send a request to a resource x, use: getServletContext().getRequestDispatcher("x")

4 Request Dispatcher Methods void forward(ServletRequest request, ServletResponse response) void forward(ServletRequest request, ServletResponse response) Forwards a request from a Servlet to another resourceForwards a request from a Servlet to another resource void include(ServletRequest request, ServletResponse response) void include(ServletRequest request, ServletResponse response) Includes the content of a resource in the current responseIncludes the content of a resource in the current response

5 Passing on Data 3 different ways to pass parameters for the forwarded Servlet or JSP 3 different ways to pass parameters for the forwarded Servlet or JSP Data that will be used only for this request:Data that will be used only for this request: request.setAttribute("key", value); Data will be used for this client (also for future requests):Data will be used for this client (also for future requests): session.setAttribute("key", value); Data that will be used in the future for every clientData that will be used in the future for every client context.setAttribute("key", value);

6 An Example The Servlet JokesAndImages enables a user to choose a random joke or a random image The Servlet JokesAndImages enables a user to choose a random joke or a random image The server has 5 images in the directory images/ and five jokes ( txt files) in the directory jokes/ The server has 5 images in the directory images/ and five jokes ( txt files) in the directory jokes/ Empty requests are forwarded to a HTML file that enables the user to choose a joke or an image Empty requests are forwarded to a HTML file that enables the user to choose a joke or an image Requests to a joke are forwarded to the servlet Jokes Requests to a joke are forwarded to the servlet Jokes Requests to an image are forwarded to a random image from the directory images/ Requests to an image are forwarded to a random image from the directory images/

7 Jokes and Images Images and Jokes Please Select: <input type="submit" name="joke" value="A Joke" /> <input type="submit" name="image" value="An Image" /> imagesJokesOptions.html

8 Jokes and Images (cont) public class JokesAndImages extends HttpServlet { public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { int randomNum = 1 + Math.abs((new Random()).nextInt() % 5); if (req.getParameter("joke") != null) { req.setAttribute("jokeNumber", new Integer(randomNum)); getServletContext().getRequestDispatcher("/Jokes").forward(req,res); } else if (req.getParameter("image") != null) { getServletContext().getRequestDispatcher("/images/image" + randomNum + ".gif").forward(req, res); } else getServletContext().getRequestDispatcher ("/imagesJokesOptions.html"). forward(req,res); } public void doGet... }} JokesAndImages.java

9 Jokes and Images (cont) public class Jokes extends HttpServlet { public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println(" A Joke "); int jokeNum = ((Integer) req.getAttribute("jokeNumber")).intValue(); getServletContext().getRequestDispatcher ("/jokes/joke" + jokeNum + ".txt").include(req, res); out.println("\n "); out.println(" Back "); out.println(" "); }} Jokes.java

10 Forwarding versus Redirection By default, SendRedirect does not preserve parameters of the request By default, SendRedirect does not preserve parameters of the request SendRedirect ends up with a different URL on the client SendRedirect ends up with a different URL on the client