HTTP status codes. Setting status of an HTTPServletResponse

Similar documents
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

Backend. (Very) Simple server examples

ServletConfig Interface

Generating the Server Response: HTTP Status Codes

Advanced Internet Technology Lab # 4 Servlets

Lab session Google Application Engine - GAE. Navid Nikaein

JAVA SERVLET. Server-side Programming INTRODUCTION

Generating the Server Response:

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

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

CIS 3952 [Part 2] Java Servlets and JSP tutorial

Servlets by Example. Joe Howse 7 June 2011

Implementation Architecture

Session 8. Introduction to Servlets. Semester Project

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

Front-end / Back-end. How does your application communicate with services?

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

Stateless -Session Bean

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

Implementation Architecture

Advanced Web Technology

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

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

Using Java servlets to generate dynamic WAP content

Session 9. Introduction to Servlets. Lecture Objectives

Université Antonine - Baabda

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

Getting started with Winstone. Minimal servlet container

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

CREATE A SERVLET PROGRAM TO DISPLAY THE STUDENTS MARKS. To create a servlet program to display the students marks

Unit-4: Servlet Sessions:

Servlet Basics. Agenda

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

Copyright 2014 Blue Net Corporation. All rights reserved

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

Introduction to Servlets. After which you will doget it

Servlet 5.1 JDBC 5.2 JDBC

Session 10. Form Dataset. Lecture Objectives

Black Box DCX3000 / DCX1000 Using the API

Handling the Client Request: HTTP Request Headers

Web based Applications, Tomcat and Servlets - Lab 3 -

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

Servlet. Web Server. Servlets are modules of Java code that run in web server. Internet Explorer. Servlet. Fire Fox. Servlet.

Java Technologies Web Filters

Develop an Enterprise Java Bean for Banking Operations

AJP. CHAPTER 5: SERVLET -20 marks

J2ME With Database Connection Program

To create a view for students, staffs and courses in your departments using servlet/jsp.

Handout 31 Web Design & Development

A10: Unvalidated Redirects and Forwards Axx: Unsolicited Framing

Welcome To PhillyJUG. 6:30-7:00 pm - Network, eat, find a seat 7:00-7:15 pm - Brief announcements 7:15-8:30 pm - Tom Janofsky's presentation

Generating the Server Response: HTTP Response Headers


Java servlets CSCI 470: Web Science Keith Vertanen Copyright 2013

Java TM. JavaServer Faces. Jaroslav Porubän 2008

Component Based Software Engineering

Connecting the RISC Client to non-javascriptinterfaces

Archer Documentation. Release 0.1. Praekelt Dev

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

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

Penetration: from application down to OS

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

Introduction. This course Software Architecture with Java will discuss the following topics:

CE212 Web Application Programming Part 3

COMP 213. Advanced Object-oriented Programming. Lecture 20. Network Programming

Introduction. Literature: Steelman & Murach, Murach s Java Servlets and JSP. Mike Murach & Associates Inc, 2003

HTTP Reading: Section and COS 461: Computer Networks Spring 2013

Scheme G Sample Question Paper Unit Test 2

Java Servlets. Preparing your System

CH -7 RESPONSE HEADERS

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

Session 11. Ajax. Reading & Reference

vrealize Log Insight Developer Resources

vrealize Log Insight Developer Resources Update 1 Modified on 03 SEP 2017 vrealize Log Insight 4.0

JAVA SERVLET. Server-side Programming ADVANCED FEATURES

Servlet. 1.1 Web. 1.2 Servlet. HTML CGI Common Gateway Interface Web CGI CGI. Java Applet JavaScript Web. Java CGI Servlet. Java. Apache Tomcat Jetty

Java4570: Session Tracking using Cookies *

WHITE LABELING IN PROGRESS ROLLBASE PRIVATE CLOUD

CSC309: Introduction to Web Programming. Lecture 10

SERVLET AND JSP FILTERS

1 Front-end Technologies - Historical Overview

Complimentary material for the book Software Engineering in the Agile World

Accessing EJB in Web applications

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.

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

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

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

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

Handling Cookies. Agenda

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

Servlet and JSP Review

Wanted: Students to participate in a user study

CS631 - Advanced Programming in the UNIX Environment

The Basic Web Server CGI. CGI: Illustration. Web based Applications, Tomcat and Servlets - Lab 3 - CMPUT 391 Database Management Systems 4

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

sessionx Desarrollo de Aplicaciones en Red A few more words about CGI CGI Servlet & JSP José Rafael Rojano Cáceres

Welcome to Ruby. Yehuda Katz. Engine Yard

WWW Architecture I. Software Architecture VO/KU ( / ) Roman Kern. KTI, TU Graz

Advanced Internet Technology Lab # 5 Handling Client Requests

Checkout Service API User Guide

Transcription:

HTTP status codes Setting status of an HTTPServletResponse

What are HTTP status codes? The HTTP protocol standard includes three digit status codes to be included in the header of an HTTP response. There are five classes of response statuses, identified by the first digit of the status code (with examples below): 1xx Informational 101 Switching Protocols (server agrees to switch) 2xx Success 200 OK 3xx Redirection 301 Moved Permanently 4xx Client Error 404 Not Found 5xx Server Error 500 Internal Server Error (something bad happened)

Setting the status in a Servlet If your servlet succeeds in sending the response to the client, you can set the HTTP status code like this: response.setstatus(httpservletresponse.sc_ok); If your servlet runs into any trouble, you should let the client know, by sending an appropriate status code (which you should document), e.g.: HttpServletResponse.SC_NOT_FOUND What code to send depends on the type of problem of course.

If your servlet is an API for data If your servlet is an API for getting data as e.g. Json, you should document the behaviour on the API documentation page. You can use SC_NOT_FOUND if the data cannot be served because it doesn t exist. You can also send an error message encoded as Json: StringBuilder page = new StringBuilder(); response.setstatus(sc_not_found); // needs import static... page.append("{studentapierror:\"").append(nsse.getmessage()) // you can include exception messages if you want.append("\""); out.println(page); out.close();

Example - student data API error - not found $ lwp-request -m GET http://localhost:8080/jsonstudents?id=666 {studentapierror:"no such student with ID: 666" $ lwp-request -m HEAD http://localhost:8080/jsonstudents?id=666 404 Not Found Connection: Keep-Alive Date: Thu, 26 Jan 2017 11:37:39 GMT Server: Winstone Servlet Engine v0.9.10 Content-Length: 49 Content-Type: application/json Client-Date: Thu, 26 Jan 2017 11:37:39 GMT Client-Peer: 127.0.0.1:8080 Client-Response-Num: 1 X-Powered-By: Servlet/2.5 (Winstone/0.9.10)

Example - student data API error - parameters $ lwp-request -m HEAD http://localhost:8080/jsonstudents?wrong=very-wrong 400 Bad Request Connection: Keep-Alive Date: Thu, 26 Jan 2017 11:39:12 GMT Server: Winstone Servlet Engine v0.9.10 Content-Length: 59 Content-Type: application/json Client-Date: Thu, 26 Jan 2017 11:39:11 GMT Client-Peer: 127.0.0.1:8080 Client-Response-Num: 1 X-Powered-By: Servlet/2.5 (Winstone/0.9.10) $ lwp-request -m GET http://localhost:8080/jsonstudents?wrong=very-wrong {name: "JSONRequestError",message: "missing id parameter"

Example - student data API error - parameters public void doget(httpservletrequest request, HttpServletResponse response) throws ServletException, IOException{ String id = request.getparameter("id"); if(id!=null){ response.setcontenttype("application/json"); PrintWriter out = response.getwriter(); try{ printresult(out,id); catch(nosuchstudentexception nsse){ StringBuilder page = new StringBuilder(); response.setstatus(sc_not_found); page.append("{studentapierror:\"").append(nsse.getmessage()).append("\""); out.println(page); out.close(); else{ printerror(response); // Missing required ID parameter

Example - student data API error - parameters // Handles the missing ID parameter response private void printerror(httpservletresponse response)throws IOException{ response.setcontenttype("application/json"); response.setstatus(sc_bad_request); PrintWriter out = response.getwriter(); out.print("{name: \"JSONRequestError\","); out.println("message: \"missing id parameter\""); out.close();

Normal response $ lwp-request -m GET http://localhost:8080/jsonstudents?id=5 #valid call with existing ID { "studentname":"edward Eriksson", "studentid":"5", "courses":[ { "coursecode":"tig058" ] $ lwp-request -m HEAD http://localhost:8080/jsonstudents?id=5 200 OK Connection: Keep-Alive Date: Thu, 26 Jan 2017 11:46:41 GMT Server: Winstone Servlet Engine v0.9.10 Content-Length: 0 Content-Type: application/json Client-Date: Thu, 26 Jan 2017 11:46:41 GMT Client-Peer: 127.0.0.1:8080 Client-Response-Num: 1 X-Powered-By: Servlet/2.5 (Winstone/0.9.10)

Links https://en.wikipedia.org/wiki/list_of_http_status_codes https://www.tutorialspoint.com/servlets/servlets-http-status-codes.htm https://kodejava.org/how-do-i-send-a-response-status-in-servlet/