You are on page 1of 1

SingleThreadModel, making it thread safe

Sending an Error Code


The HttpServletResponse also allows you to send pre-defined error messages. The interface defines a number of public static final integers that all start with SC_. For example, SC_FORBIDDEN will be translated into an HTTP error 403. Along with the error code, you also can send a custom error message. Instead of redisplaying the Login page when a failed login occurs, you can send an HTTP error 403 plus your error message. To do this, replace the call to the sendLoginForm in the doPost method with the following:
response.sendError(response.SC_FORBIDDEN, "Login failed.");

Sending Special Characters


To get around this, every time you want to display a special character, you need to encode it. The less-than character (<) is encoded as "<" and the greater-than character (>) as ">". Other special characters are the ampersand (&) and double quotation mark (") characters. You replace the ampersand (&) with "&amp;" and the double quotation marks (") with "&quot;". Additionally, two or more white spaces are always displayed as a single space, unless you convert each individual space to "&nbsp;".

You might also like