It's not a secret anymore!*
The moose likes JSP and the fly likes Refresh Child Page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Refresh Child Page" Watch "Refresh Child Page" New topic
Author

Refresh Child Page

Mahi Ranga
Ranch Hand

Joined: Jan 27, 2011
Posts: 35
Hi,

I have 2 Jsp Pages.
1.Parent.jsp
2.Child.jsp

Here i want to refresh Child.jsp from Parent.jsp.

Regards,
Eswara.L
Alex Theedom
Ranch Hand

Joined: Jan 18, 2012
Posts: 48

JSPs are servlets that execute on the server. The response from a JSP (which could be HTML) is sent to a browser and displayed. This is a typical (but not the only) scenario.

What is the functionality that you wish to implement? Would you post you attempt so far!


Alex Theedom - Senior Java EE Developer (OCPJP/OCPJWCD)
Mahi Ranga
Ranch Hand

Joined: Jan 27, 2011
Posts: 35
Hi,

Here I am Posting 2 files.I want to display child incremented value in parent page every 1 sec.


Parent.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Parent JSP</title>
<script type="text/javascript" src="../js/jquery.min.js"></script>

<script type="text/javascript">
var url = 'Child.jsp'
$(document).ready(function() {
alert("1");
setInterval(function() {$("#load_me").load(url); }, 1000);
alert("2");
});
</script>
</head>
<body>
<div id="load_me">
<%=session.getAttribute("JVal")%>
</div>
<%! int i = 0;%>
<% i++;%>
I Value :<%=i%>
</body>
</html>


Child.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Child JSP</title>
</head>
<%! int j = 0;%>
<body>
<h1>Child JSP!</h1>
<%
System.out.println(j++);
session.setAttribute("JVal", j);
%>
<%--<jsp:forward page="Parent.jsp"></jsp:forward>--%>
</body>
</html>



Regards,
Eswara.L
Alex Theedom
Ranch Hand

Joined: Jan 18, 2012
Posts: 48

This is what your code is doing.

1. The parent page sets up a timer that calls the child.jsp every second
2. The child.jsp increments the value of j by one and returns HTML code containing the incremented value
3. The jQuery .load() replaces the <div id="load_me"> ... </div> with the HTML generated by child.jsp
4. Repeats every second.

There are few comments to make with regard to the code and some things that don't make sense.

- The session variable JVal should be set first in the parent not the child.
- Its not clear why you have are storing the value of i in the session, it is not retrieved again by parent.jsp as this page in not being reloaded.
- perhaps to maintain this value so that it can be incremented. For this you would need to get the session value before incrementing, which you are not doing.
- The code



will not be evaluated again unless the parent page is reloaded, which is not what you are doing.
- The Child.jsp returns a complete HTML page when all you need is the new value. This can be better achieved with a servlet.

Something like this:



Try to rewrite the code a bit and let me know what happens.

PS: When posting code use encoding to make the code easier to read. Thanks.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Refresh Child Page
 
Similar Threads
Updating parent JSP(without a page refresh) on pop-up action
refresh parent page from child page in ModalDialog
updated data from child, refresh parent ,how to?
navigating from one page (which has a jsp:include) to another page in jsf
Error in JSP[Unexpected Error]