Upgrade to Pro — share decks privately, control downloads, hide ads and more …

An Introduction to HTTP

An Introduction to HTTP

Prepared for ThoughtWorks Junior Consultant group

Chris Bushell

July 06, 2012
Tweet

More Decks by Chris Bushell

Other Decks in Programming

Transcript

  1. URIs   •  Uniform  Resource  IdenEfiers   •  What’s  the

     difference  between  a  URI  and  a   URL?  
  2. Status  Codes   •  1xx  InformaEonal  (HTTP  1.1)   • 

    2xx  Success   –  200  Ok   –  201  Created   •  3xx  RedirecEon   –  302  Found   •  4xx  Client  Error   –  400  Bad  Request   –  404  Not  Found   •  5xx  Server  Error   –  500  Internal  Server  Error  
  3. Expires   •  Date/Eme  a\er  which  the  response  is  

    considered  stale   •  What  does  a  value  of  “-­‐1”  mean?  
  4. Cache-­‐Control   •  InstrucEon  to  all  recipients  along  the  request/

    response  chain   •  What  does  “private”  mean?  
  5. HTTP  State   •  HTTP  is  stateless   •  What

     challenges  does  this  present?  
  6. Exercises  -­‐  Setup   •  Workspace  setup   – Tomcat  

    – Eclipse   – curl   – Chrome  (try  Cmd  +  Alt  +  I)  
  7. Project  Setup   •  Go  to  ‘Java  EE’  perspecEve  

    •  Go  to  ‘Servers’  view  and  configure  a  Tomcat   server   •  Create  a  ‘Dynamic  Web  Project’   •  Create  a  ‘DemoServlet’  
  8. I  -­‐  Hello  World  GET   •  Modify  the  servlet

     to  print  ‘Hello  World’  in  the   response      
  9. II  -­‐  GET  with  curl     •  Send  an

     HTTP  GET  request  using  curl  
  10. III  -­‐  What  is  my  UserAgent?     •  Print

     the  UserAgent  informaEon  in  the   response   •  Use  a  browser  to  test  
  11. IV  -­‐  UserAgent  is  ‘myself’     •  Use  curl

     to  set  the  UserAgent  to  ‘myself’  and   print  it  in  the  response    
  12. V  -­‐  POST  with  curl     •  POST  a

     request,  using  curl,  with  values     ‘name’:  ‘<your  name>’   ‘interest’:  ‘<your  interest>’   •  And  print  them  in  the  response  
  13. Servlet  API   •  HkpServlet   •  HkpServletRequest   • 

    HkpServletResponse   •  HkpSession   •  HkpServletContext  
  14. VI  –  Count  my  requests   •  Implement  a  Servlet

     based  counter  that  on   each  invocaEon  prints   ‘Hello!  This  request  number  <number>’   •  Invoke  it  using  browser   •  And  now  invoke  it  using  curl  
  15. Extra  Credit  J   •  Create  an  authenEcaEon  servlet  that

     validates   users  based  on  form  parameters  ‘username’   and  ‘password’  against  hardcoded  values   •  Implement  the  previous  ‘counter’  example,   incremenEng  the  counter  only  if  the  user  is   authenEcated      
  16. Further  Reading   •  Request  methods   – hkp://www.w3.org/Protocols/rfc2616/rfc2616-­‐ sec9.html  

    •  Status  codes   – hkp://www.w3.org/Protocols/rfc2616/rfc2616-­‐ sec10.html   •  Header  Fields   – hkp://www.w3.org/Protocols/rfc2616/rfc2616-­‐ sec14.html#sec14.9.1