title: HttpUrlConnection redirection to other servlet is not happening
body: <p>I have the following code which will call the server through <code>HttpUrlConnection</code>.</p>
  
  <pre><code>String response = HttpUtil.submitRequest(json.toJSONString(), "http://ipaddr:port/SessionMgr/validateSession?sessionId=_78998348uthjae3a&amp;showLoginPage=true");
  </code></pre>
  
  <p>The above lines will call the following code:</p>
  
  <pre><code>public static String submitRequest(String request, String **requestUrl**) {
      try {
          URL url = new URL(requestUrl);
          HttpURLConnection conn = (HttpURLConnection) url.openConnection();
          conn.setDoOutput(true);
          conn.setRequestMethod("POST");
          conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
          OutputStream os = conn.getOutputStream();
          os.write(request.getBytes());
          os.flush();
          if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
              throw new RuntimeException("Failed : HTTP error code : "
                      + conn.getResponseCode());
          }
  
          BufferedReader br = new BufferedReader(new InputStreamReader(
                  (conn.getInputStream())));
          String output;
          StringBuffer sb = new StringBuffer();
          while ((output = br.readLine()) != null) {
              sb.append(output);
          }
          conn.disconnect();
          return sb.toString();
  
      } catch (MalformedURLException e) {
      } catch (IOException e) {
      }
      return "";
  }
  </code></pre>
  
  <p>The <code>requestUrl</code> will go to the servlet below:</p>
  
  <pre><code>public class ValidateSessionServlet extends HttpServlet {
      String session = req.getParameter(sessionId);
      if (session == null) {
      // redirect to servlet which will display login page.
      response.setContentType("text/html");
              String actionUrl = getIpPortUrl(request)
                              + PropertyConfig.getInstance().getFromIdPConfig(globalStrings.getCheckSSOSession());
              out.write("&lt;!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"&gt; \n");
              out.write("&lt;html&gt;&lt;head&gt;&lt;body onload=\"document.forms[0].submit()\"&gt;\n");
              out.write("&lt;form method=\"POST\" action=\"" + actionUrl + "\"&gt;\n");
              out.write("&lt;input type=\"hidden\" name=\"locale\" value=\"" + locale + "\"/&gt;\n");
              out.write("&lt;input type=\"hidden\" name=\"Sessionrequest\" value=\"" + true + "\"/&gt;\n");
              out.write("&lt;/form&gt;\n&lt;/body&gt;\n&lt;/html&gt;\n");
       }
  }
  </code></pre>
  
  <p>In the above code the form should go to the servlet as mentioned in the actionUrl, but it is again going to servlet which is in step(1).</p>
  
  <p>1) May i know can we make this above html form in step(3) to submitted and redirect to the servlet in <strong>actionUrl</strong>. </p>
  
  <p>As per the above code i am summarizing the requirement. If the session is null, I have to redirect the user to login page and validated against database and then the response should go to step(1), Is it possible? </p>