ajax与servlet的简单传值与web.xml的配置如何书写

qq_33474742 2016-10-15 08:11:40
帮忙找下错误,浏览器总提示404


index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>

<script type="text/javascript">
function createXMLHttpRequest()
{
xmlhttp=false;
if(window.ActiveXObject)
{
try{
xmlHttp=new ActiveXObject('Msxml2.XMLHTTP');
}
catch(e)
{
try{
xmlHttp=new ActiveXObject('Microsoft.XMLHTTP');
}
catch(ee)
{xmlHttp=fale; }
}
}
else if(window.XMLHttpRequest)
{
try{
xmlHttp=new XMLHttpRequest();
}
catch(e)
{
xmlHttp=false;
}
}
return xmlHttp;
}

function sendRequest() {
XMLHttpReq=createXMLHttpRequest();
XMLHttpReq.open("GET", "/ajaxServlet?", true);
XMLHttpReq.onreadystatechange = getContent;//指定响应函数
// XMLHttpReq.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
String str="t="+Math.random();
alert(str);
XMLHttpReq.send(null);

}

function getContent()
{
if(XMLHttpReq.readyState==4&&XMLHttpReq.status==200)
{
var queryContentStr=XMLHttpReq.responseText;
document.getElementById("div1").innerHTML=queryContentStr;
}

}
</script>
</head>
<body>
<table border="1px">
<tr>
<td height="100px" width="300px" id="div1"></td>
</tr>

</table>
<input type="button" onclick="sendRequest()" value="click">

</body>
</html>

ajaxServlet.java
package ajax;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;//这个包必须有!!




public class ajaxServlet extends HttpServlet{
public void init() throws ServletException{}
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/xml; charset=UTF-8");
//以下两句为取消在本地的缓存
response.setHeader("Cache-Control", "no-cache");

response.getWriter().println("这是从servlet传回的数据!");
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
doGet(request,response);
}
}

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns = "http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<servlet >
<servlet-name>ajaxServlet</servlet-name>
<servlet-class>aa.src.ajax.ajaxServlet</servlet-class>
</servlet >

<servlet-mapping>
<servlet-name>ajaxServlet</servlet-name>
<url-pattern>/ajaxServlet</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

</web-app>
...全文
242 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
孟子E章 2016-10-15
  • 打赏
  • 举报
回复
另外 这里也不对 <servlet-class>aa.src.ajax.ajaxServlet</servlet-class> 应该是 <servlet-class>ajax.ajaxServlet</servlet-class> 前面写的是包名
孟子E章 2016-10-15
  • 打赏
  • 举报
回复
如果你的访问方式是 http://localhost:8080/这样没有项目名单话说对的 如果有项目名,则需要 XMLHttpReq.open("GET", "/项目名/ajaxServlet?", true);

81,116

社区成员

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

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