3
votes

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>
5
Highly unusual. What happens when you open mypage.jsp directly? - kishu27
Also, post the codes of mypage.jsp - kishu27
it works perfectly when accessed directly... - Vic
Can you post the codes of mypage.jsp - kishu27
You can't access /WEB-INF directly from web browser - Hardik Mishra

5 Answers

1
votes

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.

0
votes

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.

0
votes

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.

0
votes

apparently its because of jquerymobile's ajax navigation model

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

0
votes

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.