0

I have a problem with forwarding to a .jsp page. Let me describe my program:

In Servlet I would like to confirm something so I did .forward() to .jsp where I have included javascript with confirm in $(document).ready(function()...

In case OK was selected I make new XMLHttpRequest() and set a parameter confirm=1 and go back to previous Servlet where a block of code which has to execute after confirmation(confirm=1) is executed as expected. But the very last line of that block which is .forward back to .jsp seems to have no effect at all.

Servlet code:

if("1".equals(request.getParameter("confirm"))){
  ...
   setting some request.setAttribute();
}
else{
  ...
}

RequestDispatcher  rd = request.getRequestDispatcher(...);
rd.forward(req, res);

Actually, I see the correct page, but it's from before confirmation I think. It should have probably been reloaded because I have to take some attributes from request and display some things with jquery, depending on those attributes values.

So what do you think is the cause for .forward() not having effect? Can I force the page somehow to reload?

EDIT: The first time Servlet code is executed(else case) .forward() call works fine. But next time it does nothing, no matter which page I try to forward to. Just like I would have no forward call at all. Strange. I really hope somebody has an explanation for this.

EDIT2: It's not forward problem I guess. If I do system.out.print of needed attributes I get correct values. But why is page not reloading? Even if I forward to different .jsp I still see the previous one.

badunk
  • 4,310
  • 5
  • 27
  • 47
petter386
  • 205
  • 1
  • 5
  • 19
  • http://docs.oracle.com/javaee/5/api/javax/servlet/RequestDispatcher.html#forward%28javax.servlet.ServletRequest,%20javax.servlet.ServletResponse%29 – Hardik Mishra Apr 10 '12 at 08:20
  • Like I said, it's probably not forward causing problems. Or is it? How can I solve this? – petter386 Apr 10 '12 at 08:49
  • provide code which makes AJAX Servlet call – Hardik Mishra Apr 10 '12 at 10:03
  • I solved my problem with JSON, so I got needed attributes directly to javascript. – petter386 Apr 11 '12 at 08:43
  • Better provide solution here and mark the question as close. It would be helpful to others in future. – Hardik Mishra Apr 11 '12 at 09:01
  • You seem to completely misunderstand how ajax works and is supposed to work. The code which you've there is designed for "normal" (synchronous) requests, not for ajax requests. I'd suggest to invest some more time in learning what ajax exactly is. – BalusC Apr 12 '12 at 03:53
  • I won't say I know everything about ajax but enough for this case I think. The Servlet code I have provided can "execute in two ways" based on a parameter 'confirm'. If it executes if case, then it won't even come to the forward call. There I did JASONObject and write it out. That's what I get in javascript from ajax call. In else case it executes "normally" including forward call which sends you to .jsp and that ajax call which is in $(document).ready(function(){...}); Please tell me if there is any bad practice in my precedure. I would gladly learn something new. Thanks for your answer. – petter386 Apr 12 '12 at 07:51

2 Answers2

0

in confirm case, are you sure you go to the end, and call the forward ?

may be you get a null pointer or something like this.

Try to add system.out.println() or run in a debug mode.

Overnuts
  • 783
  • 5
  • 17
0

I didn't really managed to solve my problem with .jsp page not loading. I actually only needed to pass some attributes from Servlete to javascript so I used jQuery ajax call for PrintWriter so I got those attributes as a key:value pairs in javascript function. Then I simply used javascript to set those values to some spans.

petter386
  • 205
  • 1
  • 5
  • 19