本帖最后由 冯超 于 2013-3-31 11:15 编辑
希望轮胎里面的人能帮下忙,自己搞了10几分钟都不知道为什么错。那个图片是web项目的名称,一下贴代码:- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>1.html</title>
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="this is my page">
- <meta http-equiv="content-type" content="text/html; charset=UTF-8">
- <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
- <script type='text/jscript'>
- var xmlHttp;
- //1.创建XMLHttpRequest对象并且获取xmlHttp对象
- function creatXHR(){
- if (window.XMLHttpRequest) {
- xmlHttp = new XMLHttpRequest();
- }
- else
- if (window.ActiveXObject) {
- xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
- }
-
- if (!xmlHttp) {
- alert("你使用的浏览器不支持XMLhttp对象!")
- return false;
- }
- }
-
- function sendRequest(){
- creatXHR();
- //2.打开和服务器的连接
- /* open(method, url, asynch)
- 1.method 请求方法为get或者post
- 2.url 请求的路径
- 3.asynch 表示请求是否要异步传输,默认值为true
- */
- xmlHttp.open("get", "../servlet/TestServlet", true);
-
- //3.发送数据 若用get 则不会发送任何数据 传递null
- xmlHttp.send(null);
-
- //alert(xmlHttp.nodeName);
- //4.接受服务器端的响应
- xmlHttp.onreadystatechange = catchResult;
- }
-
- function catchResult(){
- if (xmlHttp.readyState == 4) {
- if (xmlHttp.status == 200)
- alert(xmlHttp.responseText);
- else {
- alert('错误代码:' + xmlHttp.status);
- }
- }
-
- }
- </script>
- </head>
- <body>
- <a href="#" onclick="sendRequest();">说个Hello world</a>
- </body>
- </html>
复制代码 1.html代码如上- 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;
- public class TestServlet extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- request.setCharacterEncoding("utf-8");
- response.setContentType("text/html; charset=UTF-8");
-
- System.out.println("xxxxxxxxxxxxx");
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doPost(request, response);
- }
- }
复制代码 TestServlet代码如上:- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5"
- 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/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <servlet>
- <servlet-name>TestServlet</servlet-name>
- <servlet-class>TestServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>TestServlet</servlet-name>
- <url-pattern>/servlet/TestServlet</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
复制代码 web.xml配置如上。
我不知道为什么那个url老是出现错了,写了AjxaTest/servlet/TestServlet ; ..servlet/TestServlet等还是出现页面弹出404错误!
|
|