最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Refreshing a DIV element which has JSP code snippet inside - Stack Overflow

programmeradmin4浏览0评论

I have a JSP page where I am reading Session Attributes that I set in the Session.

I want to read the Session attributes in regular intervals. I don't want to reload the whole page instead I am just keeping my JSP Session read attributes in my DIV and trying to reload the DIV. But it is not reloading the DIV.

Here is my code base:

<html>
  <head>
  // Loading CSS and Script files
  </head>
  <body>
    <div id="loadData" style='display:none;'>
         <% 
           String strStatus = String.valueOf(session.getAttribute("Status")) ;
          %>
    </div>
  </body>
  <script type="text/javascript">
    var reqStatus = '<%= strStatus %>';

    $(this).load(function(){
       setInterval(function() {
            $("#loadData").load();
          } ,1000);
     });
     $("#loadData").load(function(){
        if(reqStatus == 'Done') {
        // My Code goes here..
        }
     });
</html>

Any better ideas are also wele.

I have a JSP page where I am reading Session Attributes that I set in the Session.

I want to read the Session attributes in regular intervals. I don't want to reload the whole page instead I am just keeping my JSP Session read attributes in my DIV and trying to reload the DIV. But it is not reloading the DIV.

Here is my code base:

<html>
  <head>
  // Loading CSS and Script files
  </head>
  <body>
    <div id="loadData" style='display:none;'>
         <% 
           String strStatus = String.valueOf(session.getAttribute("Status")) ;
          %>
    </div>
  </body>
  <script type="text/javascript">
    var reqStatus = '<%= strStatus %>';

    $(this).load(function(){
       setInterval(function() {
            $("#loadData").load();
          } ,1000);
     });
     $("#loadData").load(function(){
        if(reqStatus == 'Done') {
        // My Code goes here..
        }
     });
</html>

Any better ideas are also wele.

Share Improve this question edited Dec 1, 2010 at 3:24 Puru asked Nov 30, 2010 at 11:26 PuruPuru 9,11328 gold badges73 silver badges91 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

JSP renders once, on the server, and is then sent to the client, after which the Java code does nothing. You can't put both the HTML/javascript code and the Java code in the same file if you want them to be loaded at different times / frequencies.

Put this into a separate .jsp file:

<%= String.valueOf(session.getAttribute("Status")) ; %>

Assume it's mapped to some url /checkStatus.jsp

Remove the loadData div because you don't need it anymore. Replace your javascript with:

var reloadStatus = function () {
    $.ajax("/checkStatus.jsp", function (data) {
        if (data == "Done") {
            // Your code here
        }
    });
};

setInterval(reloadStatus, 1000);

Your JSP code is evaluated only once -- when the page first loads. When JSP code runs, HTML is generated and sent to the browser. You cannot "reload" a div like that; the JSP code will not run.

What you can do is put the JSP code into a separate filee and then use jQuery.load to load that page into a div:

jQuery(document).ready(function() {
   setInterval(function() {
       jQuery('#loadData').load('/status.jsp');
   }, 1000);
}

status.jsp will contain just the one line:

<%= String.valueOf(session.getAttribute("Status")) ; %>

The code in a JSP is piled/executed before the resulting HTML is sent to the browser. You can't reload part of the page as-rendered and expect it to change. You would probably need to make a hidden iframe and reload that pletely (easy), or make a webservice to query the params (harder).

发布评论

评论列表(0)

  1. 暂无评论