0% found this document useful (0 votes)
35 views2 pages

Ajax PRG

This document contains code for a servlet and JavaScript that work together to count the number of visits to a web page and dynamically update the count. The servlet increments a static counter on each request, and returns the count in XML format. The JavaScript uses AJAX to call the servlet every 300 milliseconds, parses the XML response, and updates the HTML to display the latest visit count.

Uploaded by

sridk78
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views2 pages

Ajax PRG

This document contains code for a servlet and JavaScript that work together to count the number of visits to a web page and dynamically update the count. The servlet increments a static counter on each request, and returns the count in XML format. The JavaScript uses AJAX to call the servlet every 300 milliseconds, parses the XML response, and updates the HTML to display the latest visit count.

Uploaded by

sridk78
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

//countservlet.java import javax.servlet.*; import javax.servlet.http.*; import java.io.

*; public class countservlet extends HttpServlet { private static int visits=0; public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException { visits++; res.setContentType("text/html"); PrintWriter srout=res.getWriter(); srout.println("<html><head><title> Example program for Ajax</title><script type='text/javascript' src='/countupdate.js'></script>"+ "</head><body onload='init()'><p>Welcome to the world :\n Number of visits:</p><p id='visits'>0 </p></body></html>"); srout.close(); } public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException { res.setContentType("application/xml"); PrintWriter srout=res.getWriter(); srout.println("<?xml version='1.0' ?> <count>"+visits+"</count>"); srout.close(); } }

//countupdate.js function init() { window.setInterval("getvisits()",300); return; } function getvisits() { var con=new XMLHttpRequest(); if(con) { con.open("POST","/countservlet",true); con.onreadystatechange=function update(){updatevisits(con);} con.send(""); } return; } function updatevisits(con) { if(con.readyState==4 && con.status==200) { visits=document.getElementById("visits"); var count=con.responseXML.documentElement; visits.childNodes[0].data=count.childNodes[0].data; } return; }

You might also like