3

This servlet code,here 1st i want to send return message(if message!=null) to the Ajax for alert and 2nd one if message==null i want to call another jsp with pass the list to this jsp.

    if(message!=null){
                response.setContentType("text/plain");
                response.getWriter().write(message);
            }else{
                JSONObject jobj = new JSONObject();
                String urlToRedirect = "SearchEmployee.jsp";
                jobj.put("url",urlToRedirect );
                response.getWriter().write(jobj.toString());
                }

Here i cant understand how to call this jsp url in else part

    $.ajax({
        type:"POST",
        url:'../SearchEmployeeServlet',

        data:$('#DisplayEmployeeListForm').serialize(),//$('form').serialize();
        success:function(msg){
            if(msg!=""){
                alert(msg);
                window.location.reload(true);
            }else{
                ....here i want to call 2nd jsp

            }

        }
    });

2 Answers 2

1

You could just forward the request server-side, to avoid needing the client to make a second request:

request.getRequestDispatcher("SearchEmployee.jsp").forward(request, response);

Or alternatively, you could send an HTTP redirect response, so the client should handle the redirection to the second request automatically:

response.sendRedirect(response.encodeRedirectUrl("SearchEmployee.jsp"));

Note that how you handle this in the JavaScript depends on the datatype you expect to receive from SearchEmployee.jsp. For example, if it is XML and you have set the response content-type to text/xml jQuery will parse it and pass you an XML DOM object to your success function:

success:function(msg) {
    if (msg instanceof XMLDocument) {
        // handle list
    } else {
        alert(msg);
        window.location.reload(true);
    }
}

If you're expecting HTML, this may be returned as a string, so you could test to see if your returned string starts with some HTML. For example, if your HTML will always start with a <ul> tag:

success:function(msg) {
    if (msg.startsWith("<ul>")) { // note you may need to do msg = $.trim(msg) first
        // handle list
    } else {
        alert(msg);
        window.location.reload(true);
    }
}

If you don't want to auto-forward as suggested above and you'd rather stick with your current approach, there are a few things you'll need to change.

Using if(msg!="") in your success function is bad as that will return true as long as the server does not return an empty response (so in both cases you'll get an alert).

The first thing to do is to add a content-type header to your servlet code to indicate you're returning JSON in the second case:

response.setContentType("application/json");
response.getWriter().write(jobj.toString());

Now when jQuery handles the response, it will attempt to parse it as JSON before calling the success function. As such, you can now test in your success function whether the argument jQuery has given you is an object or a string:

success:function(msg){
    if (typeof msg === "object") {
        // you got the JSON response
        $.ajax({
            url: msg.url,
            // etc...
        });
    } else {
        // you got the plain text response
        alert(msg);
        window.location.reload(true);
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

@priyankaahire Sorry, I'm not sure I understand that comment. Regardless, I've edited my answer to explain how to do things the way you're trying to.
From servlet i want 2 response 1st if alert else jsp call with list..I can't understand how to do this ...u give me you own suggestion.I will try with that
@priyankaahire If you want to do it in one request, then you'll need to test the response you get from the server to see whether it was a message to alert or a "list" to handle otherwise. Doing a typeof msg will give you that answer (e.g., if (typeof msg === 'string') { ... } else { ... }).
I've added some more detail about how to check what data has been returned by the server.
0

Just make the ajax request in else part as you did above.

$.ajax({url: "jsp url", success: function(result){

}});

Find more information here

1 Comment

"result" means, what servlet return i.e url or any thing else because i cant understand if I use this in else part of ajax what i write in servlet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.