Java-Web:Servlet - HTTP response codes
Some notes about this chapter
In this chapter, we show how to set HTTP response codes (a.k.a. status codes) from a Java Servlet.
If you need to know what HTTP is in general and what response codes (a.k.a. status codes) are in particula, please visit our chapter on HTTP here.
Note to students and teachers: If you (as a student or teacher) are running cygwin on bash, the same things apply here as always. Java class paths on windows need to use a semicolon instead of a plain old colon. And since we are running bash, semicolons have a special meaning. So if you are running cygwin and bash, you need to enclose your classpath (with semicolons) in double quotes.
Another thing for cygwin or windows users running bash. If you edit a bash script on windows, there is a risk that your editor tries to help you by corrupting the file by using windows text file format line-endings. In windows, textfiles have line-endings consisting of two characters,
\r\n, which is called "CRLF" (carriage return, linefeed). While being stupid and unnecessary, this is something you will run into as a programmer or technician! Windows is not like the rest of the operating systems. Windows typically likes to do everything which is standardized on all other operating systems, differently. Just because it can.
There are ways to fix a corrupted bash script (corrupted by a "helpful" editor on windows). You can convert the file from DOS format (CRLF line-endings) to UNIX format (LF endings) using command line programs such as
dos2unix. And you can probably convince your editor to change the file back to UNIX format by using some menu. Atom has even a small box at the bottom right, displaying either CRLF or LF, which you can click on and change it to LF (if it displays CRLF) and then save the file with the correct format.
You can read more here: Scrip error - Syntax error
The lecture in this chapter explains how to use status codes (response codes) like "404 Not found" or "200 OK" in HTTP from a Java Servlet. The exercises contain a challenge for you to, to add this functionality to an existing Servlet.
Videos and lecture slides
- Wikipedia - HTTP status codes
- JavaMex - servlets http status code
- TutorialsPoint Servlets - HTTP status codes
- polytech.unice.fr - Servlet-Tutorial-Response-Status-Line
Up next: Exercises!