我的第一个AJAX例子出错帮看下

xuexijava 2009-12-11 03:30:29
用的是Struts1.3+Jsp
Action内容如下
public class MyAjaxTest1Action extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
System.out.println("is testaction!");
UserDao ud = new UserDao();

try {
request.setCharacterEncoding("gb2312");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
String msgStr = "";
response.setContentType("text/xml;charset=GB2312");
response.setHeader("Cache-Control", "no-cache");
String name = (String) request.getParameter("names");
// System.out.println("name = " + name);
if (ud.validateUname(name)) { // 找查数据库中有无该用户名
msgStr = "对不起,此用户名已经存在,请更换用户名注册!";
} else {
msgStr = "用户未被注册,可以使用!";
}
System.out.println("is validate ok!");
response.getWriter().println(msgStr);
response.getWriter().close();

} catch (Exception ex) {

}
return null;
}
}

Jsp代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 'MyAjaxTest1.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">
/*定义一个变量用来存放XMLHttpRequest对象*/
var xmlHttp;
/*创建XMLHttpRequest对象的函数*/
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
alert("createXMLHttpRequest is ok!");
}else if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
alert("createXMLHttpRequest is ok!");
}
}
/*发送请求函数*/
function startRequest(){
/*创建XMLHttpRequest对象*/

createXMLHttpRequest();
/*创建请求*/
var name=document.getElementById("names").value;
alert("name value is ok");

xmlHttp.open("GET", "myAjaxTest1.do?names="+name, true);
alert("open is ok");
/*告诉XMLHttpRequest对象哪个函数会处理它的状态变化,并把onreadystatechange属性指向该函数*/
xmlHttp.onreadystatechange=handleStateChange;
/*发送请求*/
xmlHttp.send(null);
}
/*回调函数*/
function handleStateChange(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
/*alert("the server replied with:"+xmlHttp.responseText);*/
document.getElementById("bb").innerHTML=xmlHttp.responseText;
}
}
}
</script>

</head>

<body>
<form action="myAjaxTest1" method="post">
用户名:<input type="text" id="names"><input type="button" value="检测" onClick="startRequest()"/><div id="bb"></div>
</form>
</body>
</html>
Struts.xml配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>
<form-beans />
<global-exceptions />
<global-forwards />
<action-mappings >
<action
input="/MyAjaxTest1.jsp"
path="/myAjaxTest1"
type="web.action.MyAjaxTest1Action" />
</action-mappings>

<message-resources parameter="web.ApplicationResources" />
</struts-config>
报异常说找不到Action?
严重: Servlet.service() for servlet action threw exception
java.lang.ClassNotFoundException: web.action.MyAjaxTest1Action


高手帮看下哪里出错了,该怎么改,还有程序本身有什么错误,刚学AJAX帮看看了各位大侠!
...全文
79 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
licip 2009-12-12
  • 打赏
  • 举报
回复
查询一下包名看对不对。
你还有一个问题。
response.setContentType("text/xml;charset=GB2312");
你没有用xml呀。你应该用response.setContentType("text/plain;charset=GB2312");
鲜为人知 2009-12-12
  • 打赏
  • 举报
回复
response.getWriter().out();
试试,O(∩_∩)O哈哈~
sohighthesky 2009-12-12
  • 打赏
  • 举报
回复
Action?
严重: Servlet.service() for servlet action threw exception

先去看看
servlet的书
xuexijava 2009-12-11
  • 打赏
  • 举报
回复
UP

52,792

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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