Generating the Server Response: HTTP Status Codes

Similar documents
Generating the Server Response:

GENERATING THE SERVER RESPONSE: HTTP STATUS CODES

Generating the Server Response: HTTP Response Headers

HTTP status codes. Setting status of an HTTPServletResponse

Advanced Web Technology

In servlet, form parsing is handled automatically. You call request.getparameter to get the value of a form parameter.

Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2

Get the cookies from the service request: Cookie[] HttpServletRequest.getCookies() Add a cookie to the service response:

Handling the Client Request: HTTP Request Headers

Session 8. JavaBeans. Reading & Reference. Reading. Reference. Session 8 Java Beans. 2/27/2013 Robert Kelly, Head First Chapter 3 (MVC)

JAVA SERVLET. Server-side Programming INTRODUCTION

Servlet Basics. Agenda

Using Java servlets to generate dynamic WAP content

Advanced Topics in Operating Systems. Manual for Lab Practices. Enterprise JavaBeans

JSR 311: JAX-RS: The Java API for RESTful Web Services

INTRODUCTION TO SERVLETS AND WEB CONTAINERS. Actions in Accord with All the Laws of Nature

SERVLETS INTERVIEW QUESTIONS

Connecting the RISC Client to non-javascriptinterfaces

Using Applets as Front Ends to Server-Side Programs

A10: Unvalidated Redirects and Forwards Axx: Unsolicited Framing

Integrating Servlets and JavaServer Pages Lecture 13

Servlet and JSP Review

Servlets and JSP (Java Server Pages)

Integrating Servlets and JSP: The MVC Architecture

CH -7 RESPONSE HEADERS

Java Enterprise Edition. Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 1

JavaServer Pages (JSP)

********************************************************************

To follow the Deitel publishing program, sign-up now for the DEITEL BUZZ ON-

UNIT 6:CH:14 INTEGRATING SERVLETS AND JSPTHE MVC ARCHITECTURE

Using Applets as Front Ends to

Advanced Internet Technology Lab # 4 Servlets

Introduction to Servlets. After which you will doget it

Servlets by Example. Joe Howse 7 June 2011

Servlets. How to use Apache FOP in a Servlet $Revision: $ Table of contents

Three hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. Date: Friday 21 st May Time:

Questions and Answers

ServletConfig Interface

Servlets. How to use Apache FOP in a Servlet $Revision: $ Table of contents

CE212 Web Application Programming Part 3

Lab session Google Application Engine - GAE. Navid Nikaein

Distributed Systems 1

Advanced Internet Technology Lab # 6

HTTP Console Documentation

Kamnoetvidya Science Academy. Object Oriented Programming using Java. Ferdin Joe John Joseph. Java Session

Stateless -Session Bean

Servlets. An extension of a web server runs inside a servlet container

1Z Java EE 6 Web Component Developer Certified Expert Exam Summary Syllabus Questions

Discovery data feed for Eid 2.0

JAVA SERVLET. Server-side Programming PROGRAMMING

Managed Beans III Advanced Capabilities

CS193i Final Exam SITN students -- hopefully you are getting this soon enough to take it on

Lecture 7b: HTTP. Feb. 24, Internet and Intranet Protocols and Applications

Backend. (Very) Simple server examples

Database Systems Lab. 11. JSP I 충남대학교컴퓨터공학과 데이타베이스시스템연구실

WHITE LABELING IN PROGRESS ROLLBASE PRIVATE CLOUD

Handling Cookies. Agenda

The HTTP protocol. Fulvio Corno, Dario Bonino. 08/10/09 http 1

Session 8. Introduction to Servlets. Semester Project

Session 9. Deployment Descriptor Http. Reading and Reference. en.wikipedia.org/wiki/http. en.wikipedia.org/wiki/list_of_http_headers

HTTP Authentication API

core programming Servlets Marty Hall, Larry Brown

Java Servlets. Preparing your System

A.1 JSP A.2 JSP JSP JSP. MyDate.jsp page contenttype="text/html; charset=windows-31j" import="java.util.calendar" %>

LAB 1 PREPARED BY : DR. AJUNE WANIS ISMAIL FACULTY OF COMPUTING UNIVERSITI TEKNOLOGI MALAYSIA

This page discusses topic all around using FOP in a servlet environment. 2. Example Servlets in the FOP distribution

why? Give an app access to a resource managed by someone else, without giving the app your password. A valet key for the web Allen I.

Session 9. Introduction to Servlets. Lecture Objectives

Servlet for Json or CSV (or XML) A servlet serving either Json or CSV (or XML) based on GET parameter - This version uses org.json

Hypertext Transport Protocol

Lecture 3. HTTP v1.0 application layer protocol. into details. HTTP 1.0: RFC 1945, T. Berners-Lee HTTP 1.1: RFC 2068, 2616

SERVLET AND JSP FILTERS

SSC - Web applications and development Introduction and Java Servlet (I)

This tutorial will teach you how to use Java Servlets to develop your web based applications in simple and easy steps.

Session 20 Data Sharing Session 20 Data Sharing & Cookies

Programming on the Web(CSC309F) Tutorial 9: JavaServlets && JDBC TA:Wael Abouelsaadat

Web Application Services Practice Session #2

&' () - #-& -#-!& 2 - % (3" 3 !!! + #%!%,)& ! "# * +,

Web based Applications, Tomcat and Servlets - Lab 3 -

Complimentary material for the book Software Engineering in the Agile World

Chapter 17. Web-Application Development

Module 3 Web Component

Non-atomic check and use aka TOCTOU (Time of Check, Time of Use) or race conditions. Erik Poll Digital Security group Radboud University Nijmegen


Penetration: from application down to OS

CGI / HTTP(s) GET NETIO M2M API protocols docs

Java4570: Session Tracking using Cookies *

Servlets Overview

Session 10. Form Dataset. Lecture Objectives

Handout 31 Web Design & Development

Developing a Mobile Web-based Application with Oracle9i Lite Web-to-Go

Session 11. Calling Servlets from Ajax. Lecture Objectives. Understand servlet response formats

JSP. Common patterns

Université Antonine - Baabda

AJP. CHAPTER 5: SERVLET -20 marks

Servlets1. What are Servlets? Where are they? Their job. Servlet container. Only Http?

Java Technologies Web Filters

Steps for Implementing a Server

How to work with HTTP requests and responses

Network Programming: Servers

Transcription:

Generating the Server Response: HTTP Status Codes 1 Agenda Format of the HTTP response How to set status codes What the status codes are good for Shortcut methods for redirection and error pages A servlet that redirects users to browserspecific pages A front end to various search engines 2 1

HTTP Request/Response Request GET /servlet/somename HTTP/1.1 Host:... Header2:...... HeaderN: (Blank Line) Response HTTP/1.1 200 OK Content-Type: text/html Header2:...... HeaderN:... (Blank Line) <!DOCTYPE...> <HTML> <HEAD>...</HEAD> <BODY>... </BODY></HTML> 3 Setting Status Codes response.setstatus(int statuscode) Use a constant for the code, not an explicit int. Constants are in HttpServletResponse Names derived from standard message. E.g., SC_OK, SC_NOT_FOUND, etc. response.senderror(int code, String message) Wraps message inside small HTML document response.sendredirect(string url) Sets status code to 302 Sets Location response header also 4 2

Common HTTP 1.1 Status Codes 200 (OK) Everything is fine; document follows. Default for servlets. 204 (No Content) Browser should keep displaying previous document. 301 (Moved Permanently) Requested document permanently moved elsewhere (indicated in Location header). Browsers go to new location automatically. 5 Common HTTP 1.1 Status Codes (Continued) 302 (Found) Requested document temporarily moved elsewhere (indicated in Location header). Browsers go to new location automatically. Servlets should use sendredirect, not setstatus, when setting this header. See example. 401 (Unauthorized) Browser tried to access password-protected page without proper Authorization header. 404 (Not Found) No such page. Servlets should use senderror to set this. Fun and games: http://www.plinko.net/404/ 6 3

A Servlet That Redirects Users to Browser-Specific Pages public class WrongDestination extends HttpServlet { public void doget(httpservletrequest request, HttpServletResponse response) throws ServletException, IOException { String useragent = request.getheader("user-agent"); if ((useragent!= null) && (useragent.indexof("msie")!= -1)) { response.sendredirect("http://home.netscape.com"); else { response.sendredirect("http://www.microsoft.com"); 7 A Servlet That Redirects Users to Browser-Specific Pages 8 4

9 A Front End to Various Search Engines public class SearchEngines extends HttpServlet { public void doget(httpservletrequest request, HttpServletResponse response) throws ServletException, IOException { String searchstring = request.getparameter("searchstring"); if ((searchstring == null) (searchstring.length() == 0)) { reportproblem(response, "Missing search string"); return; searchstring = URLEncoder.encode(searchString); String searchenginename = request.getparameter("searchengine"); if ((searchenginename == null) (searchenginename.length() == 0)) { reportproblem(response, "Missing search engine name"); return; A Front End to Various Search Engines (Continued) String searchurl = SearchUtilities.makeURL(searchEngineName, searchstring); if (searchurl!= null) { response.sendredirect(searchurl); else { reportproblem(response, "Unrecognized search engine"); private void reportproblem(httpservletresponse response, String message) throws IOException { response.senderror(response.sc_not_found, message); 10 5

A Front End to Various Search Engines (Continued) public class SearchSpec { /** Builds a URL for the results page by * simply concatenating the base URL * (http://...?somevar=") with the * URL-encoded search string (jsp+training). */ public String makeurl(string searchstring) { return(baseurl + searchstring); 11 Front End to Search Engines: HTML Form 12 6

Front End to Search Engines: Result for Valid Data 13 Front End to Search Engines: Result for Invalid Data 14 7

Summary 15 Many servlet tasks can only be accomplished through use of HTTP status codes Setting status codes: In general, set via response.setstatus In special cases (302 and 404), set with response.sendredirect and response.senderror Most important status codes 200 (default) 302 (forwarding; set with sendredirect) 401 (password needed) 404 (not found; set with senderror) 8