In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about the application of Ajax Servlet in network programming. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.
Ajax asynchronous request, servlet generates random data, and there is no refresh display on Html page.
Operating environment: jdk1.5+tomcat 5.5
1. 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"; public 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 ();}
II. Testajax.html
< html > < head > < title > AJAX.html < / title > < / head > < 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);}}
/ * analyze the returned text document * /
Function complete () {if (req.readyState = = 4) {if (req.status = = 200) {/ / alert (req.responseText); var strResult = unescape (req.responseText); var arrResult = strResult.split ("@"); RemoveRow (); / / Delete previous data. Num1 = arrResult [0]; / / the value of field num1 num2 = arrResult [1]; / / the value of field 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 was a problem with the request.');}} function RemoveRow () {/ / keep * * row headers and delete the rest of the data. Var iRows = tb.rows.length; for (var iCombitor I < iRows-1;i++) {tb.deleteRow (1);}} function MyShow () automatically refresh once every 2 seconds and get data once every 2 seconds. Timer = window.setInterval ("getResult ()", 2000) } < / script > < body onload=MyShow () "> < table width=" 47 "height=" 23 "border=" 0 "cellpadding=" 1 "cellspacing=" 0 "id=" tb "> < tr > < td > Code < / td > < td > Price < / td > < / tr > < / table > < form > < p > enter stock symbol: < input type=" text "size=" 14 "name=" phone "id=" phone "/ > P > < p > number of transactions you make: < / p > < p > < textarea name= "order" rows= "6" cols= "50" id= "order" > < / textarea > < / p > < p > < input type= "submit" value= "submit" id= "submit" / > < / p > < / form > < / body >
III. 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 >
Operation diagram:
This is the end of the application of Ajax Servlet examples in network programming, isn't it interesting? Ajax Servlet examples are widely used in network programming, which should be accumulated and applied in practice.
The above is the application of Ajax Servlet in network programming shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.