Java-Web:Exercises - Servlet - HTTP response codes

From Juneday education
Jump to: navigation, search

Exercises

Get the code

Get the source code from our github repository.


There are scripts for compiling and running. Note that if you are on Windows, you must change the class path to use a semicolon instead of a colon, and put the whole class path in double quotes.

The scripts should be run from the exercise directory (above the webroot directory).

About the build.sh script: This script simply compiles your servlet! It is nothing fancy. You should be able to compile a Java class yourself by now. But if you feel insecure, don't worry! We'll explain how to compile it here. The Servlet is extending a class in the javax.servlet package hierarchy. These packages are not included in your standard Java JDK installation. However, they are included in the Winstone JAR file.

Now, the script assumes that your winstone JAR file is called jenkins-winstone-0.9.10-jenkins-47.jar and is placed in your current directory. If you have a different winstone JAR file, obviously you have to either change the script or the name of your JAR file.

To compile it on the command line you can do like this (if you don't like our script):

UNIX/Linux/Mac (assuming your winston jar file is in the current directory and called jenkins-winstone-0.9.10-jenkins-47.jar):

$ javac -cp jenkins-winstone-0.9.10-jenkins-47.jar:webroot/WEB-INF/lib/* webroot/WEB-INF/classes/student/servlets/DataServlet.java

Windows using Cygwin (also assuming your winstone jar file is in your current directory and is called jenkins-winstone-0.9.10-jenkins-47.jar:

$ javac -cp "jenkins-winstone-0.9.10-jenkins-47.jar;webroot/WEB-INF/lib/*" webroot/WEB-INF/classes/student/servlets/DataServlet.java

Instructions for Windows and Cygwin users

You are running Windows and Cygwin and the scrip doesn't work. This is because bash interprets command lines differently from how Windows does it.

For instance, if the script build.sh has a line saying:

javac -cp jenkins-winstone-0.9.10-jenkins-47.jar:webroot/WEB-INF/lib/* webroot/WEB-INF/classes/student/servlets/DataServlet.java

Then, in Windows, you need to change that very line to:

javac -cp "jenkins-winstone-0.9.10-jenkins-47.jar;webroot/WEB-INF/lib/*" webroot/WEB-INF/classes/student/servlets/DataServlet.java

That means, you should add both " (double quotes) around the whole class path, and replace the : (colon) with ; (semicolon). Why? Because on windows, the semicolon is used as a directory separator in paths. Ask Microsoft why they need to do everything backwards and differently than the rest of the world if you are curious as to why. And, then, in bash, a semicolon is to separate command lines from eachother. We don't want bash to interpret the semicolon as the terminator of a command. We want bash to interpret the whole class path as one single string. So that's why you need to put double qoutes around the classpath. The double quotes instruct bash that it is one single string. Then the semicolon won't be treated as a special command-terminating character. Instead, bash will send the whole string (in this case jenkins-winstone-0.9.10-jenkins-47.jar;webroot/WEB-INF/lib/* to the javac command (which is a windows command).

So in summary:

Running javac or java in cygwin bash with a class path, requires you to replace the colon between paths with semicolon, and to put the whole class path in double quotes (because the semicolon has a special meaning in bash...).

In addition, if you used a windows editor to change the script, then it is unfortunately possible that you saved the script as a "windows text file" by mistake (because your editor is retarded and thought that was a great idea). That won't work at all with bash scripts, because bash scripts need to be real text files with normal line endings (a single \n a.k.a. "LF") in order for bash to interpret the command lines in the script.

If your editor saved the bash script as a Windows text file, every newline character has been replaced with two characters (the way windows likes it for no apparent reason), that is \r\n a.k.a. "CRLF". If so, open the file again and make sure you save it as a UNIX text file (with the proper line endings LF). This is done differently in different editors...

See here for help: Bash FAQ about unexpected token or use a search engine like google to search for syntax error near unexpected token `$'{\r' and usually the first result is explanations of how to fix this.

Change the servlet to send response codes

Open the servlet in webroot/WEB-INF/classes/student/servlets/DataServlet.java and change it so that it sends status code 400 Bad Request if either the format parameter is missing, or if an unknown format is requested.

Expand using link to the right to see a hint.

Put this in the appropriate if clauses:

response.setStatus(HttpServletResponse.SC_BAD_REQUEST);

Verify that it sends the proper response codes if the paramter is wrong or missing.

Expand using link to the right to see a hint.

You can get the HTTP headers like this:

$ lwp-request -m HEAD http://localhost:8080/dataservlet?format=xml
$ lwp-request -m HEAD http://localhost:8080/dataservlet?grodan_boll
$ lwp-request -m HEAD http://localhost:8080/dataservlet?format=kalle_stropp

Links

Source code

Download the exercise source code from [1]

Navigation

Up next: RequestDispatch - forward a request from one servlet to another!

« PreviousBook TOCNext »