Home >Java >javaTutorial >How to implement partial refresh in jsp

How to implement partial refresh in jsp

(*-*)浩
(*-*)浩Original
2019-05-15 09:42:036393browse

Jsp method to implement partial refresh: first create a jsp file that handles ajax requests; then set the format and character set of the output information; finally use JSP and ajax to implement partial page refresh.

How to implement partial refresh in jsp

With AJAX, JavaScript can communicate directly with the server using JavaScript's XMLHttpRequest object. Through this object, JavaScript can exchange data with the web server without reloading the page.

Recommended course: Java Tutorial.

AJAX uses asynchronous data transfer (HTTP requests) between the browser and the web server, which allows a web page to request small amounts of information from the server rather than the entire page.

In the experiment, JSP ajax was used to automatically refresh the page and read/write data in the database.

The following is a small example of using JSP ajax to achieve partial page refresh:

Jsp file that handles ajax requests: ajax.jsp

<%@ page contentType="text/html; charset=gb2312" %>  
<%  
//这是一个Java程序
//设置输出信息的格式及字符集
response.setContentType("text/xml; charset=UTF-8");
response.setHeader("Cache-Control","no-cache");
out.println("<response>");
for(int i=0;i<2;i++){  
out.println("<name>"+(int)(Math.random()*10)+
"号传感器</name>");
out.println("<count>" +(int)(Math.random()*100)+ "</count>");
}
out.println("</response>");
out.close();
%>

The jsp file that sends the ajax request: zx.jsp

<head>  
    <META http-equiv=Content-Type content="text/html; charset=gb2312">  
    </head>  
    <script language="javascript">  
      
    var XMLHttpReq;  
        //创建XMLHttpRequest对象         
        function createXMLHttpRequest() {  
            if(window.XMLHttpRequest) { //Mozilla 浏览器  
                XMLHttpReq = new XMLHttpRequest();  
            }  
            else if (window.ActiveXObject) { // IE浏览器  
                try {  
                    XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");  
                } catch (e) {  
                    try {  
                        XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");  
                    } catch (e) {}  
                }  
            }  
        }  
        //发送请求函数  
        function sendRequest() {  
            createXMLHttpRequest();  
            var url = "ajax.jsp";  
            XMLHttpReq.open("GET", url, true);  
            XMLHttpReq.onreadystatechange = processResponse;//指定响应函数  
            XMLHttpReq.send(null);  // 发送请求  
        }  
        // 处理返回信息函数  
        function processResponse() {  
            if (XMLHttpReq.readyState == 4) { // 判断对象状态  
                if (XMLHttpReq.status == 200) { // 信息已经成功返回,开始处理信息  
                    DisplayHot();  
                    setTimeout("sendRequest()", 1000);  
                } else { //页面不正常  
                    window.alert("您所请求的页面有异常。");  
                }  
            }  
        }  
        function DisplayHot() {  
            var name = XMLHttpReq.responseXML.getElementsByTagName("name")[0].firstChild.nodeValue;  
            var count = XMLHttpReq.responseXML.getElementsByTagName("count")[0].firstChild.nodeValue;  
            document.getElementById("product").innerHTML = name;      
            document.getElementById("count").innerHTML = count;   
        }  
      
      
    </script>  
      
    <body onload =sendRequest()>  
    <table style="BORDER-COLLAPSE: collapse" borderColor=#111111 cellSpacing=0 cellPadding=0 width=200    bgColor=#f5efe7 border=0>  
      
    <TR>  
       <TD align=middle bgColor=#dbc2b0 height=19 colspan="2"><B>无线传感网</B> </TD>  
    </TR>  
    <tr>  
       <td height="20"> 传感器:</td>  
       <td height="20" id="product"> </td>  
    </tr>  
    <tr>  
       <td height="20">传感器个数:</td>  
       <td height="20" id="count"> </td>  
    </tr>  
    </body>  
    </table>

The effect is as follows (the value on the page changes automatically):


After partial refresh:


The above is the detailed content of How to implement partial refresh in jsp. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What is jsp pageNext article:What is jsp page