Chapter 11 © 2001 by Addison Wesley Longman, Inc. 1 Chapter 11 Sebesta: Programming the World Wide Web.

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.
JSP and Servelets.
CGI programming. Common Gateway Interface interface between web server and other programs (cgi scripts) information passed as environment variables passed.
Cookie in a servlet. Cookies are small bits of textual information that a Web server sends to a browser and that the browser returns unchanged when visiting.
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.
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.
An introduction to Java Servlet Programming
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 34 Servlets.
CP476 Internet Computing Browser and Web Server 1 Web Browsers A client software program that allows you to access and view Web pages on the Internet –Examples.
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.
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.
Chapter 10 Servlets and Java Server Pages. A servlet is a Java class designed to be run in the context of a special servlet container An instance of the.
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.
Linux Operations and Administration
Java Server Pages B.Ramamurthy. Topics for Discussion 8/20/20152 Inheritance and Polymorphism Develop an example for inheritance and polymorphism JSP.
1 Servlet How can a HTML page, displayed using a browser, cause a program on a server to be executed?
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
Chapter 11 © 2005 by Addison Wesley Longman, Inc Overview of Servlets - A servlet is a compiled Java class - Servlets are executed on the server.
Java Servelets. What Is a Servlet? A servlet is a Java programming language class used to extend the capabilities of servers that host applications accessed.
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.
Chapter 11 © 2010 by Addison Wesley Longman, Inc Introduction to Servlets - A servlet is a Java object that responds to HTTP requests and is executed.
Chapter 11 © 2009 by Addison Wesley Longman, Inc Introduction to Servlets - A servlet is a compiled Java class - Servlets are executed on the server.
Java support for WWW Babak Esfandiari (sources: Qusay Mahmoud, Roger Impey, textbook)
CSC 2720 Building Web Applications
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.
16-Oct-15 JSP Implicit Objects. 2 JSP Implicit Objects are the Java objects that the JSP Container makes available to developers in each page and developer.
Java Servlets & Java Server Pages Lecture July 2013.
Chapter 6 Server-side Programming: Java Servlets
1 CS122B: Projects in Databases and Web Applications Spring 2015 Notes 03: Web-App Architectures Professor Chen Li Department of Computer Science CS122B.
Chapter 11 © 2003 by Addison Wesley Longman, Inc. 1 Chapter 11 Introduction to Web Servers and Servlets.
LinuxChix Apache. Serving Webpages The layer 7 protocol (HTTP) is what our browsers talk to get us the websites we can't seem to live without. HTTP is.
Introduction to Server-Side Web Development Introduction to Server-Side Web Development Session II: Introduction to Server-Side Web Development with 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.
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.
HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,
©SoftMooreSlide 1 Cookies. ©SoftMooreSlide 2 Cookies Basic idea –web application sends a simple name/value pair to the client –when the client connects.
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.
Java Servlets References: Karen Anewalt, Mary Washington College.
11.1 Web Server Operation - Client-server systems
CS122B: Projects in Databases and Web Applications Spring 2017
Introduction to Servlets
CS122B: Projects in Databases and Web Applications Winter 2017
Servlets.
WWW and HTTP King Fahd University of Petroleum & Minerals
Servlet Fudamentals.
Java Servlets By: Tejashri Udavant..
Chapter 26 Servlets.
CS122B: Projects in Databases and Web Applications Winter 2018
Introduction to 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.
COP 4610L: Applications in the Enterprise Spring 2005
CS122B: Projects in Databases and Web Applications Winter 2019
Presentation transcript:

Chapter 11 © 2001 by Addison Wesley Longman, Inc. 1 Chapter 11 Sebesta: Programming the World Wide Web

Chapter 11 © 2001 by Addison Wesley Longman, Inc Web Server Operation - Client-server systems - When two computers are connected, either could be the client - The client initiates the communication, which the server accepts - Generally, clients are human consumers of information, while servers are machine suppliers - Client/server systems have an efficient division of work - All communications between Web clients and servers use HTTP - When a Web server starts, it tell its OS it is ready to accept communications through a specific port, usually 80 - All current Web servers are descendents of the first two (CERN and NCSA) - Most servers are Apache running under UNIX

Chapter 11 © 2001 by Addison Wesley Longman, Inc General Server Characteristics - Web servers have two separate directories - The document root is the root directory of all servable documents (well, not really all) e.g. Suppose the site name is and the document root is named topdocs, and it is stored in the /admin/web directory So, /admin/web/topdocs is the document directory address If a request URL is the server will search for the file named /admin/web/topdocs/bulbs/tulips.html - The server can have virtual document trees - Sometimes a different disk, possibly on a different machine, is used after the original disk is filled

Chapter 11 © 2001 by Addison Wesley Longman, Inc General Server Characteristics (continued) - The server root is the root directory for all of the code that implements the server - The server root usually has four files - One is the code for the server itself - Three others are subdirectories - conf - for configuration information - logs - to store what has happened - cgi-bin - for executable scripts - Contemporary servers provide many services: - Virtual hosts - multiple sites on the same system - Proxy servers - to serve documents from the document roots of other sites - Besides HTTP, support for FTP, Gopher, News, - Support for database access

Chapter 11 © 2001 by Addison Wesley Longman, Inc Apache under UNIX - Apache is available for other platforms, but it is most commonly used under UNIX - Apache is now a large and complex system - The configuration file is named httpd.conf - The directives in the configuration file control the operation of the server - Configuration file format: - Comments begin with a # - Blank lines are ignored - Non-blank lines that do not begin with # must begin with a directive name, which may take parameters, separated by white space - When Apache begins, it reads the configuration files and sets its parameters according to what it reads

Chapter 11 © 2001 by Addison Wesley Longman, Inc Apache under UNIX (continued) - Changes to configuration files affect Apache only if it is forced to reset - Use the following UNIX commands to force Apache to reset: cd /usr/local/etc/httpd/logs kill -HUP `cat httpd.pid` - This creates a hangup signal - It works because Apache write its process id (pid) into httpd.pid when it starts - Directives - ServerName - default is what is returned by the hostname command, but it may be only the first part ServerName

Chapter 11 © 2001 by Addison Wesley Longman, Inc Apache under UNIX (continued) - ServerRoot - to set the server root address - Default is /usr/local/etc/httpd - If it is stored elsewhere, tell Apache with: ServerRoot /usr/local/httpd - ServerAdmin - address of the site admin ServerAdmin - DocumentRoot - set the document root address - Default is /usr/local/etc/httpd/htdocs - If it is elsewhere, tell Apache with: DocumentRoot /local/webdocs

Chapter 11 © 2001 by Addison Wesley Longman, Inc Apache under UNIX (continued) - Alias - to specify a virtual document tree - Takes two parameters, virtual path for URLs and the actual path - Example: Alias /bushes /usr/local/plants/bushes - Now, will be mapped to /usr/local/plants/bushes/roses.html - ScriptAlias - to create a secure place for CGI scripts - Creates a virtual directory ScriptAlias /cgi-bin/ /usr/local/etc/httpd/cgi-bin/

Chapter 11 © 2001 by Addison Wesley Longman, Inc Apache under UNIX (continued) - Redirect - to redirect requests to another system e.g., To move the bushes directory to Redirect /bushes - DirectoryIndex - URL-specified directories - When a request includes a URL that ends with a slash, Apache searches for a document to return, called the welcome page - Default welcome page is index.html - If there is no index.html, Apache may try to build a directory listing for the home directory (unless automatic directory listings are turned off) - To avoid this, provide more than one welcome page names DirectoryIndex index.html contents.html

Chapter 11 © 2001 by Addison Wesley Longman, Inc Apache under UNIX (continued) - UserDir - to specify whether local users can or cannot add or delete documents; default is: UserDir public_html - Now, if user bob stores stuff.html in his public_html directory, the URL will work - To make a subdirectory of public_html available, include it in the parameter UserDir public_html/special_stuff - To disallow additions and deletions: UserDir disabled - Logs - Access logs record all accesses (time, date, HTTP command, URL, status, etc.) - Error logs have the form: [date/time] The error message

Chapter 11 © 2001 by Addison Wesley Longman, Inc Overview of Servlets - A servlet is a compiled Java class - Servlets are executed on the server system under the control of the Web server - Servlets are managed by the servlet container, or servlet engine - Servlets are called through HTML - Servlets receive requests and return responses, both of which are supported by the HTTP protocol - When the Web server receives a request that is for a servlet, the request is passed to the servlet container - The container makes sure the servlet is loaded and calls it - The servlet call has two parameter objects, one with the request and one for the response - When the servlet is finished, the container reinitializes itself and returns control to the Web server

Chapter 11 © 2001 by Addison Wesley Longman, Inc Overview of Servlets (continued) - Servlets are used 1) as alternatives to CGI, and 2) as alternatives to Apache modules - Servlet Advantages: - Can be faster than CGI, because they are run in the server process - Have direct access to Java APIs - Because they continue to run (unlike CGI programs), they can save state information - Have the usual benefits of being written in Java (platform independence, ease of programming) 11.5 Servlet Details - All servlets are classes that either implement the Servlet interface or extend a class that implements the Servlet interface - The Servlet interface provides the interfaces for the methods that manage servlets and their interactions with clients

Chapter 11 © 2001 by Addison Wesley Longman, Inc Servlet Details (continued) - The Servlet interface declares three methods that are called by the servlet container (the life-cycle methods) - init - initializes the servlet and prepares it to respond to client requests - service - controls how the servlet responds to requests - destroy - takes the servlet out of service - The Servlet interface declares two methods that are used by the servlet: - getServletConfig - to get initialization and startup parameters for itself - getServletInfo - to allow the servlet to return info about itself (author, version #, etc.) to clients - Most user-written servlet classes are extensions to HttpServlet (which is an extension of GenericServlet, which implements the Servlet Interface)

Chapter 11 © 2001 by Addison Wesley Longman, Inc Servlet Details (continued) - Two other necessary interfaces: - ServletResponse – to encapsulate the communications, server to client - ServletRequest – to encapsulate the communications, client to server - Provides servlet access to ServletOutputStream - HttpServlet – an abstract class - Extends GenericServlet - Implements java.io.Serializable - Every subclass of HttpServlet MUST override at least one of the methods of HttpServlet doGet * doPost * doPut * doDelete * init destroy getServletInfo * Called by the server

Chapter 11 © 2001 by Addison Wesley Longman, Inc Servlet Details (continued) - The protocol of doGet is: protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException - ServletException is thrown if the GET request could not be handled - The protocol of doPost is the similar - Servlet output – HTML 1. Use the setContentType method of the response object to set the content type to text/html response.setContentType("text/html"); 2. Create a PrintWriter object with the getWriter method of the response object PrintWriter servletOut = response.getWriter(); - Example – Respond to a GET request with no data  Show ch11_1.html and Greeting.java

Chapter 11 © 2001 by Addison Wesley Longman, Inc A Survey Example --> Show ch11_2.html (consumer electronics survey form) and Figure The servlet: - To accumulate voting totals, it must write a file on the server - The file will be read and written as an object (the array of vote totals) using ObjectInputStream - An object of this class is created with its constructor, passing an object of class FileInputStream, whose constructor is called with the file variable name as a parameter ObjectInputStream indat = new ObjectInputStream( new FileInputStream( File_variable_name )); - On input, the contents of the file will be cast to integer array - For output, the file is written as a single object

Chapter 11 © 2001 by Addison Wesley Longman, Inc A Survey Example (continued) - The servlet must access the form data from the client - This is done with the getParameter method of the request object, passing a literal string with the name of the form element e.g., if the form has an element named zip zip = request.getParameter("zip"); - If an element has no value and its value is requested by getParameter, the returned value is null - If a form value is not a string, the returned string must be parsed to get the value - e.g., suppose the value is an integer literal - A string that contains an integer literal can be converted to an integer with the parseInt method of the wrapper class for int, Integer price = Integer.parseInt( request.getParameter("price"));

Chapter 11 © 2001 by Addison Wesley Longman, Inc A Survey Example (continued) - The file structure is an array of 14 integers, 7 votes for females and 7 votes for males - Servlet actions: If the votes data array exists read the votes array from the data file else create the votes array Get the gender form value Get the form value for the new vote and convert it to an integer Add the vote to the votes array Write the votes array to the votes file Produce the return HTML document that shows the current results of the survey - Every voter will get the current totals --> Show the servlet, Survey.java --> Show the results of running Survey

Chapter 11 © 2001 by Addison Wesley Longman, Inc Storing Information about Clients - A session is the collection of all of the requests made by a particular browser from the time the browser is started until the user exits the browser - The HTTP protocol is stateless - But, there are several reasons why it is useful for the server to relate a request to a session - Shopping carts for many different simultaneous customers - Customer profiling for advertising - Customized interfaces for specific clients - Approaches to storing client information: - Store it on the server – too much to store! - Store it on the client machine - this works - Cookies - A cookie is an object sent by the server to the client

Chapter 11 © 2001 by Addison Wesley Longman, Inc Storing Information about Clients (continued) - Every HTTP communication between the browser and the server includes information in its header about the message - At the time a cookie is created, it is given a lifetime - Every time the browser sends a request to the server that created the cookie, while the cookie is still alive, the cookie is included - A browser can be set to reject all cookies - A cookie object has data members and methods - Data members to store lifetime, name, and a value (the cookies’ value) - Methods: setComment, setMaxAge, setValue, getMaxAge, getName, and getValue - Cookies are created with the Cookie constructor Cookie newCookie = new Cookie(gender, vote);

Chapter 11 © 2001 by Addison Wesley Longman, Inc Storing Information about Clients (continued) - By default, a cookie’s lifetime is the current session - If you want it to be longer, use setMaxAge - A cookie is attached to the response with addCookie - Order in which the response must be built: 1. Add cookies 2. Set content type 3. Get response output stream 4. Place info in the response - The browser does nothing with cookies, other than storing them and passing them back - A servlet gets a cookie from the browser with the getCookies method Cookie the Cookies []; … theCookies = request.getCookies(); - A Vote Counting Example  Show ch11_3.html

Chapter 11 © 2001 by Addison Wesley Longman, Inc Storing Information about Clients (continued) - Vote counting servlet activities: - See if a vote was cast - Make sure the voter hasn’t voted before - Tally real votes and give the client the totals - Store votes in a file  Show the VoteCounter algorithm -> Show VoteCounter -> Show VoteCounter output (Figure 11.6) - Session Tracking - An alternative to cookies - Use the HttpSession object, which can store a list of names and values

Chapter 11 © 2001 by Addison Wesley Longman, Inc Storing Information about Clients (continued) - Create a Session object - Put value in the session object with putValue mySession.putValue("iVoted", "true"); - A session can be killed with the invalidate method - A value can be removed with removeValue - A value can be gotten with getValue( name ) - All names of values can be gotten with getValueNames