ajax分页实现,感觉跟我想的有点差距,难道是我错了

来源:互联网 发布:ubuntu启动黑屏 编辑:程序博客网 时间:2024/05/16 18:17
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
System.out.println("tttt");
PrintWriter out = response.getWriter();
request.setAttribute("abc", "aaa");
         out.print("123456789");
         out.close();

}

纠结之处在于,实现局部刷新,这个没问题,可是如果分页,显示list不是要out.print很多个,会不会影响性能,起码,代码量变多了。

能不能输出一个对象(arraylist对象),然后使用对象局部刷新,这样,只需request.setAttribute("name",<list>).


<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'C.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
var xmlHttp;
        //创建XMLhttpRequest对象
        function createXMLHttpRequest() {
            if (window.ActiveXObject) {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            else if (window.XMLHttpRequest) {
                xmlHttp = new XMLHttpRequest();
            }
        }
        //点击按钮响应的Get方法主函数
        //Get方法把参数值以名=值方式在xmlHttp.open("GET", queryString, true)中传递,queryString的形式为URL?参数名=值&参数名=值...;而xmlHttp.send(null);
        function doRequestUsingGET() {
            createXMLHttpRequest();//第一步:创建XMLHttpRequest对象
            var queryString = "C";
           alert("1");
            + "&timeStamp=" + new Date().getTime();//第二步:定义传递的参数值字符串
            xmlHttp.open("GET", queryString, true);//第三步:建立与服务器的请求
            xmlHttp.onreadystatechange = handleStateChange;//第四步:监听状态-->转到监听状态函数    
            xmlHttp.send(null);//第五步:发送请求,并且立即返回
        }


        //监听状态函数
        function handleStateChange() {
            if(xmlHttp.readyState == 4) {
                if(xmlHttp.status == 200) {
                parseResults();//-->转到函数parseResults输出从服务器返的值
                }
            }
        }


        //在页面显示从服务器传来的结果
       function parseResults() {
            var responseDiv = document.getElementById("zz");
            if(responseDiv.hasChildNodes()) {
            responseDiv.removeChild(responseDiv.childNodes[0]);
        }
        var responseText = document.createTextNode(xmlHttp.responseText);//
        responseDiv.appendChild(responseText);
        }

</script>

  </head>
  
  <body>
  <table>
    This is my JSP page. <br>
    <button type="button" onclick="doRequestUsingGET()">asdasd</button>
    
    <div id="zz">11
    ${abc}11<%=request.getAttribute("abc") %>
    </div>
    </table>
  </body>
</html>

原创粉丝点击