0

I have a request (originating from my localhost itself for now) that sends an ajax post request with dataType set to json. On the jsp, this request is parsed and based on some conditions, i do a redirect to another page (that resides on the server).

Now I understand a redirect is only for a resource that exists on a different server. However, even with .forward(), i am facing the same issue described below. My client side (again, this file resides on the server only for now) is:

Somefile.html:

<html>
<head>
<script type="text/javascript"    
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js">
</script>
<script type="text/javascript">

    $(document).ready(function() {


var payload = new Object();

payload.userInfo = new Object();

payload.userInfo.firstName = "Sam";
payload.userInfo.lastName = "Peter";
payload.userInfo.emailAddress = "speter@gmail.com";


payload.accountInfo = new Object();
payload.accountInfo.accountId = "12321";
payload.accountInfo.agentName = "Harry";
payload.accountInfo.terminalId = "1322";

payload.locale = "en-US";
payload.loggedIn = "true";


var payloadjson = JSON.stringify(payload);


        $('#call').click(function ()
        {

            $.ajax({
                type: "post",
                url: "http://localhost:8280/Somepath/FormFiller.jsp", 
    //this is my servlet
        dataType: "json",
                success: function(data){      
                        $('#output').append(data);
                },
        data: {postData:payloadjson}
            });
        });

    });
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<input type="button" value="Call Servlet" name="Call Servlet" id="call"/>
<div id="output"></div>
</body>
</html>

And on the server side: FormFiller.jsp

<%@ page language="java" contentType="application/x-www-form-urlencoded; 
charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

<%@ page import="org.json.simple.JSONObject"%>
<%@ page import="org.json.simple.JSONValue"%>


<%
//response.setHeader("Access-Control-Allow-Origin",     
request.getHeader("Origin"));

request.setCharacterEncoding("utf8");
response.setContentType("application/json");

JSONObject jsonObj = (JSONObject)    
JSONValue.parse(request.getParameter("postData"));

JSONObject userInfo = (JSONObject)   
JSONValue.parse(jsonObj.get("userInfo").toString());

JSONObject accountInfo = (JSONObject) 
JSONValue.parse(jsonObj.get("accountInfo").toString());
String locale = (String) jsonObj.get("locale");
String loggedIn = (String) jsonObj.get("loggedIn");

out.println(userInfo.get("firstName"));
out.println(userInfo.get("lastName"));
out.println(userInfo.get("emailAddress"));

out.println(accountInfo.get("accountId"));
out.println(accountInfo.get("agentName"));
out.println(accountInfo.get("terminalId"));

out.println(locale);
out.println(loggedIn);


if(loggedIn == "false") {

// Redirect to some site

}

else {

out.println(request.getContextPath());
// Redirect to some other site and fill the form
response.sendRedirect("/somepath/xxx/yyy/zzz"); // zzz is some html file
return;
}
%>

Though I get the response (when i look at the developer console on chrome), the response (the redirect page) is not rendered on the browser so i can see all the controls/labels etc visually.

I tried this also but the same issue (not able to see the response on the browser visually).

 getServletContext().getRequestDispatcher("/xxx/yyy/zzz").forward(request,  
    response);

Kindly help.

Santhosh
  • 8,181
  • 4
  • 29
  • 56
bespectacled
  • 2,801
  • 4
  • 25
  • 35

2 Answers2

1

You need to understand the meaning of ajax, It is to

update parts of a web page, without reloading the whole page

So in your case , if you are trying to render or redirect a view(page) . Simply make an http request instead of ajax call.

Incase of partial view , i advise you render it as ,,

 $('#output').html(data);

in your ajax success

Santhosh
  • 8,181
  • 4
  • 29
  • 56
0

So, I think a GET request will suffice in this case. A POST is unnecessary here as we are not creating/editing/deleting anything on the server based on the post request payload. I found this post - How to redirect with post data

if, in case, we necessarily need a POST only here. For my use case, GET suffices. Thanks for all the help.

Community
  • 1
  • 1
bespectacled
  • 2,801
  • 4
  • 25
  • 35