Generating the Server Response: HTTP Status Codes

Slides:



Advertisements
Similar presentations
JLab Lattice Portal – Data Grid Web Service Ying Chen, Chip Watson Thomas Jefferson National Accelerator Facility.
Advertisements

 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
HTTP Requests & Responses1 Servlets: HTTP Request Header Contents and Responses.
HTTP HyperText Transfer Protocol. HTTP Uses TCP as its underlying transport protocol Uses port 80 Stateless protocol (i.e. HTTP Server maintains no information.
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.
Core servlets chapter 6 Generating server response: http status codes.
HTTP – HyperText Transfer Protocol
CS320 Web and Internet Programming Generating HTTP Responses
16-Jun-15 HTTP Hypertext Transfer Protocol. 2 HTTP messages HTTP is the language that web clients and web servers use to talk to each other HTTP is largely.
HTTP Hypertext Transfer Protocol. HTTP messages HTTP is the language that web clients and web servers use to talk to each other –HTTP is largely “under.
HTTP Response Headers Vijayan Sugumaran Department of DIS Oakland University Parts of this presentation was provided by
Hypertext Transfer Protocol Information Systems 337 Prof. Harry Plantinga.
2/9/2004 Web and HTTP February 9, /9/2004 Assignments Due – Reading and Warmup Work on Message of the Day.
Hypertext Transport Protocol CS Dick Steflik.
Client, Server, HTTP, IP Address, Domain Name. Client-Server Model Client Bob Yahoo Server yahoo.com/finance.html A text file named finance.html.
 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.
Rensselaer Polytechnic Institute CSC-432 – Operating Systems David Goldschmidt, Ph.D.
Servlets. Our Project 3-tier application Develop our own multi-threaded server Socket level communication.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
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.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
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.
Slides © Marty Hall, book © Sun Microsystems Press 1 Generating the HTTP Response Core Servlets & JSP book:
CIS679: Lecture 13 r Review of Last Lecture r More on HTTP.
1-1 HTTP request message GET /somedir/page.html HTTP/1.1 Host: User-agent: Mozilla/4.0 Connection: close Accept-language:fr request.
20-Nov-15introServlets.ppt Intro to servlets. 20-Nov-15introServlets.ppt typical web page – source Hello Hello.
Java Servlet API CGI / HTTP Concepts Java Servlet API.
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.
1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.
© Marty Hall, Larry Brown Web core programming 1 Using Applets as Front Ends to Server-Side Programs.
Overview of Servlets and JSP
HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,
COMP2322 Lab 2 HTTP Steven Lee Jan. 29, HTTP Hypertext Transfer Protocol Web’s application layer protocol Client/server model – Client (browser):
How CGI and Java Servlets are Run By David Stein 14 November 2006.
Using Applets as Front Ends to Server-Side Programs.
Lecture # 1 By: Aftab Alam Department Of Computer Science University Of Peshawar Internet Programming.
Introduction to Servlets
Servlets: HTTP Request Header Contents and Responses
Hypertext Transfer Protocol
Tiny http client and server
Building Web Apps with Servlets
Handling Errors in Web Applications
CS320 Web and Internet Programming Generating HTTP Responses
Content from Python Docs.
HTTP – An overview.
Hypertext Transfer Protocol
Networking CS 3470, Section 1 Sarah Diesburg
COMP2322 Lab 2 HTTP Steven Lee Feb. 8, 2017.
Hypertext Transfer Protocol
Hypertext Transport Protocol
CS3220 Web and Internet Programming Generating HTTP Responses
Introduction Web Environments
HTTP request message two types of HTTP messages: request, response
IS333D: MULTI-TIER APPLICATION DEVELOPMENT
Hypertext Transfer Protocol
CS320 Web and Internet Programming Cookies and Session Tracking
HTTP Request Method URL Protocol Version GET /index.html HTTP/1.1
Hypertext Transfer Protocol
Servlet APIs Every servlet must implement javax.servlet.Servlet interface Most servlets implement the interface by extending one of these classes javax.servlet.GenericServlet.
Hypertext Transfer Protocol
HyperText Transfer Protocol
Hypertext Transfer Protocol (HTTP)
Hypertext Transfer Protocol
Kevin Harville Source: Webmaster in a Nutshell, O'Rielly Books
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.
Pre-assessment Questions
Hypertext Transfer Protocol
Presentation transcript:

Generating the Server Response: HTTP Status Codes

HTTP Request/Response 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>

When a web server responds to a request, the response consists of a status line, some response headers, a blank line and the document. The status line consists of the HTTP version (HTTP/1.1 in the preceding example), a status code (an integer; 200 in the example), and a very short message corresponding to the status code (OK in the example).

Methods to set HTTP Status Codes The following methods can be used to set HTTP Status Code in your servlet program. These methods are available with HttpServletResponse object. public void setStatus( int statusCode ) This method sets an arbitrary status code. The setStatus method takes an int (the status code) as an argument. Use a constant for the code, not an explicit int. These constants are defined in HttpServletResponse. Names derived from standard message. E.g., SC_OK, SC_NOT_FOUND, etc.

Set status codes before sending any document content to the client. public void sendRedirect(String url) This method generates a 302 response along with a Location header giving the URL of the new document. The 302 status code directs the browser to connect to a new location. public void sendError(int code, String message) This method sends a status code (usually 404) along with a short message that is automatically formatted inside an HTML document and sent to the client. The 404 status code is used when no document is found on the server.

HTTP 1.1 Status Codes There are five general categories of specific status codes available in HTTP 1.1 : 100 – 199 Informational codes Client should respond with some other action 200 – 299 Signify that the request was successful 300 – 399 Used for files that have moved Includes a Location header indicating the new address 400 – 499 Error by the client 500 – 599 Signify an error by the server

Common HTTP 1.1 Status Codes (contd.) 100 (Continue) Only a part of the request has been received by the server, but as long as it has not been rejected, the client should continue with the request 200 (OK) Everything is fine; document follows for GET and POST. This is default for servlets. 202 (Accepted) The request is accepted for processing, but the processing is not complete. 204 (No Content) Browser should keep displaying previous document.

Common HTTP 1.1 Status Codes (contd.) 301 (Moved Permanently) Requested document permanently moved elsewhere (document URL is indicated in Location header). Browsers go to new location automatically. 302 (Found) Requested document temporarily moved elsewhere (indicated in Location header). Servlets should use sendRedirect, not setStatus, when setting this header.

Common HTTP 1.1 Status Codes (contd.) 400 (Bad Request) The server did not understand the request 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. 405 (Method Not Allowed) The method specified in the request is not allowed. 415 ( Unsupported Media Type) The server will not accept the request, because the media type is not supported.

Common HTTP 1.1 Status Codes (contd.) 500 (Internal Server Error) The request was not completed. The server met an unexpected condition 501 (Not Implemented) The request was not completed. The server did not support the functionality required. 503 (Service Unavailable) The request was not completed. The server is temporarily overloading or down. 505 (HTTP Version Not supported) The server does not support the "http protocol" version.

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"); } } }

A Servlet That Redirects Users to Browser-Specific Pages

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); } }

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); } …

Front End to Search Engines: HTML Form

Front End to Search Engines: Result for Valid Data

Front End to Search Engines: Result for Invalid Data