title: Redirecting a request using servlets and the "setHeader" method not working
body: <p>I am new to servlet development, and I was reading an ebook, and found that I can redirect to a different web page using </p>
  
  <pre><code>setHeader("Location", "http://www.google.com")
  </code></pre>
  
  <p>But this is not working, as I have written this code as:</p>
  
  <pre><code>import java.io.*;
  import javax.servlet.*;
  import javax.servlet.http.*;
  
  public class ModHelloWorld extends HttpServlet{
          public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{
  //              response.addHeader("Location", "http://www.google.com");
                  response.setHeader("Location", "http://www.google.com");
                  response.setContentType("text/html");
                  PrintWriter pw = response.getWriter();
                  pw.println("&lt;html&gt;&lt;head&gt;&lt;title&gt;Modified Hello World&lt;/title&gt;&lt;/head&gt;&lt;body&gt;");
                  pw.println("&lt;h1&gt;");
                  //getInitParameter function reads the contents ot init-param elements.
                  pw.println(getInitParameter("message"));
                  pw.println("&lt;/h1&gt;");
                  pw.println("&lt;/body&gt;&lt;/html&gt;");
                  pw.close();
          }
  }
  </code></pre>
  
  <p>i have checked the headers using my program to get the headers of the webpage which is as under:</p>
  
  <pre><code>import java.net.*;
  import java.io.*;
  class getHeaders{
      public static void main(String args[]){
          URL url = null;
          URLConnection urc = null;
          try {
              url = new URL(args[0]);
              urc = url.openConnection();
              for(int i=0 ; ; i++) {
                  String name = urc.getHeaderFieldKey(i);
                  String value = urc.getHeaderField(i);
                  if(name == null &amp;&amp; value == null)//both null so end of header
                      break;
                  else if(name == null){//first line of header{
                      System.out.println("Server HTTP version, Response code: ");
                      System.out.println(value);
                      System.out.println("ENd of first header field");
                  } else {
                      System.out.println("name of header is: " + name + " and its value is : " + value);
                  }
              }
          } catch(MalformedURLException e){
              System.out.println("Malformed URL " + e.getMessage());
          } catch(IOException e){
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>And i am getting the output as:</p>
  
  <pre><code>Server HTTP version, Response code: 
  HTTP/1.1 200 OK
  ENd of first header field
  name of header is: Server and its value is : Apache-Coyote/1.1
  name of header is: Location and its value is : http://www.google.com
  name of header is: Content-Type and its value is : text/html
  name of header is: Content-Length and its value is : 101
  name of header is: Date and its value is : Sat, 05 Mar 2011 15:27:29 GMT
  </code></pre>
  
  <p>But I was not redirected to google's page from my browser.</p>
  
  <p>Thanks in advance:)</p>