问题描述
这是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"><html> <head><script type="text/javascript" language="javascript"> var obj;function check(){//获得id为username的节点的对象 var usernameNode = document.getElementById("username"); //获得该节点的值 var username = usernameNode.value; //判断浏览器的类型 if(window.XMLHttpRequest()){ //FIREFOX等 obj = new XMLHttpRequest(); }else{ //IE obj = new ActiveXObject("Microsoft.XMLHTTP"); } //注册回调函数 obj.onreadystatechange = callback; var url = "ajax?username="+username; //创建和服务器的连接 //参数1表示请求的类型'GET''POST' //参数2表示请求的服务器的地址 //参数3表示是否是异步请求 //参数4表示用户名 //参数5表示密码 //没有可以省略 obj.open("GET",url,true); //发送请求 obj.send(null);}function callback(){ if(obj.readyState == 4){ if(obj.status == 200){ document.getElementById("result").innerHTML=obj.responseText; } }}</script> </head> <body> username:<input type="text" name="username" id="username"><span id="result"></span><br><input type="button" value="submit" onclick="check();"></input> </body></html>下面是servletpackage 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"?><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> <description></description> <display-name>AjaxServlet</display-name> <servlet-name>AjaxServlet</servlet-name> <servlet-class>demo.AjaxServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>AjaxServlet</servlet-name> <url-pattern>/ajax</url-pattern> </servlet-mapping></web-app> 问题补充:什么问题也没提示 这个是点提交按钮后 如果输入姓名为rolex则在username后面输出not use 否则输出can use但现在什么都不输出!
解决方案
var url = "ajax?username="+username; url错了啊···没提交到servlet <display-name>AjaxServlet</display-name> <servlet-name>AjaxServlet</servlet-name> <servlet-class>demo.AjaxServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>AjaxServlet</servlet-name> <url-pattern>/ajax</url-pattern> 不web配置的是<servlet-name>AjaxServlet</servlet-name> 所以你的是 var url = "AjaxServlet?username="+username; 晕啊 你是大意了·····
解决方案二:
你到servlet打印一下 看有没有提交进servlet
解决方案三:
我傻了 哈哈·· 我看错了··
解决方案四:
那提示什么问题了?