0

i have some.jsp file which also contains javascript/jquery. i need to do below check in javascript part of some.jsp file.based on the condition i need to display a message/ forward the request to servlet which in turn servlet forwards to other.jsp.

I wrote below code:

<script type="text/javascript">
$(document).ready(function() {
    var isInIframe = (parent !== window);
    if (!isInIframe) {
        $.ajax({
            type: "GET",
            url: "./screenInitializer",
            success: function() {},
            error: function(ob,errStr) {
                alert("Failed. Try Again.","error");
            }
        }); 
    }else{
        $("#accessDenied").text('Access Denied');
    }
});
</script>

I can call a servlet using jquery ajax. forward request to jsp is not working.it is displaying a blank page instead of other.jsp. forwards request to other.jsp as below.

//Keeps some values using request.setAttribute() then forward the request
request.getRequestDispatcher("/WEB-INF/other.jsp").forward(request, response);

Basically i dont want any response from ajax call. it just need to call the servlet which in turn servlet forwards to the jsp and displays other.jsp.all My jsps are in WEB-INF folder. Am i doing anything wrong?

Thanks!

user755806
  • 6,565
  • 27
  • 106
  • 153
  • 2
    Uh, the result of the forward is returned as ajax response, but you're completely ignoring that part (your `success` handler is an empty function). You seem to be somehow expecting that an ajax response is then interpreted as a non-ajax response. Why? Why not just sending a synchronous (non-ajax) request using plain vanilla JS `window.location` in first place if you don't want an ajax response? – BalusC Sep 16 '13 at 14:49
  • yes..i dont want the response back...anyway other.jsp has to display the page... – user755806 Sep 16 '13 at 14:53
  • 1
    Uh, you're now yet more contradicting yourself. You said that you don't want the response back, how would the client then ever know about the contents of `other.jsp` and that it has to display it? I strongly recommend to take a pause with JSP/jQuery/Ajax and take some time apart to learn some basic web development concepts such as HTTP and "server" and "client". – BalusC Sep 16 '13 at 14:56
  • I mean....once the servlet is called, servlet itself forwards to other.jsp.i dont want any response coming back as part of ajax call..Please suggest me any approach.. – user755806 Sep 16 '13 at 14:58
  • I know what you mean, but the way how you describe the requirement and think how things work is completely wrong. In any case, just use `window.location`. – BalusC Sep 16 '13 at 14:59
  • @user755806 You have to use the response on the client side (in the javascript where your ajax is being called) to actually do the redirect somehow. That or make it a plain link. – Izkata Sep 16 '13 at 15:00
  • Could you please suggest me any approach. I mean flow? – user755806 Sep 16 '13 at 15:00
  • @Balu could you please give me some idea on how window.location will solve my issue? – user755806 Sep 16 '13 at 15:01
  • Could some one please tell me the approach that i need to follow to solve my issue? – user755806 Sep 16 '13 at 15:10
  • Look through [this](http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call) for a possible approach. P.S: Answered the question too prematurely, wasn't thinking straight! – sErVerdevIL Sep 16 '13 at 15:23
  • @user755806 Exactly as BalusC said, stop and read up on the difference between "client" and "server" code. Then look at how `window.location` works. You seem to be misunderstanding some basic fundamentals, and without them we can't help you effectively. – Izkata Sep 16 '13 at 15:24
  • @lzkata on success of ajax call can i do window.location="/other.jsp" ? will it work? – user755806 Sep 16 '13 at 15:27
  • You can do window.location whenever you please!! – sErVerdevIL Sep 16 '13 at 15:28
  • Will it work even if the jsp is inside WEB-INF folder Please suggest me.. – user755806 Sep 16 '13 at 15:29
  • No. `jsp` inside `WEB-INF` cannot be accessed directly like you want. A possible solution would be write a controller servlet to redirect such requests. Apart from that you seem to be expecting to find a ready solution without trying stuff. – sErVerdevIL Sep 16 '13 at 15:48
  • @sErVerdevIL: note that OP is already using a servlet for that. It's exactly that servlet as the OP specified in ajax URL. Just use exactly that URL in `window.location`. – BalusC Sep 16 '13 at 15:55
  • @Balu am already calling a servlet. again why do i need to call the same servlet? Please suggest me.. – user755806 Sep 16 '13 at 16:02
  • Uh, the way how you called it is wrong as per your requirement. You said that you don't want an ajax response, but you apparently want a synchronous response (even though you ignorantly keep arguing that you don't need a response at all). In that case, you should fire a synchronous request on servlet's URL using `window.location` instead of an asynchronous (ajax) request servlet's URL using `$.ajax`. On a synchronous request you will (obviously) get a synchronous response. – BalusC Sep 16 '13 at 16:03
  • @Baluc could you please give me some code snippet to understand.Thanks for ur help... – user755806 Sep 16 '13 at 16:07

0 Answers0