iteye_14486 2010-07-26 10:16
浏览 213
已采纳

新手关于ajax+servlet的问题

最近小弟在学习ajax,就随便做了一个ajax请求servlet并返回结果的小测试,但是一直调不通,一直是“网页上有错误”,看了一下XMLHttpRequest的值,一直是0。这是怎么回事啊?那位高手请指点一下,不胜感激!!!!!

希望实现功能:通过URL请求把参数传给ajaxServlet类处理,并显示处理结果!

index.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 'index.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">
    
    <script type="text/javascript">
    var xmlHttp;

    function createXMLHttpRequest()
    {
        try{
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e)
        {
            try{
                xmlHttp = new XMLHttpRequest();
            }
            catch(e)
            {
                xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
            }
        }
    }

    function startRequest()
    {
        createXMLHttpRequest();
                        xmlHttp.onreadystatechange = handleXMLHttpRequest;
        xmlHttp.open("GET",basePath+"servlet/ajaxServlet?username=pangbo&password=19871201",true);
        send(null);
    }

    function handleXMLHttpRequest()
    {
            if(xmlHttp.readyState==4)
            {
                    if(xmlHttp.state==200)
                    {   
                        alert(xmlHttp.responseText());
                    }
            }
    }
    
</script>
  </head>
  
  
  <body>
   <input name="submit" value="提交" type="submit" onclick="startRequest()"/>
  </body>

</html>

 ajaxServlet类:

package com.ac.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ajaxServlet extends HttpServlet{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public void init(ServletConfig config) throws ServletException
    {
        super.init(config);
    }
    public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
    {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String responseText = username+"的密码是"+password+"请牢记!!!!";
        
        PrintWriter out = response.getWriter() ;
        out.println(responseText);
        
        out.close();
    }
}

web.xml(应该没有配错)

<?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">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>ajaxServlet</servlet-name>
    <servlet-class>com.ac.servlet.ajaxServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ajaxServlet</servlet-name>
    <url-pattern>/servlet/ajaxServlet</url-pattern> 
  </servlet-mapping>
</web-app>
  • 写回答

3条回答 默认 最新

  • iteye_6807 2010-07-26 18:44
    关注

    你的startRequest方法有点问题,
    最后一句send(null);有问题。楼上说的是错误的。

    应该改成:xmlHttp.send(null);

    你试一下吧。

    这是我一年前写过的Demo,你对比一下看看:

    var request = false;
    function getXMLHttpRequest() {
    try {
    request = new ActiveXObject("MSXML2.XMLHTTP");
    }
    catch (e) {
    try {
    request = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e1) {
    request = false;
    }
    }
    if (!request && typeof XMLHttpRequest != "undefined") {
    request = new XMLHttpRequest();
    }

    return request;
    

    }
    function sendRequest(url, paramer, method, back) {
    getXMLHttpRequest();
    request.onreadystatechange = back;
    if (method == "get") {
    request.open(method, url + "?" + paramer, true);
    request.send(null);
    } else {
    if (method == "post") {
    request.open(method, url, true);
    request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    request.send(paramer);
    }
    }
    }

    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 混合键合键合机对准标识
  • ¥100 现在不懂的是如何将当前的相机中的照片,作为纹理贴图,映射到扫描出的模型上
  • ¥15 目标跟踪,计算机视觉
  • ¥15 魔霸ROG7 pro,win11.息屏后会显示黑屏,如图,如何解决?(关键词-重新启动)
  • ¥15 有没有人知道这是哪里出了问题啊?要怎么改呀?
  • ¥200 C++表格文件处理-悬赏
  • ¥15 Windows Server2016本地登录失败
  • ¥15 复合卡卡号轨道写入芯片卡
  • ¥20 基于MATLAB的TDOA
  • ¥15 为啥输入字体突然变了