J2EE training: 1 TODO Replace Netscape examples with Firefox. In later two sections also.

Slides:



Advertisements
Similar presentations
Chapter 61 Processing the Client Request JavaServer Pages By Xue Bai.
Advertisements

J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
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.
1 HTTP – HyperText Transfer Protocol Part 1. 2 Common Protocols In order for two remote machines to “ understand ” each other they should –‘‘ speak the.
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
Core Servlets chapter 5: HTTP request headers. Requesting header information It is possible that information the servlet needs is not in the form data.
JSP Java Server Pages Reference:
Core Servlets Chapter 3 Link for Core Servlets code: om/archive/ om/archive/
1 Handling the Client Request: HTTP Request Headers Parts of this presentation was provided by Vijayan Sugumaran Department of DIS.
1 CS6320 – Servlet Request and Response L. Grewe.
CSC 2720 Building Web Applications Servlet – Getting and Setting HTTP Headers.
Slides © Marty Hall, book © Sun Microsystems Press 1 Handling the Client Request: HTTP Request Headers Core Servlets & JSP.
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.
A Servlet’s Job Read explicit data sent by client (form data) Read implicit data sent by client (request headers) Generate the results Send the explicit.
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.
Web technologies and programming cse hypermedia and multimedia technology Fanis Tsandilas April 3, 2007.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
27.1 Chapter 27 WWW and HTTP Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Maryam Elahi University of Calgary – CPSC 441.  HTTP stands for Hypertext Transfer Protocol.  Used to deliver virtually all files and other data (collectively.
Slides © Marty Hall, book © Sun Microsystems Press 1 Including Files & Applets in JSP Documents Core Servlets & JSP book:
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
SKT-SSU IT Training Center Servlet and JSP. Chapter Three: Servlet Basics.
COMP 321 Week 7. Overview HTML and HTTP Basics Dynamic Web Content ServletsMVC Tomcat in Eclipse Demonstration Lab 7-1 Introduction.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
Chapter 5 HTTP Request Headers. Content 1.Request headers 2.Reading Request Headers 3.Making a Table of All Request Headers 4.Sending Compressed Web Pages.
Slides © Marty Hall, book © Sun Microsystems Press 1 Generating the HTTP Response Core Servlets & JSP book:
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.
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.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
1 Servlets Modified slides from Dr.Sagiv. 2 Introduction.
SKT-SSU IT Training Center Servlet and JSP. Chapter Five: Handling the Client Request: HTTP Request Headers.
CIS679: Lecture 13 r Review of Last Lecture r More on HTTP.
Saving Client State Session Tracking: Maintain state about series of requests from same client over time Using Cookies: Clients hold small amount of their.
1 Handling the Client Request: HTTP Request Headers.
20-Nov-15introServlets.ppt Intro to servlets. 20-Nov-15introServlets.ppt typical web page – source Hello Hello.
S ERVLETS Cookies Handling 5-Dec-15. S ERVLETS - C OOKIES H ANDLING Cookies are text files stored on the client computer and they are kept for various.
Slides © Marty Hall, book © Sun Microsystems Press 1 Handling Cookies Core Servlets & JSP book: More.
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.
27.1 Chapter 27 WWW and HTTP Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
27.1 Chapter 27 WWW and HTTP Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Slides © Marty Hall, book © Sun Microsystems Press 1 Session Tracking Core Servlets & JSP book: More.
SKT-SSU IT Training Center Servlet and JSP. Chapter Seven: Generating the Server Response: HTTP Response Headers.
® IBM Software Group © 2007 IBM Corporation Servlet API (Part II)
Representation and Management of Data on the Web
Servlets 4 Lec 30 Web Design and Development. Looking Back… Response Redirection  Sending a standard redirect  Sending a redirect to an error page Request.
Java Servlets and Java Server Pages
Overview of Servlets and JSP
HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,
8-Mar-16 More About Servlets Session Tracking. Persistent information A server site typically needs to maintain two kinds of persistent (remembered) information:
27.1 Chapter 27 WWW and HTTP Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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.
Speaker Name Speaker Title Speaker Title Oracle Corporation Olivier Le Diouris Principal Product Manager Oracle Corporation Building Servlet and JSP Applications.
Chapter 27 WWW and HTTP Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
HTTP request message two types of HTTP messages: request, response
Servlet.
Generating the Server Response: HTTP Status Codes
Servlets and Java Server Pages
CS320 Web and Internet Programming Cookies and Session Tracking
CS3220 Web and Internet Programming Cookies and Session Tracking
CS3220 Web and Internet Programming Cookies and Session Tracking
Presentation transcript:

J2EE training: 1 TODO Replace Netscape examples with Firefox. In later two sections also.

J2EE training: 2 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting institutions –Slides not permitted for use in commercial training courses except when taught by coreservlets.com (see Slides can be modified however instructor desires Instructor can give PDF or hardcopy to students, but should protect the PowerPoint files –This slide is suppressed in Slide Show mode

JSP, Servlet, Struts, JSF & Java Training: J2EE Books from Sun Press: © 2005 Marty Hall 3 Handling the Client Request: HTTP Request Headers

J2EE training: 4 Agenda Reading HTTP request headers Building a table of all the request headers Understanding the various request headers Reducing download times by compressing pages Differentiating among types of browsers

J2EE training: 5 A Typical HTTP Request GET /servlet/Search?keywords=servlets+jsp HTTP/1.1 Accept: image/gif, image/jpg, */* Accept-Encoding: gzip Connection: Keep-Alive Cookie: userID=id Host: Referer: User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0) It shouldn't take a rocket scientist to realize that you need to understand HTTP to be effective with servlets and JSP

J2EE training: 6 Reading Request Headers (Methods in HttpServletRequest) General –getHeader (header name is not case sensitive) –getHeaders –getHeaderNames Specialized –getCookies –getAuthType and getRemoteUser –getContentLength –getContentType –getDateHeader –getIntHeader Related info –getMethod, getRequestURI, getQueryString, getProtocol

J2EE training: 7 Checking For Missing Headers HTTP 1.0 –All request headers are optional HTTP 1.1 –Only Host is required Conclusion –Always check that request.getHeader is non-null before trying to use it String val = request.getHeader("Some-Name"); if (val != null) { … }

J2EE training: 8 Making a Table of All Request Headers public class ShowRequestHeaders extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { out.println (docType + " \n" + " "+title+" \n"+ " \n" + " " + title + " \n" + " Request Method: " + request.getMethod() + " \n" + " Request URI: " + request.getRequestURI() + " \n" + " Request Protocol: " + request.getProtocol() + " \n" +

J2EE training: 9 Making a Table of All Request Headers (Continued) " \n" + " Header Name Header Value"); Enumeration headerNames = request.getHeaderNames(); while(headerNames.hasMoreElements()) { String headerName = (String)headerNames.nextElement(); out.println(" " + headerName); out.println(" "+request.getHeader(headerName)); } out.println(" \n "); } /** Since this servlet is for debugging, have it * handle GET and POST identically. */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }

J2EE training: 10 Making a Table of All Request Headers (Result 1)

J2EE training: 11 Making a Table of All Request Headers (Result 2)

J2EE training: 12 Common HTTP 1.1 Request Headers Accept –Indicates MIME types browser can handle –Can send different content to different clients. For example, PNG files have good compression characteristics but are not widely supported in browsers. A servlet could check to see if PNG is supported, sending if it is supported, and if not. –Warning: IE incorrectly sets this header when you hit the Refresh button. It sets it correctly on original request. Accept-Encoding –Indicates encodings (e.g., gzip or compress) browser can handle. –See following example

J2EE training: 13 Common HTTP 1.1 Request Headers (Continued) Authorization –User identification for password-protected pages. –See upcoming example. –Instead of HTTP authorization, use HTML forms to send username/password and store info in session object. This approach is usually preferable because standard HTTP authorization results in a small, terse dialog box that is unfamiliar to many users. –Servers have high-level way to set up password-protected pages without explicit programming in the servlets. For details, see Chapter 7 (Declarative Security) and Chapter 8 (Programmatic Security) of More Servlets and JavaServer Pages,

J2EE training: 14 Common HTTP 1.1 Request Headers (Continued) Connection –In HTTP 1.0, keep-alive means browser can handle persistent connection. In HTTP 1.1, persistent connection is default. Persistent connections mean that the server can reuse the same socket over again for requests very close together from the same client (e.g., the images associated with a page, or cells within a framed page). –Servlets can't do this unilaterally; the best they can do is to give the server enough info to permit persistent connections. So, they should set Content-Length with setContentLength (using ByteArrayOutputStream to determine length of output). Cookie –Gives cookies previously sent to client. Use getCookies, not getHeader. See chapter & later class session.

J2EE training: 15 Common HTTP 1.1 Request Headers (Continued) Host –Indicates host given in original URL –This is a required header in HTTP 1.1. This fact is important to know if you write a custom HTTP client (e.g., WebClient used in book) or telnet to a server and use the HTTP/1.1 version. If-Modified-Since –Indicates client wants page only if it has been changed after specified date –Don’t handle this situation directly; implement getLastModified instead. –See lottery-number example in book (Core Servlets & JSP (2nd Ed) Chapter 3).

J2EE training: 16 Common HTTP 1.1 Request Headers (Continued) Referer –URL of referring Web page –Useful for tracking traffic; logged by many servers –Can also be used to let users set preferences and then return to the page they came from –Can be easily spoofed; don't let this header be sole means of deciding how much to pay sites that show your banner ads. –Some browsers (Opera), ad filters (Web Washer), and personal firewalls (Norton) screen out this header –See example in book User-Agent –Best used for identifying category of client Web browser vs. I-mode cell phone, etc. –For Web applications, use other headers if possible –Again, can be easily spoofed –See following example

J2EE training: 17 Sending Compressed Web Pages Dilbert used with permission of United Syndicates Inc.

J2EE training: 18 Sending Compressed Pages: GzipUtilities.java public class GzipUtilities { public static boolean isGzipSupported (HttpServletRequest request) { String encodings = request.getHeader("Accept-Encoding"); return((encodings != null) && (encodings.indexOf("gzip") != -1)); } public static boolean isGzipDisabled (HttpServletRequest request) { String flag = request.getParameter("disableGzip"); return((flag != null)&& (!flag.equalsIgnoreCase("false"))); } public static PrintWriter getGzipWriter (HttpServletResponse response) throws IOException { return(new PrintWriter (new GZIPOutputStream (response.getOutputStream()))); }

J2EE training: 19 Sending Compressed Pages: LongServlet.java public class LongServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); // Change the definition of "out" depending on // whether or not gzip is supported. PrintWriter out; if (GzipUtilities.isGzipSupported(request) && !GzipUtilities.isGzipDisabled(request)) { out = GzipUtilities.getGzipWriter(response); response.setHeader("Content-Encoding", "gzip"); } else { out = response.getWriter(); }

J2EE training: 20 Sending Compressed Pages: LongServlet.java (Continued) … out.println (docType + " \n" + " " + title + " \n" + " \n" + " " + title + " \n"); String line = "Blah, blah, blah, blah, blah. " + "Yadda, yadda, yadda, yadda."; for(int i=0; i<10000; i++) { out.println(line); } out.println(" "); out.close(); }

J2EE training: 21 Sending Compressed Pages: Results Uncompressed (28.8K modem), Firefox, Netscape and Internet Explorer: > 50 seconds Compressed (28.8K modem), Firefox, Netscape and Internet Explorer: < 5 seconds Caution: be careful about generalizing benchmarks

J2EE training: 22 Differentiating Among Different Browser Types Use User-Agent only when necessary. –Otherwise, you will have difficult-to-maintain code that consists of tables of browser versions and associated capabilities. Check for null. –The header is not required by the HTTP 1.1 specification, some browsers let you disable it (e.g., Opera), and custom clients (e.g., Web spiders or link verifiers) might not use the header at all. To differentiate among Firefox, Netscape, and Internet Explorer, check for “MSIE,” not “Mozilla.” –Both Firefox and Internet Explorer say “Mozilla” at the beginning of the header. For JavaScript compatability. Note that the header can be faked. –If a client fakes this header, the servlet cannot tell the difference.

J2EE training: 23 Differentiating Among Different Browser Types (Code) public class BrowserInsult extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title, message; // Assume for simplicity that Netscape and IE are // the only two browsers. String userAgent = request.getHeader("User-Agent"); if ((userAgent != null) && (userAgent.indexOf("MSIE") != -1)) { title = "Microsoft Minion"; message = "Welcome, O spineless slave to the " + "mighty empire."; } else { title = "Hopeless Netscape Rebel"; message = "Enjoy it while you can. " + "You will be assimilated!"; }

J2EE training: 24 Differentiating Among Browser Types (Result)

J2EE training: 25 Summary Many servlet tasks can only be accomplished by making use of HTTP headers coming from the browser Use request.getHeader for arbitrary header –Remember to check for null Cookies, authorization info, content length, and content type have shortcut methods Most important headers you read directly –Accept –Accept-Encoding –Connection –Referer –User-Agent

JSP, Servlet, Struts, JSF & Java Training: J2EE Books from Sun Press: © 2005 Marty Hall 26 Questions?