3

I have servlet which does a forward using requestdispatcher() to one of my jsp pages

request.getRequestDispatcher("/WEB-INF/view/mypage.jsp").forward(
                    request, response);

The target mypage.jsp has some jquery goodness which required to do some show/hide stuff but for some reason the jquery is not being triggered when the jsp is displayed after being forwarded from the servlet, but it works when I directly access the page via the address bar(I placed the page outside the web-inf to access is directly).

Even a simple <body onload="alert('Testing')"> is also not working. I'm also using HTML5 and jquerymobile in my jsp.

Any replies to fill-up the gaps in my knowledge would be great. Thanks

Following is my jsp page :

<!DOCTYPE html>
<html>
<head>
<title>My App</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css" />
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js"></script>
</head>
<body onload="alert('Even this is not popping up')">
-- Stuff--
</body>
</html>
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js"></script>
</head>
<body onload="alert('Even this is not popping up')">
-- Stuff--
</body>
</html>
Vic
  • 336
  • 6
  • 18

5 Answers5

1

The problem could be in the way you are accessing mypage.jsp. Since it's inside the /WEB-INF folder, it is not directly accessible from a browser. I don't thinks you could directly access the page via the address bar if it's still inside /WEB-INF folder.

If you are accessing it from outside /WEB-INF and it works but after your servlet forwards request to it, it doesn't, the most probable reason is your JSP page tries to load something after it's been sent to the browser and the server refuses to serve it - perhaps that resource is also inside /WEB/INF. You may also find this link helpful.

Community
  • 1
  • 1
dragon66
  • 2,645
  • 2
  • 21
  • 43
  • thanks for the reply, I placed the page outside the web-inf to access is directly (updated in the question as well) and actually my jsp page is trying to access some scripts and css which are in the web-inf... will see if that is the problem and update – Vic May 10 '12 at 05:04
  • So I changed all paths to my scripts/css to access them directly over the web but its still not working... also if i don't use the jquerymobile script, the javascript works... can this be a problem... or is there some other larger issue ? – Vic May 10 '12 at 09:46
  • I removed the redundant part of your posted JSP (still kept the two JavaScripts untouched), tested forwarding request to it from a Servlet on my machine. It works perfect in both cases whether I put the JSP inside /WEB-INF or directly at the root of the application. – dragon66 May 10 '12 at 13:14
0

I think you'll find the problem if you access the page and view source when accessing directly and indirectly. Use a file diff tool to compare the two outputs. Most likely you have either not included some JavaScript in one of the versions, or the relative paths to the jquery.js library is incorrect when you access indirectly.

Brad
  • 15,186
  • 11
  • 60
  • 74
0

RequestDispatcher is a redirect that works similar to how include works, so your pages may be being served but the static files will not be accessible since they are requested by front end.

Move your js files out of WEB-INF, css and images too.

kishu27
  • 3,140
  • 2
  • 26
  • 41
0

apparently its because of jquerymobile's ajax navigation model

http://jquerymobile.com/test/docs/pages/page-scripting.html

Vic
  • 336
  • 6
  • 18
0

Use like this

window.alert("This is alert");

window.document.location.href="mypage.html";

On appearing alert when you click on ok button you will be redirected to your desired page.