Michael Brockway Application Integration Servlets l Introduction & Overview l HTTP Servlets l HTTP get Requests l HTTP post Requests l Multi-tier Applications.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
 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.
Servlets and a little bit of Web Services Russell Beale.
WEB1P servintro1 Introduction to servlets and JSP Dr Jim Briggs.
An introduction to Java Servlet Programming
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 34 Servlets.
2/16/2004 Dynamic Content February 16, /16/2004 Assignments Due – Message of the Day Part 1 Due – Reading and Warmup Work on Message of the Day.
Enhancing a Web Server with Servlets. Servlets Here focus on both sides of a client-server relationship. The client requests that some action be performed.
Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET.
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.
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. 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 and JSP.
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.
Introduction to servlet programming Pertemuan 6 Matakuliah: Web Programming Tahun: 2009.
1 Servlet How can a HTML page, displayed using a browser, cause a program on a server to be executed?
 2005 Pearson Education, Inc. All rights reserved Servlets.
Chapter 17 - Deploying Java Applications on the Web1 Chapter 17 Deploying Java Applications on the Web.
Objectives Java Servlet Web Components
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 30 - Servlets: Bonus for Java Developers Outline 30.1 Introduction 30.2 Servlet Overview and.
Java support for WWW Babak Esfandiari (sources: Qusay Mahmoud, Roger Impey, textbook)
CSC 2720 Building Web Applications
111 Java Servlets Dynamic Web Pages (Program Files) Servlets versus Java Server Pages Implementing Servlets Example: F15 Warranty Registration Tomcat Configuration.
COMP 321 Week 7. Overview HTML and HTTP Basics Dynamic Web Content ServletsMVC Tomcat in Eclipse Demonstration Lab 7-1 Introduction.
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems Web based Applications,
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 Outline 24.1 Introduction 24.2 Servlet Overview and Architecture Interface Servlet and the Servlet Life Cycle HttpServlet Class.
Java Servlets & Java Server Pages Lecture July 2013.
20-Nov-15introServlets.ppt Intro to servlets. 20-Nov-15introServlets.ppt typical web page – source Hello Hello.
A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing.
Java Servlet API CGI / HTTP Concepts Java Servlet API.
@2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING.
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
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.
 2002 Prentice Hall. All rights reserved. 9.8 Multi-Tier Applications: Using JDBC from a Servlet Three-tier distributed applications –User interface –Business.
Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 24: Servlets Outline 24.1 Introduction 24.2 Servlet Overview and Architecture Interface.
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.
Java Servlets and Java Server Pages
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.
1 Lecture 8 George Koutsogiannakis/Summer 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES.
Servlets Outline 1 Introduction 2 Servlet Overview and Architecture 2.1 Interface Servlet and the Servlet Life Cycle 2.2 HttpServlet Class 2.3 HttpServletRequest.
 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
Java Servlets References: Karen Anewalt, Mary Washington College.
Introduction to Servlets
Servlets.
Servlet Fudamentals.
Java Servlets By: Tejashri Udavant..
Pre-assessment Questions
Chapter 26 Servlets.
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
COP 4610L: Applications in the Enterprise Spring 2005
COP 4610L: Applications in the Enterprise Spring 2005
J2EE Lecture 1:Servlet and JSP
Java Chapter 7 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Michael Brockway Application Integration Servlets l Introduction & Overview l HTTP Servlets l HTTP get Requests l HTTP post Requests l Multi-tier Applications Using JDBC from a Servlet l References

2 Introduction l References: Y D Liang, Intro to Java Programming, 7ed chapter 39 l Java’s support for distributed / internet- / web-based applications includes n Multithreading n Networking u java.net package u Socket-based communications, packet-based communications n Remote Method Calling u RMI (java.rmi package) u CORBA n Servlets u A client requests an action be performed; a server responds to client requests u An applet is a class that is downloaded to the client and performs functions there; u A servlet lives in the server and responds to requests from the client. u Basic packages are javax.servlet, javax.servlet.http t javax.servlet.jsp and javax.servlet.jsp.tagext comprise Java Server Pages (JSP), extending servlet capabilities to enable servers to deliver to the client dynamically created XHTML pages with embedded Java functionality.

3 Introduction n Servlets (ctd) u A distributed application often has a 3-tier architecture with a web server providing secure access to a “back end”, commonly a database system. u Servlets may be used to support thin clients with minimal client-side software. u The client sends one of a small number of requests to the server; servlets residing in the server respond u Clients connect to the server using standard protocols t HTTP forms t “get”, “post” requests  See for information on URLs, and  l This chapter shows how servlets manage client/server communication using the HTTP protocol n Client sends an HTTP request n Servlet processes it and returns a response to client Web Server Back End Client eg database business logic

4 Servlet Architecture l A servlet is a class which implements the javax.servlet.Servlet interface void init(ServletConfig cfg) u parameter supplied by server containing the servletclass ServletConfig get ServletConfig() u returns reference to implementing object String get ServletInfo() n void service(ServletRequest rq, ServletResponse rp) u Containing server calls this in response to a client request to the servlet. n void destroy() l The packages provide two abstract classes implementing this n javax.servlet.GenericServlet n javax.servlet.http.HttpServlet n Most servlets extend one of these. We shall concentrate on the HttpServlet from now on. Class HttpServlet n Has empty methods, at leat one of which should be over-ridden:  doGet(HttpServletRequest rq, HttpServletResponse rp ) and  doPost( HttpServletRequest rq, HttpServletResponse rp ) n Overrides service() with a version which calls one of these, depending on request type; An HttpServlet needs to define specific overriding doGet() and/or doPost() methods

5 Servlet Architecture l Interface HttpServletRequest String getParameter(String param)  cf applet parameter Enumeration getParameterNames() u names of all parameters set as part of a post request String[] getParameterValues(String paramName) u a parameter can have multiple values Cookie[] getCookies() u cookies are stored on the client by the server (see later) HttpSession getSession (boolean create) u returns an HttpSession associated with current browsing session (see later) u if create = true, creates one if one does not already exist l Interface HttpServletResponse void addCookie(Cookie cookie) u added to header of response to client. Stored in client if client has enabled cookies. ServletOutputStream getOutputStream() n PrintWriter getWriter() u byte-based (binary) and character-based output to client, respecively u cookies are stored on the client by the server (see later) void setContentType(String type)  MIME type of output to client -- “text/html” for HTML content l Every call to doGet(...), doPost(...) gets from containing server objects implementing these.

6 Handling get Requests WelcomeServlet Example n WelcomeServlet.java + WelcomeServlet.html (See listings at back) n Note the tag in the XHTML form in WelcomeServlet.html: u directs a request from this form to the server, to the WelcomeServlet; u Establishes it as a get request n The tag displays a button labelled “Get HTML Document” u When the client loads this XHTML and clicks the button the get request is sent to the server n The WelcomeServlet, once correctly deployed in the server, responds by  Setting the response content type to “text/html”  Getting the output PrintWriter object for the response -- called out  Using out.println(...) to output lines of HTML to the client. n Practicalities  javax.servlet and javax.servlet.http are J2EE packages: J2EE needs to be installed in the development environment and an appropriate CLASSPATH set.

7 Handling get Requests l Deploying the WelcomeServlet in Tomcat Server n to serve the get request from this XHTML form Look at Tomcat’s webapps directory In here make a directory servletexs u the context root of this Web application n In this directory install the following subdirectories and files (files in italics): servletexs WelcomeServlet.html WEB-INF web.xml classes WelcomeServlet.class web.xml is the deployment descriptor also listed at the back of these slides n Ties the servlet to incoming get requests from client forms: see Servlet Mappings section n Client runs the servlet by first loading the form generating the get request:

8 Handling get Requests Containing Data l WelcomeServlet2 n WelcomeServlet2.java + WelcomeServlet2.html ( See listings at back) n Note the tag in a XHTML form in WelcomeServlet2.html as before: n The tags within the form display a text box and a “Submit” button. u When the client clicks it the get request is sent to the server u with parameter named “firstname” with value = contents of text box WelcomeServlet2 can use request.getParameter( “firstname”) to obtain the data entered by the client in the form. l Deploying: n Install WelcomeServlet2.java, WelcomeServlet2.html in Tomcat in advjhtp1/... n Edit web.xml to include and elements for this servlet n Restart Tomcat l Client

9 Handling post Requests l WelcomeServlet3 n WelcomeServlet3.java + WelcomeServlet3.html ( See listings at back) n Note the tag in a XHTML form in WelcomeServlet3.html: n The tags within the form display a text box and a “Submit” button. u When the client clicks it the post request is sent to the server u with parameter named “firstname” with value = contents of text box WelcomeServlet3 uses request.getParameter( “firstname”) to obtain the data entered by the client in the form. l Deploying: n Install WelcomeServlet3.java, WelcomeServlet3.html in Tomcat in advjhtp1/... n Edit web.xml to include and elements for this servlet n Restart Tomcat l Client

10 Multi-Tier Applications -- Using JDBC from a Servlet l SurveyServlet.java, Survey.html n Functionality u Collects a vote from each client “What is your favourite pet?” u Stores votes in database u Returns to client a report of survey statistics n Thin client: just a web browser n Middle tier: the SurveyServlet n Back end: a Cloudscape database connected to middle tier by JDBC l ServeyServlet n init(...) u Initially sets up a JDBC Connection to the database, and SQL PreparedStatement objects to update the database and extract statistics from it Web Server Back End Client db access middle tier JDBC XHTML

11 Multi-Tier Applications -- Using JDBC from a Servlet l ServeyServlet n doPost(...) u Gets vote from client  request.getParameter(“animal”) u Uses a Statement to update the database u Uses Statement objects to get get statistics u Sends XHTML response to client l Survey.html n Form with post method, containing n Radio buttons with name = “animal”for the client to vote n A Submit button l Deploy in the usual way Install class, html file, and add animalsurvey elements to web.xml; restart Tomcat. u servlet-name = animalsurvey, url-pattern = /animalsurvey, etc Also copy ojdbc14.jar (containing the Oracle JDBC driver) to Tomcat’s...\servletexs\WEB-INF\lib subdirectory.

12 Session Tracking -- Cookies l A way for a server to keep persistent information about a particular client n preferences n contents of an e-shopping cart n etc l Text based data l If cookies enabled in client’s browser, n Server can send cookies in header of an XHTML page n Client browser stores them in client machine n Sent back to server with next get or post from client n Cookies expire after a finite amount of time l See Liang n CookieServlet.java, CookieSelectLanguage.html

13 Session Tracking -- HttpSession Interface l Another way of supporting a server to maintain information about client preferences n See, eg Liang n SessionServlet.java, SessionSelectLanguage.html n The Request object provides an object implementing HttpSession n Same functionality as Cookie example l Unlike Cookies, HttpSession objects are not normally saved between browser sessions but just “live” for the duraiton of a session.

14 Further Reading l WWW Resources n The Sun java enterprise edition web site has plenty on servlets. Go to ffollow the links to API documentation on the Classes and interfaces in the javax.servlet and javax.servlet.http packages, and tutorial and background reading on servlets. Explore For reading about servlets deployed in Tomcat l The Liang textbook references cited above. l The O’Reilly book Java Servlet Programming