ajax 的一个问题

zcqzcq 2008-05-30 11:31:49
tr.aapendChild(td); 老是说这句话报错. 还有从数据库取出的中文 显示是???
大家试下代码就知道问题出在哪里了

index.jsp

<%@ page contentType="text/html; charset=gbk" language="java" import="java.util.*" %>

<!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)
{

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;

table(zcq1);

document.getElementById('sp').innerHTML=" <font color='#00FF66'>加载成功</font> "
}

}
}
else
{
document.getElementById('sp').innerHTML="<font color='red'>加载中</font>"
}

}
function table(txt)
{

var tb=document.getElementById('tb');
tb.setAttribute('border','1');

var tr=document.createElement('tr');


var td=document.createElement('td');
var tn=document.createTextNode(txt);


td.appendChild(tn);
tr.aapendChild(td);

tb.appendChild(tr);
}

</script>
</head>

<body>
<input type="text" id="zcq" name="name" />
<input type="text" id="un" name="name" />
<input type="button" value="dianji" onclick="asd()" />
<span id="sp"></span>


<span id="aaa"></span>
<table id="tb" >


</table>
</body>
</html>






Test.java


package 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("</zcqzcq>");

}

buff.append("</b>");
dao.closeSession();

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
}

}


...全文
80 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
tavor 2008-05-30
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zcq87642231 的回复:]
oracle 的编码是什么是
[/Quote]
呵呵,这个要看你建库时用的啥,
zcqzcq 2008-05-30
  • 打赏
  • 举报
回复
oracle 的编码是什么是
tavor 2008-05-30
  • 打赏
  • 举报
回复
乱码的问题,要看你数据用的编码方式和你页面上的编码方式和一致呀,不一致的话就在JAVA代码转一下
tavor 2008-05-30
  • 打赏
  • 举报
回复
明显方法写错了,
原先是
 tr.aapendChild(td);

应该是
 tr.appendChild(td);
palm_civet 2008-05-30
  • 打赏
  • 举报
回复
应该是 tr.appendChild(td);
zw2455 2008-05-30
  • 打赏
  • 举报
回复
建议在和数据库交互的时候对数据进行转码,用过滤器过滤一下

67,538

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧