Generating the Server Response: HTTP Response Headers.

Slides:



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

1 Servlets Based on Notes by Dave Hollinger & Ethan Cerami Also, the Online Java Tutorial by Sun.
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.
TCP/IP Protocol Suite 1 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 22 World Wide Web and HTTP.
J.Sant Servlets Joseph Sant Sheridan Institute of Technology.
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.
HTTP – HyperText Transfer Protocol
An introduction to Java Servlet Programming
CS320 Web and Internet Programming Generating HTTP Responses
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 34 Servlets.
JSP Java Server Pages Reference:
Core Servlets Chapter 3 Link for Core Servlets code: om/archive/ om/archive/
HTTP Response Headers Vijayan Sugumaran Department of DIS Oakland University Parts of this presentation was provided by
Java database Programming JDBC Trademarked name of a Java API that supports Java programs that access relational databases Stand for Java DataBase Connectivity.
HTTP Overview Vijayan Sugumaran School of Business Administration Oakland University.
CSC 2720 Building Web Applications Servlet – Getting and Setting HTTP Headers.
 What is it ? What is it ?  URI,URN,URL URI,URN,URL  HTTP – methods HTTP – methods  HTTP Request Packets HTTP Request Packets  HTTP Request Headers.
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.
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.
Christopher M. Pascucci Basic Structural Concepts of.NET Browser – Server Interaction.
Computer Concepts 2014 Chapter 7 The Web and .
Web technologies and programming cse hypermedia and multimedia technology Fanis Tsandilas April 3, 2007.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
1 HTML and CGI Scripting CSC8304 – Computing Environments for Bioinformatics - Lecture 10.
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
CSC 2720 Building Web Applications Getting and Setting HTTP Headers (With PHP Examples)
1 Chapter 1: Overview of Servlets and JavaSerevr Pages.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
CP476 Internet Computing Lecture 5 : HTTP, WWW and URL 1 Lecture 5. WWW, HTTP and URL Objective: to review the concepts of WWW to understand how HTTP works.
TCP/IP Protocol Suite 1 Chapter 22 Upon completion you will be able to: World Wide Web: HTTP Understand the components of a browser and a server Understand.
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.
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.
Li Tak Sing COMPS311F. Static attributes in Servlets Since Servlets are also Java classes, you can also use static attributes to store values that can.
COMP 321 Week 7. Overview HTML and HTTP Basics Dynamic Web Content ServletsMVC Tomcat in Eclipse Demonstration Lab 7-1 Introduction.
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.
Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some.
Slides © Marty Hall, book © Sun Microsystems Press 1 Generating the HTTP Response Core Servlets & JSP book:
JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA James Faeldon CS 119 Enterprise Systems Programming.
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.
Saving Client State Session Tracking: Maintain state about series of requests from same client over time Using Cookies: Clients hold small amount of their.
20-Nov-15introServlets.ppt Intro to servlets. 20-Nov-15introServlets.ppt typical web page – source Hello Hello.
Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets.
Operating Systems Lesson 12. HTTP vs HTML HTML: hypertext markup language ◦ Definitions of tags that are added to Web documents to control their appearance.
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.
Web Design and Development. World Wide Web  World Wide Web (WWW or W3), collection of globally distributed text and multimedia documents and files 
27.1 Chapter 27 WWW and HTTP Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Internet Applications (Cont’d) Basic Internet Applications – World Wide Web (WWW) Browser Architecture Static Documents Dynamic Documents Active Documents.
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)
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,
How CGI and Java Servlets are Run By David Stein 14 November 2006.
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.
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.
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
CS320 Web and Internet Programming Generating HTTP Responses
CS3220 Web and Internet Programming Generating HTTP Responses
Invoking Java Code from JSP
Chapter 26 Servlets.
Servlets and Java Server Pages
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.
Presentation transcript:

Generating the Server Response: HTTP Response Headers

HTTP response headers Response headers can be used to specify cookies, to supply the page modification date, to instruct the browser to reload the page after a designated interval, to give the file size so that persistent HTTP connections can be used, to designate the type of document being generated, and to perform many other tasks.

Setting Response Headers from Servlets The following methods can be used to set HTTP response header in your servlet program. These methods are available with HttpServletResponse object. public void setHeader(String headerName, String headerValue) – Sets a response header with the given name and value. public void setDateHeader(String name, long millisecs) – Sets a response header with the given name and date-value. – Converts milliseconds since 1970 to a date string in GMT format. public void setIntHeader(String name, int headerValue) – Prevents need to convert int to String before calling setHeader. addHeader(), addDateHeader(), addIntHeader() – Adds new occurrence of header instead of replacing.

Setting Common Response Headers setContentType (String mimeType) – Sets the Content-Type header. Servlets almost always use this. setContentLength (int length) – Sets the Content-Length header. Used for persistent HTTP connections. addCookie(Cookie c) – Adds a value to the Set-Cookie header. sendRedirect (String address) – Sets the Location header as well as setting the status code to 302.

Common MIME Types TypeMeaning application/mswordMicrosoft Word document application/octet-streamUnrecognized or binary data application/pdfAcrobat (.pdf) file application/postscriptPostScript file application/vnd.ms-excelExcel spreadsheet application/vnd.ms-powerpointPowerpoint presentation application/x-gzipGzip archive application/x-java-archiveJAR file application/x-java-vmJavabytecode (.class) file application/zipZip archive audio/basicSound file in.au or.snd format audio/x-aiffAIFF sound file audio/x-wavMicrosoft Windows sound file audio/midiMIDI sound file text/cssHTML cascading style sheet text/htmlHTML document text/plainPlain text text/xmlXML document image/gifGIF image image/jpegJPEG image image/pngPNG image image/tiffTIFF image video/mpegMPEG video clip video/quicktimeQuickTime video clip

HTTP 1.1 Response Headers Following is a summary of the most useful HTTP 1.1 response headers which go back to the browser from web server : Allow Specifies the request methods(GET,POST,etc.) that the server supports. Cache-Control This header specifies the circumstances in which the response document can safely be cached. It can have values public, private or no-cache etc. Public means document is cacheable, Private means document is for a single user and can only be stored in private (non shared) caches and no-cache means document should never be cached. Connection This header instructs the browser whether to use persistent in HTTP connections or not. A value of close instructs the browser not to use persistent HTTP connections and keep-alive means using persistent connections.

Common HTTP 1.1 Response Headers (Continued) Content-Disposition – Lets you request that the browser ask the user to save the response to disk in a file of the given name Content-Disposition: attachment; filename=file-name Content-Language This header signifies the language in which the document is written. For example en, en-us, etc. Content-Encoding – This header specifies the way in which the page was encoded during transmission. Content-Length – This header indicates the number of bytes in the response. This information is needed only if the browser is using a persistent (keep-alive) HTTP connection.

Common HTTP 1.1 Response Headers (Continued) Content-Type – The MIME type of the document being returned. – Use setContentType to set this header. Expires – The time at which document should be considered out-of-date and thus should no longer be cached. – Use setDateHeader to set this header. Last-Modified – This header indicates when the document was last changed. – Provide a getLastModified method to set this header.

Common HTTP 1.1 Response Headers (Continued ) Location – The URL to which browser should reconnect. – Use sendRedirect instead of setting this directly. Refresh This header specifies how soon the browser should ask for an updated page. You can specify time in number of seconds after which a page would be refreshed. Set-Cookie – This header specifies a cookie associated with the page. Use addCookie method to set this header. WWW-Authenticate – This header tells the browser what authorization type is needed in Authorization header.

Building Excel Spreadsheets It is sometimes useful to generate Microsoft Excel content so that users can save the results in a report and so that you can make use of the built-in formula support in Excel. Excel accepts input in at least three distinct formats: tab- separated data, HTML tables, and a native binary format. In this section, we illustrate the use of tab-separated data to generate spreadsheets. You use the shorthand setContentType method to set the Content-Type header, and the MIME type for Excel spreadsheets is application/vnd.ms-excel. So, to generate Excel spreadsheets, just do: response.setContentType("application/vnd.ms-excel"); PrintWriter out = response.getWriter(); Then, simply print some entries with tabs (\t in Java strings) in between.

public class ApplesAndOranges extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("application/vnd.ms-excel"); PrintWriter out = response.getWriter(); out.println("\tQ1\tQ2\tQ3\tQ4\tTotal"); out.println("Apples\t78\t87\t92\t29\t=SUM(B2:E2)"); out.println ("Oranges\t77\t86\t93\t30\t=SUM(B3:E3)"); }

Building Excel Spreadsheets

Requirements for Handling Long-Running Servlets A way to store data between requests. – For data that is not specific to any one client, store it in a field (instance variable) of the servlet. – For data that is specific to a user, store it in the HttpSession object – For data that needs to be available to other servlets or JSP pages (regardless of user), store it in the ServletContext A way to keep computations running after the response is sent to the user. - This task is simple: just start a Thread. The thread started by the system to answer requests automatically finishes when the response is finished, but other threads can keep running. The only subtlety: set the thread priority to a low value so that you do not slow down the server.

A way to get the updated results to the browser when they are ready. – Use Refresh header to tell browser to ask for updates

Using Servlets to Generate JPEG Images 1.Create a BufferedImage BufferedImage image = new BufferedImage(width,height, BufferedImage.TYPE_INT_RBG); 2. Draw into the BufferedImage Graphics2D g2d = (Grahics2D)image.getGrapics(); g2d.fill(someshape); g2d.draw(someshape); 3. Set the Content-Type response header response.setContentType("image/jpeg"); 4. Get an output stream OutputStream out = response.getOutputStream(); 5. Send the BufferedImage in JPEG format to the output stream try { ImageIO.write(image, "jpg", out); } catch(IOException ioe) { System.err.println("Error writing JPEG file: “ + ioe); }

Using Servlets to Generate JPEG Images