下面是编程之家 jb51.cc 通过网络收集整理的代码片段。
编程之家小编现在分享给大家,也给大家做个参考。
//ajax类库 function ajax(json){ var type=json.type||'GET'; var url=json.url; var success=json.success; var error=json.error||false; var data=json.data||""; //1、创建AJAX对象 var OAjax; try{ //Firefox,Opera 8.0+,Safari OAjax=new XMLHttpRequest(); }catch(e){ //Internet Explorer try{ OAjax=new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){ try{ OAjax=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){ alert("你的浏览器不支持AJAX!"); return false; } } } //2、连接服务器 OAjax.open(type,url,true); //3、发送请求 OAjax.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8"); OAjax.send(data); //4、接受数据 OAjax.onreadystatechange=function(){ if(OAjax.readyState==4){ if(OAjax.status==200){ var data=OAjax.responseText; success(data); }else{ if(error){ error(); } } } } } //显示页面 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'page.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"> <style type="text/css"> body{margin:0;padding:0;} ul,li{margin:0;padding:0;list-style:none;} #main{width:940px;height:400px;border:1px solid #d8d8d8;Box-shadow:0px 0px 5px;margin:0 auto;} #main ul{width:900px;height:300px;padding:10px 20px;} #main ul li{width:100%;height:40px;line-height:40px;border-bottom:1px solid #d8d8d8;margin-bottom:3px;} #page{width:100%;height:40px;line-height:60px;text-align:center;} #page a{width:130px;height:40px;line-height:40px;display:inline-block;text-align:center;border:1px solid #ddd;} #page a:link,#page a:visited{text-decoration:none;} </style> <script type="text/javascript" src="./js/ajax1.js"></script> <script type="text/javascript"> window.onload=function(){ var liNodes=document.getElementById('main').getElementsByTagName('li'); var pageNode=document.getElementById('page'); var aNodes=pageNode.getElementsByTagName('a'); for(var i=0;i<aNodes.length;i++){ aNodes[i].onclick=function(){ var url=this.href; ajax({ type:'GET',url:url,success:function(data){ var data=eval("("+data+")"); for(var i=0;i<data.length;i++){ liNodes[i].innerHTML=data[i]; } },error:function(){ alert("request error"); } }); return false; } } } </script> </head> <body> <div id="main"> <ul> <li>afdsafdsafdsafdsafdsafdsafdsafdsa</li> <li>afdsafdsafdsafdsafdsafdsafdsafdsa</li> <li>afdsafdsafdsafdsafdsafdsafdsafdsa</li> <li>afdsafdsafdsafdsafdsafdsafdsafdsa</li> <li>afdsafdsafdsafdsafdsafdsafdsafdsa</li> <li>afdsafdsafdsafdsafdsafdsafdsafdsa</li> </ul> <div id="page"> <a href="${pageContext.request.contextPath}/doPageServlet?page=1">1</a> <a href="${pageContext.request.contextPath}/doPageServlet?page=2">2</a> <a href="${pageContext.request.contextPath}/doPageServlet?page=3">3</a> </div> </div> </body> </html> //ajax数据处理程序 package dcr.service.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONArray; public class doPageServlet extends HttpServlet { public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html"); int num=Integer.parseInt(request.getParameter("page")); PrintWriter writer=response.getWriter(); int pagesize=6; int start=(num-1)*pagesize; int end=start+pagesize; System.out.println(start+":"+end); String[] data={"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb","bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",}; ArrayList dataList=new ArrayList<String>(); for(int i=start;i<end;i++){ dataList.add(data[i]); } System.out.println(dataList.toString()); JSONArray jsonArray=JSONArray.fromObject(dataList); System.out.println(jsonArray.toString()); writer.write(jsonArray.toString()); writer.flush(); writer.close(); } public void doPost(HttpServletRequest request,IOException { doGet(request,response); } } 用到的jar json-lib-2.4-jdk15.jar commons-beanutils.jar commons-collections.jar commons-lang.jar commons-logging.jar ezmorph-1.0.6.jar
以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
原文链接:https://www.f2er.com/html/457408.html