A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 冯超 高级黑马   /  2013-3-30 00:34  /  1681 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 冯超 于 2013-3-31 11:15 编辑

希望轮胎里面的人能帮下忙,自己搞了10几分钟都不知道为什么错。那个图片是web项目的名称,一下贴代码:
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3.     <head>
  4.         <title>1.html</title>
  5.         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  6.         <meta http-equiv="description" content="this is my page">
  7.         <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  8.         <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
  9.         <script type='text/jscript'>
  10.             var xmlHttp;
  11.             //1.创建XMLHttpRequest对象并且获取xmlHttp对象
  12.             function creatXHR(){
  13.                 if (window.XMLHttpRequest) {
  14.                     xmlHttp = new XMLHttpRequest();
  15.                 }
  16.                 else
  17.                     if (window.ActiveXObject) {
  18.                         xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  19.                     }
  20.                
  21.                 if (!xmlHttp) {
  22.                     alert("你使用的浏览器不支持XMLhttp对象!")
  23.                     return false;
  24.                 }
  25.             }
  26.             
  27.             function sendRequest(){
  28.                 creatXHR();
  29.                 //2.打开和服务器的连接
  30.                 /*        open(method, url, asynch)
  31.                  1.method 请求方法为get或者post
  32.                  2.url 请求的路径
  33.                  3.asynch 表示请求是否要异步传输,默认值为true
  34.                  */
  35.                 xmlHttp.open("get", "../servlet/TestServlet", true);
  36.                
  37.                 //3.发送数据 若用get 则不会发送任何数据  传递null
  38.                 xmlHttp.send(null);
  39.                
  40.                 //alert(xmlHttp.nodeName);
  41.                                 //4.接受服务器端的响应
  42.                 xmlHttp.onreadystatechange = catchResult;
  43.             }
  44.             
  45.             function catchResult(){
  46.                 if (xmlHttp.readyState == 4) {
  47.                     if (xmlHttp.status == 200)
  48.                         alert(xmlHttp.responseText);
  49.                                         else {
  50.                             alert('错误代码:' + xmlHttp.status);
  51.                         }        
  52.                 }
  53.                                 
  54.             }
  55.         </script>
  56.     </head>
  57.     <body>
  58.         <a href="#" onclick="sendRequest();">说个Hello world</a>
  59.     </body>
  60. </html>
复制代码
1.html代码如上
  1. import java.io.IOException;
  2. import java.io.PrintWriter;

  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;


  7. public class TestServlet extends HttpServlet {

  8.         public void doGet(HttpServletRequest request, HttpServletResponse response)
  9.                         throws ServletException, IOException {
  10.                 request.setCharacterEncoding("utf-8");
  11.                 response.setContentType("text/html; charset=UTF-8");
  12.                
  13.                 System.out.println("xxxxxxxxxxxxx");
  14.         }

  15.         public void doPost(HttpServletRequest request, HttpServletResponse response)
  16.                         throws ServletException, IOException {
  17.                 doPost(request, response);
  18.         }

  19. }
复制代码
TestServlet代码如上:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5"
  3.         xmlns="http://java.sun.com/xml/ns/javaee"
  4.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6.         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  7.   <servlet>
  8.     <servlet-name>TestServlet</servlet-name>
  9.     <servlet-class>TestServlet</servlet-class>
  10.   </servlet>

  11.   <servlet-mapping>
  12.     <servlet-name>TestServlet</servlet-name>
  13.     <url-pattern>/servlet/TestServlet</url-pattern>
  14.   </servlet-mapping>
  15.   <welcome-file-list>
  16.     <welcome-file>index.jsp</welcome-file>
  17.   </welcome-file-list>
  18. </web-app>
复制代码
web.xml配置如上。
我不知道为什么那个url老是出现错了,写了AjxaTest/servlet/TestServlet ; ..servlet/TestServlet等还是出现页面弹出404错误!

xaq.jpg (17.42 KB, 下载次数: 9)

xaq.jpg

点评

如果仍有问题,请继续追问,如果问题已解决,请将分类改为已解决,谢谢  发表于 2013-3-30 22:39

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

8 个回复

倒序浏览
如果路径是2.html 就没有错···
  明早我看看吧··到底是什么问题
回复 使用道具 举报
你的ajax访问的地址应该与你配置的servlet-mapping中的一致才行啊
xmlHttp.open("get", "servlet/TestServlet", true);
回复 使用道具 举报
fighting 发表于 2013-3-30 09:21
你的ajax访问的地址应该与你配置的servlet-mapping中的一致才行啊
xmlHttp.open("get", "servlet/TestServl ...

还是有那个问题
回复 使用道具 举报
fighting 发表于 2013-3-30 09:21
你的ajax访问的地址应该与你配置的servlet-mapping中的一致才行啊
xmlHttp.open("get", "servlet/TestServl ...

还是有那个问题········!!
回复 使用道具 举报
冯超 发表于 2013-3-30 10:02
还是有那个问题········!!

怎么可能,在我的机器上可以正常运行啊。。。
回复 使用道具 举报
冯超 高级黑马 2013-3-30 10:40:36
7#
fighting 发表于 2013-3-30 10:10
怎么可能,在我的机器上可以正常运行啊。。。

不知道为什么···老是出现404错误
回复 使用道具 举报
冯超 高级黑马 2013-3-30 10:40:42
8#
fighting 发表于 2013-3-30 10:10
怎么可能,在我的机器上可以正常运行啊。。。

不知道为什么···老是出现404错误················
回复 使用道具 举报
``ding 好····
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马