不知道怎么出不来,又不报错
执行到JSONObject jsonobj=new JSONObject(data.toString());时后面的就不执行了!!
大虾帮忙看下Java文件(servlet):
PostData.javaimport java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.json.JSONObject;public class PostData extends HttpServlet
{
protected void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
doPost(req,res);
}
protected void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
StringBuffer data=new StringBuffer();
String temp=null;
try
{
req.setCharacterEncoding("UTF-8");
res.setCharacterEncoding("UTF-8");
BufferedReader reade=req.getReader();
while((temp=reade.readLine())!=null)
{
data.append(temp);
}
System.out.println(temp);
JSONObject jsonobj=new JSONObject(data.toString());
System.out.println("ok");
res.setContentType("text/xml");
res.getWriter().print("您的信息: 名字:"+jsonobj.getString("name")+",年龄:"+jsonobj.getInt("age")+",性别:"+jsonobj.getString("sex")+",喜欢的颜色:"+jsonobj.getString("loveColor")+".");
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}HTML文件 test.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>动态查询显示结果
</title>
<script type="text/javascript" src="json.js"></script>
<script type="text/javascript">
var xmlHttp;
function createXMLHTTPRequest()
{
if(window.ActiveXObject)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp=new XMLHttpRequest();
}
}
function doJson()
{
createXMLHTTPRequest();
var url="PostData?timeStamp="+(new Date()).getTime();
//var json=JSON.stringify(new People("唐俊","21","男","蓝色"));
var carAsJson=JSON.stringify(new People("aa","21","b","blue"));
//alert(JSON.stringify(new People("唐俊","21","男","蓝色")));
xmlHttp.open("POST",url,true);
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
xmlHttp.send(carAsJson);
}
function handleStateChange()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
alert(xmlHttp.responseText);
document.getElementById("selectdata").innerHTML=xmlHttp.responseText;
}
}
}
function People(name,age,sex,loveColor)
{
this.name=name;
this.age=age;
this.sex=sex;
this.loveColor=loveColor;
}
</script>
</head>
<body>
<form action="#">
<table>
<tr>
<td>姓名:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="text" name="age"></td>
</tr>
<tr>
<td>性别:</td>
<td><input type="text" name="sex"></td>
</tr>
<tr>
<td>你喜欢的颜色:</td>
<td><input type="text" name="loveColor"></td>
</tr>
</table>
<input type="button" value="GetData" onclick="doJson();"/><br><br>
<div id="selectdata"></div>
</form>
</body>
</html>多谢了!
执行到JSONObject jsonobj=new JSONObject(data.toString());时后面的就不执行了!!
大虾帮忙看下Java文件(servlet):
PostData.javaimport java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.json.JSONObject;public class PostData extends HttpServlet
{
protected void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
doPost(req,res);
}
protected void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
StringBuffer data=new StringBuffer();
String temp=null;
try
{
req.setCharacterEncoding("UTF-8");
res.setCharacterEncoding("UTF-8");
BufferedReader reade=req.getReader();
while((temp=reade.readLine())!=null)
{
data.append(temp);
}
System.out.println(temp);
JSONObject jsonobj=new JSONObject(data.toString());
System.out.println("ok");
res.setContentType("text/xml");
res.getWriter().print("您的信息: 名字:"+jsonobj.getString("name")+",年龄:"+jsonobj.getInt("age")+",性别:"+jsonobj.getString("sex")+",喜欢的颜色:"+jsonobj.getString("loveColor")+".");
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}HTML文件 test.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>动态查询显示结果
</title>
<script type="text/javascript" src="json.js"></script>
<script type="text/javascript">
var xmlHttp;
function createXMLHTTPRequest()
{
if(window.ActiveXObject)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp=new XMLHttpRequest();
}
}
function doJson()
{
createXMLHTTPRequest();
var url="PostData?timeStamp="+(new Date()).getTime();
//var json=JSON.stringify(new People("唐俊","21","男","蓝色"));
var carAsJson=JSON.stringify(new People("aa","21","b","blue"));
//alert(JSON.stringify(new People("唐俊","21","男","蓝色")));
xmlHttp.open("POST",url,true);
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
xmlHttp.send(carAsJson);
}
function handleStateChange()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
alert(xmlHttp.responseText);
document.getElementById("selectdata").innerHTML=xmlHttp.responseText;
}
}
}
function People(name,age,sex,loveColor)
{
this.name=name;
this.age=age;
this.sex=sex;
this.loveColor=loveColor;
}
</script>
</head>
<body>
<form action="#">
<table>
<tr>
<td>姓名:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="text" name="age"></td>
</tr>
<tr>
<td>性别:</td>
<td><input type="text" name="sex"></td>
</tr>
<tr>
<td>你喜欢的颜色:</td>
<td><input type="text" name="loveColor"></td>
</tr>
</table>
<input type="button" value="GetData" onclick="doJson();"/><br><br>
<div id="selectdata"></div>
</form>
</body>
</html>多谢了!
解决方案 »
- 关于Tomcat的设定文档
- <s:actionmessage /> 无法显示 addActionMessage(str)中的信息
- java.学习群新开的。共同进步MSN
- 急啊!好几天了啊,高分请教关于Tomcat配置完了以后不能正常显示JSP的问题!!
- 如何用JSP实现读取另一个网页中数据和代码
- Tapestry的workbench例子发布在tomcat5.0上失败求助?
- 我刚用MYECLIPSE运行java程序的时候,突然弹出editor does not contain a main type这个东西
- 在Jbuilder X中编译jsp出错的问题!!!
- 求Jbuilder8 下JSP不能保存的补丁,高分奉送!!!!!
- idea启动tomcat时报错
- 看看这个是什么问题
- 简单的html提交到servlet验证的代码..不过一直没连通...
import org.w3c.dom.NodeList;
import org.json.JSONObject;以上类都没用过,顶!