haojing8312 2011-08-15 18:18
浏览 167
已采纳

下面ajax访问servlet的程序有问题 谁帮我看一下

这是index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


<br> var obj;<br> function check(){<br> //获得id为username的节点的对象<br> var usernameNode = document.getElementById(&quot;username&quot;);<br> //获得该节点的值<br> var username = usernameNode.value;</p> <pre><code>//判断浏览器的类型 if(window.XMLHttpRequest()){ //FIREFOX等 obj = new XMLHttpRequest(); }else{ //IE obj = new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;); } //注册回调函数 obj.onreadystatechange = callback; var url = &quot;ajax?username=&quot;+username; //创建和服务器的连接 //参数1表示请求的类型&#39;GET&#39;&#39;POST&#39; //参数2表示请求的服务器的地址 //参数3表示是否是异步请求 //参数4表示用户名 //参数5表示密码 //没有可以省略 obj.open(&quot;GET&quot;,url,true); //发送请求 obj.send(null); </code></pre> <p>}<br> function callback(){<br> if(obj.readyState == 4){<br> if(obj.status == 200){<br> document.getElementById(&quot;result&quot;).innerHTML=obj.responseText;<br> }<br> }<br> }<br>


username:




下面是servlet
package demo;

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;

/**

  • Servlet implementation class AjaxServlet
    */
    public class AjaxServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**

    • Default constructor. */ public AjaxServlet() { // TODO Auto-generated constructor stub }

    /**

    • @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); PrintWriter out = response.getWriter(); if("rolex".equals(username)){ out.println("not use"); }else{ out.print("can use"); } }

}

下面是web.xml
<?xml version="1.0" encoding="UTF-8"?>
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">

index.jsp



AjaxServlet
AjaxServlet
demo.AjaxServlet


AjaxServlet
/ajax

  • 写回答

4条回答 默认 最新

  • laopeng301 2011-08-15 18:39
    关注

    :x var url = "ajax?username="+username;
    url错了啊···
    没提交到servlet
    AjaxServlet
    AjaxServlet
    demo.AjaxServlet


    AjaxServlet
    /ajax
    不web配置的是AjaxServlet
    所以你的是 var url = "AjaxServlet?username="+username;
    晕啊 你是大意了·····

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

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图问题
  • ¥15 有段代码不知道怎么理解,const isToken = (config.headers || {}).isToken === false
  • ¥100 采用栈和深度优先算法对我的代码进行修改显示路径
  • ¥15 pointnet2包安装
  • ¥20 射频功率问题,解答者有酬谢!
  • ¥80 构建降雨和积水的预测模型
  • ¥15 #Qt Transform setTransform()在鼠标拖动移动视角是一致在原地不动,无法变换视角(细微观察似乎视图有在原地抖动),无法变换视角(细微观察似乎视图有在原地抖动)
  • ¥50 如何利用无人机拍摄的数码照片测量鸟卵的长短径
  • ¥100 github贡献者给与奖励
  • ¥15 使用DS18B20+ESP8266获取温度数据返回-127.00