AJAX与servlet的信息交互
1 <%@ page language="java" import="java.util.*" pageEncoding="gb2312" %> 2 <%@ page import=" java.net.*" %> 3 4 5 <html> 6 <head> 7 <script type="text/javascript"> 8 function showHint(str) 9 { 10 var xmlhttp; 11 if (str.length==0) 12 { 13 document.getElementById("txtHint").innerHTML=""; 14 return; 15 } 16 if (window.XMLHttpRequest) 17 {// code for IE7+, Firefox, Chrome, Opera, Safari 18 xmlhttp=new XMLHttpRequest(); 19 } 20 else 21 {// code for IE6, IE5 22 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 23 } 24 xmlhttp.onreadystatechange=function() 25 { 26 if (xmlhttp.readyState==4 && xmlhttp.status==200) 27 { 28 document.getElementById("txtHint").innerHTML=xmlhttp.responseText; 29 } 30 } 31 32 xmlhttp.open("GET","servlet/gethint?q="+str,true); 33 xmlhttp.send(); 34 } 35 </script> 36 <title>验证码测试</title> 37 </head> 38 <body> 39 <h3>请输入字母(a-z)</h3> 40 <form action=""> 41 姓名:<input type="text" id="text1" onkeyup="showHint(this.value)"/> 42 <p>建议:<span id="txtHint"></span></p> 43 </form> 44 </body> 45 </html>
1 package demoservlet; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 public class gethint extends HttpServlet { 12 13 /** 14 * Constructor of the object. 15 */ 16 public gethint() { 17 super(); 18 } 19 20 /** 21 * Destruction of the servlet. <br> 22 */ 23 public void destroy() { 24 super.destroy(); // Just puts "destroy" string in log 25 // Put your code here 26 } 27 28 /** 29 * The doGet method of the servlet. <br> 30 * 31 * This method is called when a form has its tag value method equals to get. 32 * 33 * @param request the request send by the client to the server 34 * @param response the response send by the server to the client 35 * @throws ServletException if an error occurred 36 * @throws IOException if an error occurred 37 */ 38 public void doGet(HttpServletRequest request, HttpServletResponse response) 39 throws ServletException, IOException { 40 request.setCharacterEncoding("gb2312"); 41 response.setCharacterEncoding("gb2312"); 42 String str=request.getParameter("q"); 43 44 response.setContentType("text/html"); 45 PrintWriter out = response.getWriter(); 46 out.write("ada"); 47 out.flush(); 48 out.close(); 49 } 50 51 /** 52 * The doPost method of the servlet. <br> 53 * 54 * This method is called when a form has its tag value method equals to post. 55 * 56 * @param request the request send by the client to the server 57 * @param response the response send by the server to the client 58 * @throws ServletException if an error occurred 59 * @throws IOException if an error occurred 60 */ 61 public void doPost(HttpServletRequest request, HttpServletResponse response) 62 throws ServletException, IOException { 63 64 response.setContentType("text/html"); 65 PrintWriter out = response.getWriter(); 66 out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); 67 out.println("<HTML>"); 68 out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); 69 out.println(" <BODY>"); 70 out.print(" This is "); 71 out.print(this.getClass()); 72 out.println(", using the POST method"); 73 out.println(" </BODY>"); 74 out.println("</HTML>"); 75 out.flush(); 76 out.close(); 77 } 78 79 /** 80 * Initialization of the servlet. <br> 81 * 82 * @throws ServletException if an error occurs 83 */ 84 public void init() throws ServletException { 85 // Put your code here 86 } 87 88 }