Redirecting HttpUrlConnection to another servlet does not occur

I have the following code that will call the server through HttpUrlConnection.

String response = HttpUtil.submitRequest(json.toJSONString(), "http://ipaddr:port/SessionMgr/validateSession?sessionId=_78998348uthjae3a&showLoginPage=true");

The above lines will call the following 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 "";
}

requestUrl will go to the servlet below:

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("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"> \n");
            out.write("<html><head><body onload=\"document.forms[0].submit()\">\n");
            out.write("<form method=\"POST\" action=\"" + actionUrl + "\">\n");
            out.write("<input type=\"hidden\" name=\"locale\" value=\"" + locale + "\"/>\n");
            out.write("<input type=\"hidden\" name=\"Sessionrequest\" value=\"" + true + "\"/>\n");
            out.write("</form>\n</body>\n</html>\n");
     }
}

In the above code, the form should go to the servlet as specified in actionUrl, but it will go back to the servlet, which is in step (1).

1) Can I find out if we can do this above the html form in step (3) to send and redirect to the servlet in actionUrl .

According to the code above, I summarize this requirement. If the session is null, I have to redirect the user to the login page and check it against the database, and then the answer should go to step (1), is this possible?

+4
2

, HttpUrlConnection , HttpUrlConnection :

...
conn.setRequestProperty("User-agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.215 Safari/535.1");
conn.setInstanceFollowRedirects(true);
...

, - , conn .

+3

, setInstanceFollowRedirects(true) , HTTP- HttpURLConnection. , , session null ( - , ).

( ) , HTTP 3xx . :

if (responseStatusCode != HttpURLConnection.HTTP_OK) {
   switch(responseStatusCode){ 
      case HttpURLConnection.HTTP_MOVED_TEMP:
          // handle 302
      case HttpURLConnection.HTTP_MOVED_PERM:
          // handle 301
      case HttpURLConnection.HTTP_SEE_OTHER:                        
          String newUrl = conn.getHeaderField("Location");   // use redirect url from "Location" header field
          String cookies = conn.getHeaderField("Set-Cookie");       // if cookies are needed (i.e. for login)
          // manually redirect using a new connection 
          conn = (HttpURLConnection) new URL(newUrl).openConnection();
          conn.setRequestProperty("Cookie", cookies);
          conn.addRequestProperty("User-agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.215 Safari/535.1"); 
      default:
          // handle default (other) case
   }
}

, , , . ( , HTTP , .)

, JSON lib, json.org, .

+2

All Articles