Integrating Servlets and JavaServer Pages Vijayan Sugumaran School of Business Administration Oakland University Parts of this presentation provided by.

Slides:



Advertisements
Similar presentations
Java Server Pages (JSP)
Advertisements

JSP1 Java Server Pages (JSP) Introducing JavaServer Pages TM (JSP TM ) JSP scripting elements.
 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
Java Servlet & JSP © copyright 2005 SNU OOPSLA Lab.
DT211/3 Internet Application Development JSP: Processing User input.
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:
Introduction to JavaServer Pages Vijayan Sugumaran Dept. of DIS Oakland University Parts of this presentation was provided by
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 10 Object Oriented Programming in Java Advanced Topics Servlets.
MVC Architecture1 Usage Rules PowerPoint slides for use only in for-credit courses at degree-granting institutions Slides can be modified.
JSP Architecture  JSP is a simple text file consisting of HTML or XML content along with JSP elements  JSP packages define the interface for the compiled.
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.
CSCI 6962: Server-side Design and Programming History and Background.
Introduction to Java web programming Dr Jim Briggs JWP intro1.
Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.
UNIT-V The MVC architecture and Struts Framework.
Netbeans – jsp.zip Introduction to JSP Netbeans – jsp.zip.
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.
CSC 2720 Building Web Applications Using Java Beans, Custom Tags and Tag Libraries in JSP pages.
Introduction to JSP Based on: Marty Hall, Larry Brown, Core Servlets and JavaServer Pages.
JDeveloper 10g and JavaServer Faces: High-Performance UIs on the Web Avrom Roy-Faderman Senior Programmer May, 2006.
What Are Beans? beans are simply Java classes that are written in a standard format. A bean class must have a zero-argument (default) constructor. A bean.
JSP Java Server Pages Softsmith Infotech.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
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.
Java Server Pages Lecture July Java Server Pages Java Server Pages (JSPs) provide a way to separate the generation of dynamic content (java)
JSP Architecture Outline  Model 1 Architecture  Model 2 Architecture.
COMP 321 Week 7. Overview HTML and HTTP Basics Dynamic Web Content ServletsMVC Tomcat in Eclipse Demonstration Lab 7-1 Introduction.
Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some.
JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA James Faeldon CS 119 Enterprise Systems Programming.
Slides © Marty Hall, book © Sun Microsystems Press 1 JSP Scripting Elements Core Servlets & JSP book:
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.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
Introduction to JavaServer Pages. 2 JSP and Servlet Limitations of servlet  It’s inaccessible to non-programmers JSP is a complement to servlet  focuses.
JSP Fundamentals Elements of a JSP Using Beans with JSP Integrating Servlets and JSP.
CSC 2720 Building Web Applications JavaServer Pages (JSP) The Basics.
Copyright © 2002 ProsoftTraining. All rights reserved. JavaServer Pages.
Java Server Pages (JSP)
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.
Server-side Programming The combination of –HTML –JavaScript –DOM is sometimes referred to as Dynamic HTML (DHTML) Web pages that include scripting are.
Fall 2007cs4201 Advanced Java Programming Umar Kalim Dept. of Communication Systems Engineering
 Obtaining a RequestDispatcher Forwarding requests from servlets to dynamic resources  Forwarding requests from servlets to static resources  Using.
Chapter 11 Invoking Java Code with JSP Scripting Elements.
CSC 2720 Building Web Applications JavaServer Pages (JSP) JSP Directives and Action Elements.
Fall 2007cs4201 Advanced Java Programming Umar Kalim Dept. of Communication Systems Engineering
JSP Pages. What and Why of JSP? JSP = Java code imbedded in HTML or XML –Static portion of the page is HTML –Dynamic portion is Java Easy way to develop.
Slides © Marty Hall, book © Sun Microsystems Press 1 Using JavaBeans with JSP Core Servlets & JSP book:
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
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.
Vakgroep Informatietechnologie – Onderzoeksgroep (naam) Web Centric Design of Distributed Software.
INVOKING JAVA CODE WITH JSP SCRIPTING ELEMENTS. Creating Template Text A large percentage of your JSP document consists of static text (usually HTML),
Introduction To HTML Dr. Magdi AMER. HTML elements.
 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.
Java Server Pages. 2 Servlets The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
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.
CS 562 Advanced Java and Internet Application Computer Warehouse Web Application By Team Alpha :-  Puja Mehta (102163)  Mona Nagpure (102147)
Enterprise Java v050228MVC1 Model, View, Controller Web Architecture.
Speaker Name Speaker Title Speaker Title Oracle Corporation Olivier Le Diouris Principal Product Manager Oracle Corporation Building Servlet and JSP Applications.
JSP Action Elements Lec - 37.
Java Servlets By: Tejashri Udavant..
Java Server Pages By: Tejashri Udavant..
COMP9321 Web Application Engineering Semester 2, 2017
Scripted Page Web App Development (Java Server Pages)
Scope and State Handling in JSPs
Engineering the Java Web Application (MVC)
Presentation transcript:

Integrating Servlets and JavaServer Pages Vijayan Sugumaran School of Business Administration Oakland University Parts of this presentation provided by

Agenda  Reasons to combine servlets and JSP  Approach to integration  Dispatching requests  Forwarding requests: an on-line travel agent  Including requests: showing raw servlet and JSP output

Uses of JSP Constructs  Scripting elements calling servlet code directly  Scripting elements calling servlet code indirectly (by means of utility classes)  Beans  Custom tags  Servlet/JSP combo (MVC), with beans and possibly custom tags Simple Application Complex Application

Why Combine Servlets & JSP?  Typical picture: use JSP to make it easier to develop and maintain the HTML content  For simple dynamic code, call servlet code from scripting elements  For slightly more complex applications, use custom classes called from scripting elements  For moderately complex applications, use beans and custom tags  But, that's not enough  For complex processing, starting with JSP is awkward  Despite the ease of separating the real code into separate classes, beans, and custom tags, the assumption behind JSP is that a single page gives a single basic look

Approach  Joint servlet/JSP process:  Original request is answered by a servlet  Servlet processes request data, does database lookup, business logic, etc.  Results are placed in beans  Request is forwarded to a JSP page to format result  Different JSP pages can be used to handle different types of presentation  Often called the "MVC (Model View Controller" or "Model 2" approach to JSP  Formalized in Apache Struts Framework 

Dispatching Requests  First, call the getRequestDispatcher method of ServletContext  Supply URL relative to server or Web application root  Example  String url = "/presentations/presentation1.jsp"; RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);  Second  Call forward to completely transfer control to destination page (no communication with client in between, as with response.sendRedirect)  Call include to insert output of destination page and then continue on

Forwarding Requests: Example Code public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String operation = request.getParameter("operation"); if (operation == null) { operation = "unknown"; } if (operation.equals("operation1")) { gotoPage("/operations/presentation1.jsp", request, response); } else if (operation.equals("operation2")) { gotoPage("/operations/presentation2.jsp", request, response); } else { gotoPage("/operations/unknownRequestHandler.jsp", request, response); }

Forwarding Requests: Example Code (Continued) private void gotoPage(String address, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(address); dispatcher.forward(request, response); }

Reminder: JSP useBean Scope Alternatives  request   session   application   page  or just  This scope is not used in MVC (Model 2) architecture

Storing Data for Later Use: The Servlet Request  Purpose  Storing data that servlet looked up and that JSP page will use only in this request.  Servlet syntax to store data SomeClass value = new SomeClass(…); request.setAttribute("key", value); // Use RequestDispatcher to forward to JSP  JSP syntax to retrieve data

Storing Data for Later Use: The Servlet Request (Variation)  Purpose  Storing data that servlet looked up and that JSP page will use only in this request. Servlets 2.2 and later only.  Servlet syntax to store data  Add new request parameters to servlet request String address ="/path/resource.jsp?newParam=value"; RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(address); dispatcher.forward(request, response);  JSP syntax to retrieve data  No useBean syntax. However, recall that request parameters can be accessed without explicit Java code by means of jsp:setProperty

Storing Data for Later Use: The Session Object  Purpose  Storing data that servlet looked up and that JSP page will use in this request and in later requests from same client.  Servlet syntax to store data SomeClass value = new SomeClass(…); HttpSession session = request.getSession(true); session.setAttribute("key", value); // Use RequestDispatcher to forward to JSP  JSP syntax to retrieve data

Storing Data for Later Use: The Servlet Context  Purpose  Storing data that servlet looked up and that JSP page will use in this request and in later requests from any client.  Servlet syntax to store data SomeClass value = new SomeClass(…); getServletContext().setAttribute("key", value); // Use RequestDispatcher to forward to JSP  JSP syntax to retrieve data

Relative URLs in JSP Pages  Issue:  Forwarding with a request dispatcher is transparent to the client  Why does this matter?  What will browser do with tags like the following: …  Answer: browser treats them as relative to servlet URL  Solution  Use URLs that begin with a slash

Example: An On-Line Travel Agent

 All requests include  address, password, trip origin, trip destination, start date, and end date  Original request answered by servlet  Looks up real name, address, credit card information, frequent flyer data, etc., using address and password as key. Data stored in session object.  Depending on what button user pressed, request forwarded to:  Page showing available flights, times, and costs  Page showing available hotels, features, and costs  Rental car info, edit customer data, error handler

An On-Line Travel Agent: Servlet Code public void doPost(HttpServletRequest request, HttpServletResponse response)...// Store data in TravelCustomer bean called "customer" HttpSession session = request.getSession(true); session.setAttribute("customer", customer); if (request.getParameter("flights") != null) { gotoPage("/travel/BookFlights.jsp", request, response); } else if... } private void gotoPage(String address, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(address); dispatcher.forward(request, response); }

An On-Line Travel Agent: JSP Code (Flight Page) Best Available Flights <jsp:useBean id="customer" class="coreservlets.TravelCustomer" scope="session" /> Finding flights for <jsp:getProperty name="customer" property="fullName" />...

Forwarding Requests from JSP Pages -- jsp:forward  You usually forward from a servlet to a JSP page, but you can also forward from a JSP page also <% String destination; if (Math.random() > 0.5) { destination = "/examples/page1.jsp"; } else { destination = "/examples/page2.jsp"; } %> " />

Including Pages Instead of Forwarding to Them  With the forward method of RequestDispatcher:  Control is permanently transferred to new page  Original page cannot generate any output  With the include method of RequestDispatcher:  Control is temporarily transferred to new page  Original page can generate output before and after the included page  Original servlet does not see the output of the included page ("servlet chaining" is not a standard capability)  Useful for portals: JSP gives common pieces, but pieces arranged in different orders for different users

A Servlet that Shows Raw Servlet and JSP Output out.println(... " "); if ((url == null) || (url.length() == 0)) { out.println("No URL specified."); } else { // Attaching data works only in version 2.2. String data = request.getParameter("data"); if ((data != null) && (data.length() > 0)) { url = url + "?" + data; } RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url); dispatcher.include(request, response); } out.println(" \n" +...);

A Servlet that Shows Raw Servlet and JSP Output

Summary  Use MVC (Model 2) approach when:  One submission will result in more than one basic look  Several pages have substantial common processing  Architecture  A servlet answers the original request  Servlet does the real processing & stores results in beans  Beans stored in HttpServletRequest, HttpSession, or ServletContext  Servlet forwards to JSP page via forward method of RequestDispatcher  JSP page reads data from beans by means of jsp:useBean with appropriate scope (request, session, or application)