DAO.javapackage com.zcq.dao;import javax.transaction.Transaction;import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration;import sun.print.resources.serviceui; public class Dao { public Session session=null; public Dao() { Configuration config=new Configuration().configure(); SessionFactory factory=config.buildSessionFactory(); session=factory.openSession();
} public void closeSession() { try { if(session!=null) { session.close(); session=null; } } catch (Exception e) { e.printStackTrace(); } } }Test.javapackage com.zcq.Servlet;import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Iterator; import java.util.List;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import org.hibernate.Query; import org.hibernate.Session;import com.zcq.dao.Asdas; import com.zcq.dao.Dao;public class Test extends HttpServlet { /** * Constructor of the object. */ public Test() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); response.setContentType("text/xml;charset=gbk");
Dao dao=new Dao(); Session session=dao.session; Query q=session.createQuery("from Asdas "); List list=q.list();
/** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here }} index.jsp<%@ page language="java" import="java.util.*" pageEncoding="gbk" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head>
<title>My JSP 'index.jsp' starting page</title> <script type="text/javascript"> var xmlhttp;
xmlhttp.open("post","Test"); xmlhttp.send("action=a"); } function handechange() { if(xmlhttp.readyState==4) { if(xmlhttp.status==200) { clarChilad(); var property=null; var result=xmlhttp.responseXML; var zcqs= result.getElementsByTagName("zcqzcq");
for(var i=0;i<zcqs.length;i++) { property=zcqs[i]; var zcq1=property.getElementsByTagName('zcq')[0].firstChild.nodeValue; var bb=property.getElementsByTagName('bb')[0].firstChild.nodeValue;
我现在这个项目也用AJAX,也在学习
楼主看看下面的帖子也许有帮助
http://blog.csdn.net/voff12/archive/2007/06/03/1636344.aspx
好像是数据量过大的原因。
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;import sun.print.resources.serviceui;
public class Dao {
public Session session=null;
public Dao()
{
Configuration config=new Configuration().configure();
SessionFactory factory=config.buildSessionFactory();
session=factory.openSession();
}
public void closeSession()
{
try {
if(session!=null)
{
session.close();
session=null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}Test.javapackage com.zcq.Servlet;import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.hibernate.Query;
import org.hibernate.Session;import com.zcq.dao.Asdas;
import com.zcq.dao.Dao;public class Test extends HttpServlet { /**
* Constructor of the object.
*/
public Test() {
super();
} /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/xml;charset=gbk");
Dao dao=new Dao();
Session session=dao.session;
Query q=session.createQuery("from Asdas ");
List list=q.list();
StringBuffer buff=new StringBuffer("<b>");
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
Asdas a= (Asdas) iterator.next();
buff.append("<zcqzcq>");
buff.append("<zcq>");
buff.append(a.getZcq());
buff.append("</zcq>");
buff.append("<bb>");
buff.append(a.getUnitno());
buff.append("</bb>");
buff.append("</zcqzcq>");
}
buff.append("</b>");
dao.closeSession();
out.println(buff.toString());
System.out.println(buff.toString());
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doGet(request, response);
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}}
index.jsp<%@ page language="java" import="java.util.*" pageEncoding="gbk" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<script type="text/javascript">
var xmlhttp;
function createHttpRequest()
{
if(window.ActiveXObject)
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
}
function asd()
{
createHttpRequest();
xmlhttp.onreadystatechange=handechange;
xmlhttp.open("post","Test");
xmlhttp.send("action=a");
}
function handechange()
{
if(xmlhttp.readyState==4)
{
if(xmlhttp.status==200)
{
clarChilad();
var property=null;
var result=xmlhttp.responseXML;
var zcqs= result.getElementsByTagName("zcqzcq");
for(var i=0;i<zcqs.length;i++)
{
property=zcqs[i];
var zcq1=property.getElementsByTagName('zcq')[0].firstChild.nodeValue;
var bb=property.getElementsByTagName('bb')[0].firstChild.nodeValue;
table(zcq1,bb);
document.getElementById('sp').innerHTML=" <font color='#00FF66'>加载成功</font> "
}
}
}
else
{
document.getElementById('sp').innerHTML="<font color='red'>加载中..</font> "
}
}
function clarChilad()
{
while(tb.childNodes.length>0)
{
tb.removeChild(tb.childNodes[0]);
}
}function table(txt,txt2)
{
var tb=document.getElementById('tb');
var tr=document.createElement('tr');
var td=document.createElement('td');
var tn=document.createTextNode(txt);
var td2=document.createElement('td');
var tn2=document.createTextNode(txt2);
td.appendChild(tn);
td2.appendChild(tn2);
tr.appendChild(td);
tr.appendChild(td2);
tb.appendChild(tr);
}
</script>
</head>
<body>
<input type="text" id="zcq" name="name" />
<input type="button" value="dianji" onclick="asd()" />
<span id="sp"></span>
<span id="aaa"></span>
<table border="1" >
<tbody id="tb"></tbody>
</table>
</body>
</html>
static SessionFactory factory=null;
private Session session=null; static{
Configuration config=new Configuration().configure();
factory=config.buildSessionFactory(); } public Dao()
{
session=factory.openSession();
}
public Session getSession()
{
return session;
}
public void closeSession()
{
try {
if(session!=null)
{
session.close();
session=null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}Test中
Dao dao=new Dao();
Session session=dao.getSession();
这不是AJAX的问题,而是你的c3p0配置有问题
只配置了一个最大连接数
但没有配置自动关闭连接