小虾请教一个问题,关于ajax和servlet交互的问题~~~
html:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%><%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_1.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"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <script type="text/javascript"> //定义变量xmlHttp var xmlHttp; //定义函数,用来为xmlHttp赋值 function createXMLHttpRequest(){ if(window.ActiveXObject){ xmlHttp= new ActiveXObject("Microsoft.XMLHTTP"); }else if(window.XMLHttpRequest){ xmlHttp=new XMLHttpRequest(); } } //定义函数,向服务器发送信息 function sendMessage(){ createXMLHttpRequest(); //处理缓存问题 xmlHttp.open("GET","http://localhost:80/ajax_0/servlet/Servlet_1?time="+new Date().getDay(),true); xmlHttp.send(null); } </script> <body> <b>利用JavaScript的事件处理机制,通过XMLHttpRequest对象执行服务器端的程序</b><br><br><br> 1、onMouseOver事件 <a href="#" onMouseOver="sendMessage()">哈哈</a> <br><br><br> 2、onClick事件 <input type="button" value="按钮" onClick="sendMessage()" /> <br><br><br> 3、onBlur事件 <input type="text" name="user" onBlur="sendMessage()"/> <br><br><br> 4、onChange事件 <input type="text" name="user" value="tianyitime" onChange="sendMessage()"/> <br><br><br> <a href="index.jsp">返回</a> </body></html>package com.tianyitime.ajax;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 Servlet_1 extends HttpServlet { /** * Constructor of the object. */ public Servlet_1() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = new String(request.getParameter("name").getBytes ("ISO-8859-1"), "GBK");System.out.println(name); doPost(request,response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = new String(request.getParameter("name").getBytes ("ISO-8859-1"), "GBK"); System.out.println(name); System.out.println("Servlet1执行了"); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occure */ public void init() throws ServletException { // Put your code here }}
<?xml version="1.0" encoding="ISO-8859-1"?><web-app xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"version="2.4"><display-name>Welcome to Tomcat</display-name> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>Servlet_1</servlet-name> <servlet-class>com.tianyitime.ajax.Servlet_1</servlet-class> </servlet> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>Servlet_2</servlet-name> <servlet-class>com.tianyitime.ajax.Servlet_2</servlet-class> </servlet> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>Servlet_3</servlet-name> <servlet-class>com.tianyitime.ajax.Servlet_3</servlet-class> </servlet> <servlet-mapping> <servlet-name>Servlet_1</servlet-name> <url-pattern>/servlet/Servlet_1</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Servlet_2</servlet-name> <url-pattern>/servlet/Servlet_2</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Servlet_3</servlet-name> <url-pattern>/servlet/Servlet_3</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list></web-app>以上就是全部代码,我不明白的是为什么点击网页没有反应呢?感觉像是前台去后台不交互的样子~~~~~