小虾请教一个有关问题,关于ajax和servlet交互的有关问题~
小虾请教一个问题,关于ajax和servlet交互的问题~~~
html:
servlet:
html:
- HTML code
<%@ 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>
servlet:
- Java code
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 } }