Implicit object – Response (2) – Redirect

Go to page using Redirect

One of the most used features of response is Redirect feature.

What is Redirect? It is Web server telling web browser to go to another page.

For example, there are many pages where after successful login, it automatically goes to main page. After running a specific page like login page, you can use redirect feature to automatically go to another specified page.


  • Web browser sends request to a.jsp
  • a. jsp tells web browser to redirect to b.jsp
  • web browser requests b.jsp

Likewise, redirect is web server telling web browser to go to specified page. So if there are a.jsp and b.jsp like above, web browser is substantially requesting twice.

Response can direct the web browser to be redirected by using the following method.

  • response.sendRedirect(String location)

response.sendRedirect() method is mainly used in the following form.

Snip20150516_2

For example, if you wish to move to first page after logging in, you can use the sendRedirect() method as follows:

Snip20150516_5

After running a web browser, enter this URL.

http://localhost:8080/jsp_practice/login.jsp?memberId=madv

Snip20150516_6

On the other hand, if the value of parameter entered in memberId is equal to “madvirus”, it will move to /jsp_practice and output index.jsp.

Here is index.jsp.

Snip20150516_7

On the other hand, if the value of the parameter entered in memberId “madvirus” The results of this screen are generated in the index.jsp / jsp_practice as follows: Go to /jsp_practice/index.jsp will be output.

http://localhost:8080/jsp_practice/login.jsp?memberId=madvirus

Snip20150516_8

Our example has web browser to go to a page located on the same server ,but we can also make it to move to a different server. For example, if I want it to redirect to my blog, just type the full URL as follows:
response.sendRedirect(“https://minjookwon.wordpress.com”);

Earlier, I said the value of parameter that is send to the web server has to be properly encoded. In other words, in order to include something other than alphabet, numbers, and letters to URL, we need to encode it. response.sendRedirect() method also requires proper encoding. For example, let’s say you want to have a value of parameter named “name” to be ‘자바’. (Means Java in Korean).  In this case, you must enter a URL in the form of encoding ‘자바’.

/jsp_practice/index.jsp?name=%C05DA%B9%D9

If programmer have to work to change ‘자바’ to ‘% C05DA% B9% D9’, it will be pain the ass. Fortuantely, there is a java.net.URLEncoder class that does this for us. We can use URLEncoder.encode() method to encode value of parameter with specified character set. The following example illustrates the use of URLEncoder.encode().

Snip20150516_11

Try to run http://localhost:8080/jsp_practice/redirectEncodingTest.jsp

Snip20150516_12

Implicit object – Response (1)

Response implicit object does the opposite of what request does. While request contains request information sent by web browser, response contains response information sent to web browser.

Functions response implicit object provides is as follows.

  • Enter header information
  • Redirect

Although there are a few more features in addition to this, they are rarely used in JSP page.

Sending header information to web browser

Request provides methods that read header information. On the other hand, response provides methods that add headers to response information. The methods related to response method are as follows.

  • addDateHeader(String name, long date)
    • return void
    • Add date to “name” header. It shows time if flew since January 1, 1970 in miliseconds.
  • addHeader(String name, String value)
    • return void
    • add “value” to “name” header.
  • addIntHeader(String name, int value)
    • return void
    • add int “value” to the “name” header.
  • setDateHeader(String name, long date)
    • return void
    • Set value of “name” header to long “date”.
  • setHeader(String name, String value)
    • return void
    • set value of “name” header to string “value”.
  • setIntHeader(String name, int value)
    • return void
    • set value of “name” header to “int” value.
  • containsHeader(String name)
    • return boolean
    • If it contains header named “name”, return true. If not, return false.

Methods starting with “add” are used to add new value to an existing header. Methods starting with “set” are used to specify the value of new header.

Entering response header for cache control

When developing a web application using JSP, output printed to the web browser sometimes does not change, even though new contents are addd to DB. In fact, one of the reasons server doesn’t show the changes is because web browser prints data stored in cache rather than the actual result server created.

Websites that rarely change can provide faster response using web browser cache, but websites that do change a lot like Facebook, if web server uses data stored in cache, users can’t see the changes, so it can be thought of it as the website is not working well. For example, if timeline is cached, newly added post might not show up at timeline.

HTTP has control on deciding whether to cache the web browser, using unique response header. Headers associated with this is as follows.

  • Cache-Control response header
    • supported by HTTP 1.1 version.
    • If you specify the value of this header to “no-cache”, web browser will not save the response result in the cache.
  • Pragma response header
    • supported by HTTP 1.0 version
    • If you specify the value of this header to “no-cache”, web browser will not store the response result in the cache.

Currently used web browser such as Internet Explorer or Firefox9 supports HTTP 1.1, so you can set the value of Cache-control to “no-cache” to make it not save the response result in cache. However, there could be web browsers supporting HTTP 1.0, so it is good to set “no-cache” to both cache-control and Pragma header as shown in the following code.

Snip20150516_1

Expires response header in above code is a HTTP 1.0 response header entered when you specify an expiration date for response result. The expiry time has to be in milliseconds after January 1, 1970. If it shouldn’t be cached, then you enter past time such as 0 or 1.