Ajax+servlet instance

Source: Internet
Author: User
Tags abs html page string first row version tostring window stringbuffer
Ajax|servlet

Ajax asynchronous request, the servlet produces random data, HTML page no refresh display.
Operating Environment: Jdk1.5+tomcat 5.5

First, Ajaxservlet.java

Package Com;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;import Java.util.random;public class Ajaxservlet extends HttpServlet{private Static final String content_type= "Text/xml; charset=gb2312 ";p ublic void Init () throws Servletexception{}public void Doget (HttpServletRequest request, HttpServletResponse response) throws servletexception,ioexception{Response.setcontenttype (Content_Type); Response.setheader ("Cache-control", "No-cache"); Response.setheader ("Pragma", "No-cache"); Response.setdateheader ("Expires", 0);  PrintWriter out=response.getwriter (); String action=request.getparameter ("action"); if (("Send"). Equals (action) {    Random Random = new Random ();  string aa=integer.tostring (Math.Abs (Random.nextint ()));  string bb=integer.tostring (Math.Abs ( Random.nextint ());  String Cc=inteGer.tostring (Math.Abs (Random.nextint ()));  String dd=integer.tostring (Math.Abs (Random.nextint ()));  StringBuffer sb=new stringbuffer ();  sb.append (AA);  sb.append ("@@@");  sb.append (BB);  Sb.append ("@@@");  sb.append (CC);  sb.append ("@@@");  sb.append (DD);  Out.write (sb.toString ()) ;  out.close (); }}}

Second, testajax.html
<title>AJAX.html</title>
<script type= "Text/javascript" >

function GetResult () {  var url = "/tajax/aajaxservlet?action=send";  if window. XMLHttpRequest) {     req = new XMLHttpRequest ();  }else if (window. ActiveXObject) {     req = new ActiveXObject ("Microsoft.XMLHTTP");  }   if ( Req) {      Req.open ("Get", url, True);      Req.onreadystatechange = complete;      req.send (null);  }}/* Parse-returned text document * * function complete () {  if (req.readystate = 4) {     if (Req.status =) {   & nbsp;  //Alert (Req.responsetext);       var strresult = unescape ( Req.responsetext);      var arrresult = strresult.split ("@@@");       Removerow (); //Delete previous data .      NUM1 = arrresult[0]; Value of field num1       num2 = arrresult[1]; WordValue of segment num2       num3 = arrresult[2];      num4 = arrresult[3];       row1= tb.insertrow ();      cell1 = Row1.insertcell ();       Cell1.innertext = num1;      cell2 = Row1.insertcell ();       cell2.innertext = num2;      row2= tb.insertrow ();       Cell3 = Row2.insertcell ();      cell3.innertext = num3;       CELL4 = Row2.insertcell ();      cell4.innertext = num4;   } else {       alert (' There is a problem with the request. ');     }  }}  function Removerow () {//Left first row header, all remaining data deleted .    var irows = tb.rows.length;    for (var i=0;i< irows-1;i++) {       Tb.deleteRoW (1);   }} function Myshow () {//2 seconds automatically refresh once, 2 seconds to get data .  timer = Window.setinterval ("GetResult ()", ); }

</script>

<body >
<table width= "47%" height= "border=" 0 "cellpadding=" 1 "cellspacing=" 0 "id=" TB ">
<tr><td> Code </td>
<td> Price </td>
</tr>
</table>

<form>
<p> Enter Stock code:
<input type= "text" size= "name=" "Phone" id= "Phone"/>
</p>
<p> Number of transactions you:</p>
<p><textarea name= "Order" rows= "6" cols= "M" id= "order" ></textarea></p>
<p><input type= "Submit" value= "submitted" id= "Submission"/></p>
</form>

</body>

Third, Web.xml

<?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>
<description>
Welcome to Tomcat
</description>

<servlet>
<servlet-name>AajaxServlet</servlet-name>
<servlet-class>com.ajaxServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AajaxServlet</servlet-name>
<url-pattern>/AajaxServlet</url-pattern>
</servlet-mapping>


</web-app>


Run diagram:



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.