J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10 Servlets and Java Server Pages.
Advertisements

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.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
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.
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.
HTTP – HyperText Transfer Protocol
CSE 190: Internet E-Commerce Lecture 7. HTML Templates Designed to separate server side logic from HTML presentation Key features –Escapes from HTML into.
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/
Definitions, Definitions, Definitions Lead to Understanding.
HTTP Response Headers Vijayan Sugumaran Department of DIS Oakland University Parts of this presentation was provided by
Hypertext Transport Protocol CS Dick Steflik.
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.
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.
Chapter 9 Using Perl for CGI Programming. Computation is required to support sophisticated web applications Computation can be done by the server or the.
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)
J2EE training: 1 TODO Replace Netscape examples with Firefox. In later two sections also.
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
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.
Generating the Server Response: HTTP Response Headers.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
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.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
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:
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.
Java Servlets. Servlets When we run small Java programs within a browser these are referred to as Applets... And when we run small Java programs within.
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 6 Server-side Programming: Java Servlets
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.
Slides © Marty Hall, book © Sun Microsystems Press 1 Handling Cookies Core Servlets & JSP book: More.
Appendix E: Overview of HTTP ©SoftMoore ConsultingSlide 1.
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.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, Responds oriented other.
Introduction to Servlets. Introduction Servlet is a language to develop the server side applications, and it is also server side component. It can develop.
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)
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
HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,
Network Programming: Servers. Agenda l Steps for creating a server Create a ServerSocket object Create a Socket object from ServerSocket Create an input.
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.
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
Chapter 26 Servlets.
Generating the Server Response: HTTP Status Codes
Servlets and Java Server Pages
Presentation transcript:

J2EE training: 1 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 2 Generating the Server Response: HTTP Response Headers

J2EE training: 3 Agenda Format of the HTTP response Setting response headers Understanding what response headers are good for Building Excel spread sheets Generating JPEG images dynamically Sending incremental updates to the browser

J2EE training: 4 HTTP Request/Response Request GET /servlet/SomeName HTTP/1.1 Host:... Header2: HeaderN: (Blank Line) Response HTTP/ OK Content-Type: text/html Header2: HeaderN:... (Blank Line)......

J2EE training: 5 Setting Arbitrary Response Headers response.setHeader(String headerName, String headerValue) –Sets an arbitrary header response.setDateHeader(String name, long millisecs) –Converts milliseconds since 1970 to a date string in GMT format response.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

J2EE training: 6 Setting Common Response Headers setContentType –Sets the Content-Type header. –Servlets almost always use this. –See table of common MIME types. setContentLength –Sets the Content-Length header. –Used for persistent HTTP connections. –See Connection request header. addCookie –Adds a value to the Set-Cookie header. –See separate section on cookies. sendRedirect –Sets the Location header (plus changes status code).

J2EE training: 7 Common MIME Types

J2EE training: 8 Building Excel Spreadsheets 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)"); }

J2EE training: 9 Building Excel Spreadsheets

J2EE training: 10 Common HTTP 1.1 Response Headers Cache-Control (1.1) and Pragma (1.0) –A no-cache value prevents browsers from caching page. 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-Encoding –The way document is encoded. See earlier compression example Content-Length –The number of bytes in the response. –See setContentLength on previous slide. –Use ByteArrayOutputStream to buffer document before sending it, so that you can determine size. See discussion of the Connection request header

J2EE training: 11 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 –The time document was last changed. –Don’t set this header explicitly; provide a getLastModified method instead. See lottery number example in book (Chapter 3).

J2EE training: 12 Common HTTP 1.1 Response Headers (Continued) Location –The URL to which browser should reconnect. –Use sendRedirect instead of setting this directly. Refresh –The number of seconds until browser should reload page. Can also include URL to connect to. See following example. Set-Cookie –The cookies that browser should remember. Don’t set this header directly; use addCookie instead. See next section. WWW-Authenticate –The authorization type and realm needed in Authorization header. See security chapters in More Servlets & JSP.

J2EE training: 13 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 See upcoming lecture on session tracking –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: start a Thread. 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

J2EE training: 14 Persistent Servlet State and Auto-Reloading Pages: Example Idea: generate list of large (e.g., 150-digit) prime numbers –Show partial results until completed –Let new clients make use of results from others Demonstrates use of the Refresh header. Shows how easy it is for servlets to maintain state between requests. –Very difficult in traditional CGI. Also illustrates that servlets can handle multiple simultaneous connections –Each request is in a separate thread.

J2EE training: 15 Finding Prime Numbers for Use with Public Key Cryptography public class PrimeNumberServlet extends HttpServlet { private ArrayList primeListCollection = new ArrayList(); private int maxPrimeLists = 30; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int numPrimes = ServletUtilities.getIntParameter(request, "numPrimes", 50); int numDigits = ServletUtilities.getIntParameter(request, "numDigits", 120); PrimeList primeList = findPrimeList(primeListCollection, numPrimes, numDigits);

J2EE training: 16 Finding Prime Numbers for Use with Public Key Cryptography if (primeList == null) { primeList = new PrimeList(numPrimes, numDigits, true); // Multiple servlet request threads share the instance // variables (fields) of PrimeNumbers. So // synchronize all access to servlet fields. synchronized(primeListCollection) { if (primeListCollection.size() >= maxPrimeLists) primeListCollection.remove(0); primeListCollection.add(primeList); } ArrayList currentPrimes = primeList.getPrimes(); int numCurrentPrimes = currentPrimes.size(); int numPrimesRemaining = (numPrimes - numCurrentPrimes); boolean isLastResult = (numPrimesRemaining == 0); if (!isLastResult) { response.setIntHeader("Refresh", 5); } …

J2EE training: 17 Finding Prime Numbers for Use with Public Key Cryptography

J2EE training: 18 Finding Prime Numbers for Use with Public Key Cryptography

J2EE training: 19 Using Servlets to Generate JPEG Images 1.Create a BufferedImage 2.Draw into the BufferedImage 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); }

J2EE training: 20 Using Servlets to Generate JPEG Images

J2EE training: 21 Using Servlets to Generate JPEG Images

J2EE training: 22 Summary Many servlet tasks can only be accomplished through use of HTTP response headers Setting response headers: –In general, set with response.setHeader –In special cases, set with response.setContentType, response.setContentLength, response.addCookie, and response.sendRedirect Most important response headers you set directly: –Cache-Control and Pragma –Content-Disposition –Content-Encoding –Content-Length –Expires –Refresh –WWW-Authenticate

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