1

I am trying to get JSON response which has been set in servlet to a JSP page.

JSP page:

<script>
$(document).ready(function(){
     $("#submitBut").click(function(){
         var formData=getFormData();
         var strUrl="rwcntrlr.do?action=loginForm";
         $.post(strUrl, {jsonData: JSON.stringify(formData)},function(response){
                response = jQuery.parseJSON( response);
                if(response.message=='not ok')
                {
                    alert("not ok");
                }
                else{
                    alert('OK');
                }
            });
    }); 
});
</script>

Servlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String formName=request.getParameter("action");
        if(formName.equalsIgnoreCase("loginForm")){
            String strJSONData = request.getParameter("jsonData");
            System.out.println(strJSONData);// data received correctly...
            JSONObject jsonResponse = new JSONObject();
            
            try{
                JSONObject requestedJSONObject = new JSONObject(strJSONData);
                String login_name = requestedJSONObject.getString("login_name");
                String password = requestedJSONObject.getString("password");
                if(login_name.equalsIgnoreCase("mark")){
                    response.setContentType("application/json");
                    jsonResponse.put("status", "OK");
                    response.getWriter().write(jsonResponse.toString());
                    }
                    else{
                        response.setContentType("application/json");
                        jsonResponse.put("status", "NOT OK");                       
                        response.getWriter().write(jsonResponse.toString());
                    }
            }
            catch(Exception ex){
                ex.printStackTrace();
            }           
        }
    }

in above code I am able to get form data in a JSON pattern in my servlet controller.

But I am not able show valid alert boxes back to JSP page based on JSON type message sent by servlet as response.

Roman C
  • 49,761
  • 33
  • 66
  • 176
JPG
  • 1,247
  • 5
  • 31
  • 64
  • To get json from servlet to jsp you can see this link http://www.ebhor.com/servlet-json-response/ – xrcwrn Dec 26 '18 at 10:00

2 Answers2

1

To write to the response you should use something like

response.setContentType("application/json");
response.setHeader("cache-control", "no-cache");
PrintWriter out = response.getWriter();

out.println(jsonResponse.toString());
out.flush();

Note, cache control usually prevents the browser from caching the value. And your text written to the out is too small to fill the buffer. Flush the buffer before returning the response.If you set the content type "application/json" you don't need to parse object, unless it converted to text by jQuery. Use dataType: 'json' option to get the JSON object.

$.post(strUrl, {jsonData: JSON.stringify(formData)},
  function(response){
    if(response.status=='NOT OK') {
      alert("not ok");
    } else {
      alert('OK');
    }
  }, 'json');
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Explicit `flush()` is unnecessary: http://stackoverflow.com/q/5043657 Explicit `dataType` is unnecessary: http://stackoverflow.com/q/30150148 Explicit `cache-control` is unnecessary on POST in a default configured server: http://www.faqs.org/rfcs/rfc2616.html – BalusC Feb 07 '16 at 19:42
0

Could it be that

if(response.message=='not ok')

should be

if(response.status=='not ok')

since you put

jsonResponse.put("status", "NOT OK");
  • sharp observation. But, still no response. – JPG Feb 07 '16 at 16:51
  • If you put a breakpoint on if(response.message=='not ok') your can examine the contents of the response, that could tell you what is send back. Perhaps you should use a === in stead of just == . – Mirko Pelgrom Feb 08 '16 at 08:57