majji naidu

Greenhorn
+ Follow
since Mar 12, 2015
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by majji naidu

send redirect response code:
response.sendRedirect("/actionUrl");

for request dispatcher:

RequestDispatcher dispatcher = request.getRequestDispatcher("/validateform.jsp");
dispatcher.forward(req,resp);

My expectation is:
control should go to the next path which is specified in actionUrl but it goes back to the from where the request got initialized.

Is it possible to do like that(as per my expectation)
9 years ago
Yes i tried it with "Requestdispatcher", "sendRedirect", but it does not work. I don't know whether the redirection is possible or not.
9 years ago
The problem with the code is:

We have to set content type before sending response in a servlet:

response.setContentType("text/html");
9 years ago
I have the following code which will call the server through HttpUrlConnection.



1)


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

The above lines will call the following code:

2)

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 "";
}

The "requestUrl" will go to the servlet below:

3)
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 mentioned in the actionUrl, but it is again going to servlet which is in step(1).

1) May i know can we make this above html form in step(3) to submitted and redirect to the servlet in **actionUrl**.

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?
9 years ago
I have written sample web application with servlets and jsp. As per the below code the action url should be executed automatically
according to the "onload". I have written this below code in one of my servlet, The below "onload" form hits the path in action
only in Firefox and IE but not in Chrome(41.0.2272.74 beta-m (64-bit)).I have seen the link [Chrome - <body onload=“”> is not working ][1] but i did not get proper idea, I have my below code in the end of servelt. there is nothing to execute after this code in the servlet.

out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"> \n");
out.write("<html><body onload=\"alert('alert has been fired');document.forms[0].submit()\">\n");
out.write("<form method=\"POST\" action=\"" + actionUrl + "\">\n");
out.write("<input type=\"hidden\" name=\"resourcePath\" value=\"" + callbackUrl + "\"/>\n");
out.write("<input type=\"hidden\" name=\"locale\" value=\"" + locale + "\"/>\n");
out.write("<input type=\"hidden\" name=\"rspjRequest\" value=\"" + true + "\"/>\n");
out.write("</form>\n</body>\n</html>\n");
out.close();

Is there any reserved key words of chrome in the above action url?

If i execute this code in the html format, it is working in chrome. but if it is from servlet, alert() is not running.
9 years ago