SlideShare une entreprise Scribd logo
1  sur  13
Télécharger pour lire hors ligne
© 2010 Marty Hall
Handling the Clientg
Request: HTTP
R t H dRequest HeadersOriginals of Slides and Source Code for Examples:
http://courses coreservlets com/Course-Materials/csajsp2 html
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.2
http://courses.coreservlets.com/Course-Materials/csajsp2.html
© 2010 Marty Hall
For live Java EE training, please see training courses
at http://courses.coreservlets.com/.at http://courses.coreservlets.com/.
Servlets, JSP, Struts, JSF 1.x, JSF 2.0, Ajax (with jQuery, Dojo,
Prototype, Ext-JS, Google Closure, etc.), GWT 2.0 (with GXT),
Java 5, Java 6, SOAP-based and RESTful Web Services, Spring,g
Hibernate/JPA, and customized combinations of topics.
Taught by the author of Core Servlets and JSP, More
Servlets and JSP and this tutorial Available at public
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Servlets and JSP, and this tutorial. Available at public
venues, or customized versions can be held on-site at your
organization. Contact hall@coreservlets.com for details.
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
4
A Typical HTTP Request
GET /search-servlet?keywords=servlets+jsp HTTP/1.1
A t i / if i /j */*Accept: image/gif, image/jpg, */*
Accept-Encoding: gzip
Connection: Keep-Alivep
Cookie: userID=id456578
Host: www.somebookstore.com
Referer: http://www somebookstore com/findbooks htmlReferer: http://www.somebookstore.com/findbooks.html
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– It shouldn t take a rocket scientist to realize that you need
to understand HTTP to be effective with servlets and JSP
5
Reading Request Headers
(Methods in HttpServletRequest)(Methods in HttpServletRequest)
• General
tH d (h d i t iti )– getHeader (header name is not case sensitive)
– getHeaders
– getHeaderNamesg
• Specialized
– getCookies
getAuthType and getRemoteUser– getAuthType and getRemoteUser
– getContentLength
– getContentType
– getDateHeader
– getIntHeader
• Related info• Related info
– getMethod, getRequestURI , getQueryString, getProtocol
6
Checking For Missing Headers
• HTTP 1.0
– All request headers are optional
• HTTP 1.1
O l i i d– Only Host is required
• Conclusion
Always check that request getHeader is non null before– Always check that request.getHeader is non-null before
trying to use it
String val = request.getHeader("Some-Name");
if (val != null) {
…
}
7
Making a Table of
All Request HeadersAll Request Headers
@WebServlet("/show-request-headers")
public class ShowRequestHeaders extends HttpServlet {public class ShowRequestHeaders extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {throws ServletException, IOException {
out.println
(docType +
"<HTML>n" +
"<HEAD><TITLE>"+title+"</TITLE></HEAD>n"+
"<BODY BGCOLOR="#FDF5E6">n" +
"<H1 ALIGN="CENTER">" + title + "</H1>n" +
"<B>Request Method: </B>" +
request.getMethod() + "<BR>n" +
"<B>Request URI: </B>" +
request.getRequestURI() + "<BR>n" +
"<B>Request Protocol: </B>" +
request.getProtocol() + "<BR><BR>n" +8
Making a Table of All Request
Headers (Continued)Headers (Continued)
"<TABLE BORDER=1 ALIGN="CENTER">n" +
"<TR BGCOLOR="#FFAD00">n" +
"<TH>Header Name<TH>Header Value");
Enumeration<String> headerNames =
request.getHeaderNames();
while(headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
out.println("<TR><TD>" + headerName);
out.println(" <TD>"+request.getHeader(headerName));
}
out.println("</TABLE>n</BODY></HTML>");
}
/** Since this servlet is for debugging, have itgg g
* handle GET and POST identically. */
public void doPost(HttpServletRequest request,
HttpServletResponse response)p p p )
throws ServletException, IOException {
doGet(request, response);
}
}9
Making a Table of All Request
Headers (Firefox)Headers (Firefox)
10
As of summer 2010,
Firefox was the #1
browser for visitors to
coreservlets.com,
accounting for 41.8%
of the traffic.
Making a Table of All Request
Headers (Internet Explorer)Headers (Internet Explorer)
11
As of summer 2010,
IE was the #2 browser
for visitors to
coreservlets.com,
accounting for 41.6%
of the traffic.
Making a Table of All Request
Headers (Chrome)Headers (Chrome)
12
As of summer 2010,
Chrome was the #3 browser for
visitors to coreservlets.com,
accounting for 12.7%
of the traffic. Safari was
fourth at 1.2%
Common HTTP 1.1 Request
HeadersHeaders
• Accept
– Indicates MIME types browser can handle
– Can send different content to different clients. For
example PNG files have good compressionexample, PNG files have good compression
characteristics but are not widely supported in browsers.
A servlet could check to see if PNG is supported, sending
<IMG SRC="picture png" > if it is supported and<IMG SRC picture.png ...> if it is supported, and
<IMG SRC="picture.gif" ...> if not.
– Warning: IE incorrectly sets this header when you hit the
R f h b tt It t it tl i i l tRefresh button. It sets it correctly on original request.
• Accept-Encoding
– Indicates encodings (e g gzip or compress) browser can– Indicates encodings (e.g., gzip or compress) browser can
handle.
– See following example13
Common HTTP 1.1 Request
Headers (Continued)Headers (Continued)
• Authorization
– User identification for password-protected pages.
– See upcoming example.
Instead of HTTP authorization use HTML forms to send– 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
a thori ation res lts in a small terse dialog bo that isauthorization 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, www.moreservlets.com.
14
Common HTTP 1.1 Request
Headers (Continued)Headers (Continued)
• Connection
I HTTP 1 0 k li b h dl– 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 closereuse 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– 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 tosetContentLength (using ByteArrayOutputStream to
determine length of output).
• Cookie
Gives cookies previously sent to client Use getCookies not– Gives cookies previously sent to client. Use getCookies, not
getHeader. See chapter & later class session.
15
Common HTTP 1.1 Request
Headers (Continued)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 clientimportant 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 dateafter specified date
– Don’t handle this situation directly; implement
getLastModified instead.
S l b l i b k (C S l &– See lottery-number example in book (Core Servlets &
JSP (2nd Ed) Chapter 3).
16
Common HTTP 1.1 Request
Headers (Continued)Headers (Continued)
• Referer
URL f f i W b– 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.
S b (O ) d fil (W b W h ) d l– Some browsers (Opera), ad filters (Web Washer), and personal
firewalls (Norton) screen out this header
– See example in book
U A t• User-Agent
– Best used for identifying category of client
• Regular Web browser vs. iPhone, etc.g
– For Web applications, use other headers if possible
– Again, can be easily spoofed
– See following example17
Sending Compressed
Web PagesWeb Pages
Dilbert used with permission of United Syndicates IncDilbert used with permission of United Syndicates Inc.
18
Sending Compressed Pages:
GzipUtilities javaGzipUtilities.java
public class GzipUtilities {
public static boolean isGzipSupported
(HttpServletRequest request) {
String encodings = request.getHeader("Accept-Encoding");
return((encodings != null) &&
(encodings.contains("gzip"));
}
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())));
}
}19
Sending Compressed Pages:
LongServlet javaLongServlet.java
@WebServlet("/long-servlet")
public class LongServlet extends HttpServlet {public class LongServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException IOException {throws ServletException, IOException {
response.setContentType("text/html");
// Change the definition of "out" depending on// Change the definition of out depending on
// whether or not gzip is supported.
PrintWriter out;
if (GzipUtilities.isGzipSupported(request) &&(G pUt t es. sG pSuppo ted( equest) &&
!GzipUtilities.isGzipDisabled(request)) {
out = GzipUtilities.getGzipWriter(response);
response.setHeader("Content-Encoding", "gzip");p ( g , g p );
} else {
out = response.getWriter();
}20
Sending Compressed Pages:
LongServlet java (Continued)LongServlet.java (Continued)
…
out printlnout.println
(docType +
"<HTML>n" +
"<HEAD><TITLE>" + title + "</TITLE></HEAD>n" +<HEAD><TITLE> + title + </TITLE></HEAD>n +
"<BODY BGCOLOR="#FDF5E6">n" +
"<H1 ALIGN="CENTER">" + title + "</H1>n");
String line = "Blah, blah, blah, blah, blah. " +String line Blah, blah, blah, blah, blah. +
"Yadda, yadda, yadda, yadda.";
for(int i=0; i<10000; i++) {
out.println(line);out.p t ( e);
}
out.println("</BODY></HTML>");
out.close();();
}
}
21
Sending Compressed Pages:
ResultsResults
• Uncompressed (28.8K modem),
Fi f d I E lFirefox and Internet Explorer:
> 50 seconds
Compressed (28 8K modem)• Compressed (28.8K modem),
Firefox and Internet Explorer:
< 5 seconds5 seconds
• Caution
– Be careful
about
generalizing
benchmarksbenchmarks
22
Differentiating Among
Different Browser TypesDifferent Browser Types
• Use User-Agent only when necessary.
Other ise o ill ha e diffic lt to maintain code that consists of– Otherwise, you will have difficult-to-maintain code that consists of
tables of browser versions and associated capabilities.
• Check for null.
Th h d i t i d b th HTTP 1 1 ifi ti– 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.
• Differentiating among clients• Differentiating among clients
– To differentiate among Firefox, Netscape, and Internet Explorer,
check for “MSIE,” not “Mozilla.”
• Both Firefox and Internet Explorer say “Mozilla” at the beginning of theBoth Firefox and Internet Explorer say Mozilla at the beginning of the
header. (For JavaScript compatibility.)
– Detecting Safari on the iPhone
• Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+
(KHTML lik G k ) V i /3 0 M bil /1A543 S f i/419 3(KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3
• The header can be faked.
– If a client fakes this header, the servlet cannot tell the difference.23
Differentiating Among
Different Browser Types (Code)Different Browser Types (Code)
@WebServlet("/browser-insult")
public class BrowserInsult extends HttpServlet {p p
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");response.setContentType( text/html );
PrintWriter out = response.getWriter();
String title, message;
// Assume for simplicity that Firefox and IE are
// the only two browsers// the only two browsers.
String userAgent = request.getHeader("User-Agent");
if ((userAgent != null) &&
(userAgent.contains("MSIE")) {
titl " i ft i i "title = "Microsoft Minion";
message = "Welcome, O spineless slave to the " +
"mighty empire.";
} else {
title = "Hopeless Firefox Rebel";
message = "Enjoy it while you can. " +
"You <I>will</I> be assimilated!";
}24
Differentiating Among
Browser Types (Result)Browser Types (Result)
25
Summary
• HTTP is important
Man ser let tasks can l be accomplished b making se of– 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
• Shortcuts discussed later
Cookies authorization info content length and content type– Cookies, authorization info, content length, and content type
have shortcut methods
• Most important headers you read directly
– Accept
– Accept-Encoding
– Connection– Connection
– Referer
– User-Agent26
© 2010 Marty Hall
Questions?
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.27

Contenu connexe

Similaire à 04 request-headers

Similaire à 04 request-headers (20)

Servlet classnotes
Servlet classnotesServlet classnotes
Servlet classnotes
 
REST in ( a mobile ) peace @ WHYMCA 05-21-2011
REST in ( a mobile ) peace @ WHYMCA 05-21-2011REST in ( a mobile ) peace @ WHYMCA 05-21-2011
REST in ( a mobile ) peace @ WHYMCA 05-21-2011
 
01 overview-and-setup
01 overview-and-setup01 overview-and-setup
01 overview-and-setup
 
06 response-headers
06 response-headers06 response-headers
06 response-headers
 
Ftp servlet
Ftp servletFtp servlet
Ftp servlet
 
Servlet by Rj
Servlet by RjServlet by Rj
Servlet by Rj
 
Web technology-guide
Web technology-guideWeb technology-guide
Web technology-guide
 
Servlet.ppt
Servlet.pptServlet.ppt
Servlet.ppt
 
Servlet.ppt
Servlet.pptServlet.ppt
Servlet.ppt
 
Servlet1.ppt
Servlet1.pptServlet1.ppt
Servlet1.ppt
 
Portlet
PortletPortlet
Portlet
 
HTTP
HTTPHTTP
HTTP
 
BITM3730Week12.pptx
BITM3730Week12.pptxBITM3730Week12.pptx
BITM3730Week12.pptx
 
Anintroductiontojavawebtechnology 090324184240-phpapp01
Anintroductiontojavawebtechnology 090324184240-phpapp01Anintroductiontojavawebtechnology 090324184240-phpapp01
Anintroductiontojavawebtechnology 090324184240-phpapp01
 
Marata
MarataMarata
Marata
 
Restful web services
Restful web servicesRestful web services
Restful web services
 
Web Tech Java Servlet Update1
Web Tech   Java Servlet Update1Web Tech   Java Servlet Update1
Web Tech Java Servlet Update1
 
Spatial approximate string search Doc
Spatial approximate string search DocSpatial approximate string search Doc
Spatial approximate string search Doc
 
Introduction To Web (Mukesh Patel)
Introduction To Web (Mukesh Patel)Introduction To Web (Mukesh Patel)
Introduction To Web (Mukesh Patel)
 
SOAP vs REST
SOAP vs RESTSOAP vs REST
SOAP vs REST
 

Plus de snopteck

08 session-tracking
08 session-tracking08 session-tracking
08 session-trackingsnopteck
 
07 cookies
07 cookies07 cookies
07 cookiessnopteck
 
13 java beans
13 java beans13 java beans
13 java beanssnopteck
 
11 page-directive
11 page-directive11 page-directive
11 page-directivesnopteck
 
08 session-tracking
08 session-tracking08 session-tracking
08 session-trackingsnopteck
 
05 status-codes
05 status-codes05 status-codes
05 status-codessnopteck
 
03 form-data
03 form-data03 form-data
03 form-datasnopteck
 
02 servlet-basics
02 servlet-basics02 servlet-basics
02 servlet-basicssnopteck
 
01 web-apps
01 web-apps01 web-apps
01 web-appssnopteck
 
15 expression-language
15 expression-language15 expression-language
15 expression-languagesnopteck
 

Plus de snopteck (13)

10 jdbc
10 jdbc10 jdbc
10 jdbc
 
08 session-tracking
08 session-tracking08 session-tracking
08 session-tracking
 
07 cookies
07 cookies07 cookies
07 cookies
 
14 mvc
14 mvc14 mvc
14 mvc
 
13 java beans
13 java beans13 java beans
13 java beans
 
11 page-directive
11 page-directive11 page-directive
11 page-directive
 
10 jdbc
10 jdbc10 jdbc
10 jdbc
 
08 session-tracking
08 session-tracking08 session-tracking
08 session-tracking
 
05 status-codes
05 status-codes05 status-codes
05 status-codes
 
03 form-data
03 form-data03 form-data
03 form-data
 
02 servlet-basics
02 servlet-basics02 servlet-basics
02 servlet-basics
 
01 web-apps
01 web-apps01 web-apps
01 web-apps
 
15 expression-language
15 expression-language15 expression-language
15 expression-language
 

Dernier

Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 

Dernier (20)

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 

04 request-headers

  • 1. © 2010 Marty Hall Handling the Clientg Request: HTTP R t H dRequest HeadersOriginals of Slides and Source Code for Examples: http://courses coreservlets com/Course-Materials/csajsp2 html Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.2 http://courses.coreservlets.com/Course-Materials/csajsp2.html © 2010 Marty Hall For live Java EE training, please see training courses at http://courses.coreservlets.com/.at http://courses.coreservlets.com/. Servlets, JSP, Struts, JSF 1.x, JSF 2.0, Ajax (with jQuery, Dojo, Prototype, Ext-JS, Google Closure, etc.), GWT 2.0 (with GXT), Java 5, Java 6, SOAP-based and RESTful Web Services, Spring,g Hibernate/JPA, and customized combinations of topics. Taught by the author of Core Servlets and JSP, More Servlets and JSP and this tutorial Available at public Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6. Developed and taught by well-known author and developer. At public venues or onsite at your location. Servlets and JSP, and this tutorial. Available at public venues, or customized versions can be held on-site at your organization. Contact hall@coreservlets.com for details.
  • 2. 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 4 A Typical HTTP Request GET /search-servlet?keywords=servlets+jsp HTTP/1.1 A t i / if i /j */*Accept: image/gif, image/jpg, */* Accept-Encoding: gzip Connection: Keep-Alivep Cookie: userID=id456578 Host: www.somebookstore.com Referer: http://www somebookstore com/findbooks htmlReferer: http://www.somebookstore.com/findbooks.html 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– It shouldn t take a rocket scientist to realize that you need to understand HTTP to be effective with servlets and JSP 5
  • 3. Reading Request Headers (Methods in HttpServletRequest)(Methods in HttpServletRequest) • General tH d (h d i t iti )– getHeader (header name is not case sensitive) – getHeaders – getHeaderNamesg • Specialized – getCookies getAuthType and getRemoteUser– getAuthType and getRemoteUser – getContentLength – getContentType – getDateHeader – getIntHeader • Related info• Related info – getMethod, getRequestURI , getQueryString, getProtocol 6 Checking For Missing Headers • HTTP 1.0 – All request headers are optional • HTTP 1.1 O l i i d– Only Host is required • Conclusion Always check that request getHeader is non null before– Always check that request.getHeader is non-null before trying to use it String val = request.getHeader("Some-Name"); if (val != null) { … } 7
  • 4. Making a Table of All Request HeadersAll Request Headers @WebServlet("/show-request-headers") public class ShowRequestHeaders extends HttpServlet {public class ShowRequestHeaders extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {throws ServletException, IOException { out.println (docType + "<HTML>n" + "<HEAD><TITLE>"+title+"</TITLE></HEAD>n"+ "<BODY BGCOLOR="#FDF5E6">n" + "<H1 ALIGN="CENTER">" + title + "</H1>n" + "<B>Request Method: </B>" + request.getMethod() + "<BR>n" + "<B>Request URI: </B>" + request.getRequestURI() + "<BR>n" + "<B>Request Protocol: </B>" + request.getProtocol() + "<BR><BR>n" +8 Making a Table of All Request Headers (Continued)Headers (Continued) "<TABLE BORDER=1 ALIGN="CENTER">n" + "<TR BGCOLOR="#FFAD00">n" + "<TH>Header Name<TH>Header Value"); Enumeration<String> headerNames = request.getHeaderNames(); while(headerNames.hasMoreElements()) { String headerName = headerNames.nextElement(); out.println("<TR><TD>" + headerName); out.println(" <TD>"+request.getHeader(headerName)); } out.println("</TABLE>n</BODY></HTML>"); } /** Since this servlet is for debugging, have itgg g * handle GET and POST identically. */ public void doPost(HttpServletRequest request, HttpServletResponse response)p p p ) throws ServletException, IOException { doGet(request, response); } }9
  • 5. Making a Table of All Request Headers (Firefox)Headers (Firefox) 10 As of summer 2010, Firefox was the #1 browser for visitors to coreservlets.com, accounting for 41.8% of the traffic. Making a Table of All Request Headers (Internet Explorer)Headers (Internet Explorer) 11 As of summer 2010, IE was the #2 browser for visitors to coreservlets.com, accounting for 41.6% of the traffic.
  • 6. Making a Table of All Request Headers (Chrome)Headers (Chrome) 12 As of summer 2010, Chrome was the #3 browser for visitors to coreservlets.com, accounting for 12.7% of the traffic. Safari was fourth at 1.2% Common HTTP 1.1 Request HeadersHeaders • Accept – Indicates MIME types browser can handle – Can send different content to different clients. For example PNG files have good compressionexample, PNG files have good compression characteristics but are not widely supported in browsers. A servlet could check to see if PNG is supported, sending <IMG SRC="picture png" > if it is supported and<IMG SRC picture.png ...> if it is supported, and <IMG SRC="picture.gif" ...> if not. – Warning: IE incorrectly sets this header when you hit the R f h b tt It t it tl i i l tRefresh button. It sets it correctly on original request. • Accept-Encoding – Indicates encodings (e g gzip or compress) browser can– Indicates encodings (e.g., gzip or compress) browser can handle. – See following example13
  • 7. Common HTTP 1.1 Request Headers (Continued)Headers (Continued) • Authorization – User identification for password-protected pages. – See upcoming example. Instead of HTTP authorization use HTML forms to send– 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 a thori ation res lts in a small terse dialog bo that isauthorization 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, www.moreservlets.com. 14 Common HTTP 1.1 Request Headers (Continued)Headers (Continued) • Connection I HTTP 1 0 k li b h dl– 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 closereuse 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– 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 tosetContentLength (using ByteArrayOutputStream to determine length of output). • Cookie Gives cookies previously sent to client Use getCookies not– Gives cookies previously sent to client. Use getCookies, not getHeader. See chapter & later class session. 15
  • 8. Common HTTP 1.1 Request Headers (Continued)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 clientimportant 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 dateafter specified date – Don’t handle this situation directly; implement getLastModified instead. S l b l i b k (C S l &– See lottery-number example in book (Core Servlets & JSP (2nd Ed) Chapter 3). 16 Common HTTP 1.1 Request Headers (Continued)Headers (Continued) • Referer URL f f i W b– 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. S b (O ) d fil (W b W h ) d l– Some browsers (Opera), ad filters (Web Washer), and personal firewalls (Norton) screen out this header – See example in book U A t• User-Agent – Best used for identifying category of client • Regular Web browser vs. iPhone, etc.g – For Web applications, use other headers if possible – Again, can be easily spoofed – See following example17
  • 9. Sending Compressed Web PagesWeb Pages Dilbert used with permission of United Syndicates IncDilbert used with permission of United Syndicates Inc. 18 Sending Compressed Pages: GzipUtilities javaGzipUtilities.java public class GzipUtilities { public static boolean isGzipSupported (HttpServletRequest request) { String encodings = request.getHeader("Accept-Encoding"); return((encodings != null) && (encodings.contains("gzip")); } 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()))); } }19
  • 10. Sending Compressed Pages: LongServlet javaLongServlet.java @WebServlet("/long-servlet") public class LongServlet extends HttpServlet {public class LongServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException IOException {throws ServletException, IOException { response.setContentType("text/html"); // Change the definition of "out" depending on// Change the definition of out depending on // whether or not gzip is supported. PrintWriter out; if (GzipUtilities.isGzipSupported(request) &&(G pUt t es. sG pSuppo ted( equest) && !GzipUtilities.isGzipDisabled(request)) { out = GzipUtilities.getGzipWriter(response); response.setHeader("Content-Encoding", "gzip");p ( g , g p ); } else { out = response.getWriter(); }20 Sending Compressed Pages: LongServlet java (Continued)LongServlet.java (Continued) … out printlnout.println (docType + "<HTML>n" + "<HEAD><TITLE>" + title + "</TITLE></HEAD>n" +<HEAD><TITLE> + title + </TITLE></HEAD>n + "<BODY BGCOLOR="#FDF5E6">n" + "<H1 ALIGN="CENTER">" + title + "</H1>n"); String line = "Blah, blah, blah, blah, blah. " +String line Blah, blah, blah, blah, blah. + "Yadda, yadda, yadda, yadda."; for(int i=0; i<10000; i++) { out.println(line);out.p t ( e); } out.println("</BODY></HTML>"); out.close();(); } } 21
  • 11. Sending Compressed Pages: ResultsResults • Uncompressed (28.8K modem), Fi f d I E lFirefox and Internet Explorer: > 50 seconds Compressed (28 8K modem)• Compressed (28.8K modem), Firefox and Internet Explorer: < 5 seconds5 seconds • Caution – Be careful about generalizing benchmarksbenchmarks 22 Differentiating Among Different Browser TypesDifferent Browser Types • Use User-Agent only when necessary. Other ise o ill ha e diffic lt to maintain code that consists of– Otherwise, you will have difficult-to-maintain code that consists of tables of browser versions and associated capabilities. • Check for null. Th h d i t i d b th HTTP 1 1 ifi ti– 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. • Differentiating among clients• Differentiating among clients – To differentiate among Firefox, Netscape, and Internet Explorer, check for “MSIE,” not “Mozilla.” • Both Firefox and Internet Explorer say “Mozilla” at the beginning of theBoth Firefox and Internet Explorer say Mozilla at the beginning of the header. (For JavaScript compatibility.) – Detecting Safari on the iPhone • Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML lik G k ) V i /3 0 M bil /1A543 S f i/419 3(KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3 • The header can be faked. – If a client fakes this header, the servlet cannot tell the difference.23
  • 12. Differentiating Among Different Browser Types (Code)Different Browser Types (Code) @WebServlet("/browser-insult") public class BrowserInsult extends HttpServlet {p p public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html");response.setContentType( text/html ); PrintWriter out = response.getWriter(); String title, message; // Assume for simplicity that Firefox and IE are // the only two browsers// the only two browsers. String userAgent = request.getHeader("User-Agent"); if ((userAgent != null) && (userAgent.contains("MSIE")) { titl " i ft i i "title = "Microsoft Minion"; message = "Welcome, O spineless slave to the " + "mighty empire."; } else { title = "Hopeless Firefox Rebel"; message = "Enjoy it while you can. " + "You <I>will</I> be assimilated!"; }24 Differentiating Among Browser Types (Result)Browser Types (Result) 25
  • 13. Summary • HTTP is important Man ser let tasks can l be accomplished b making se of– 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 • Shortcuts discussed later Cookies authorization info content length and content type– Cookies, authorization info, content length, and content type have shortcut methods • Most important headers you read directly – Accept – Accept-Encoding – Connection– Connection – Referer – User-Agent26 © 2010 Marty Hall Questions? Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.27