You are on page 1of 5

Ex.

No:8 Date:

UPDATION OF VISITOR COUNT USING AJAX

AIM:
To implement updation of visitor count using ajax

ALGORITHM:
1. 2. 3. 4. 5. Write a Servlet program. Place the program in classes directory Write the xml program and place it in the root directory Modify web.xml Compile and execute the program.

AJAX:
Ajax is a group of interrelated web developement techniques used on the client-side to create asynchronous web applications. With Ajax, web applications can send data to, and retrieve data from, a server asynchronously (in the background) without interfering with the display and behavior of the existing page. Data is usually retrieved using the XMLHttpRequest object. Despite the name, the use of XML is not needed (JSON is often used instead), and the requests do not need to be asynchronous.[2]

PROGRAM:
xm.java import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; public class xm extends HttpServlet { int visit=0; public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException {

visit++; response.setContentType("text/html"); PrintWriter servletout=response.getWriter(); servletout.println("<html xmlns='http://www.w3.org/1999/xthml'>"); servletout.println("<head>"); servletout.println("<script type='text/javascript' src='/cs2k9b36/Visitcount.js'></script>"); servletout.println("<meta http-equiv='Content-ScriptType' content='text/javascript' /></head>"); servletout.println("<body onload='init();'>"); servletout.println("<p>helloworld</p><p>This page has been viewed <span id='visits'>"+visit+"</span> times since the most recent visit </p></html>"); //"<html><head><script type='text/javascript' src='/cs2k9b36/Visitcount.js'></script></head><body onload='init()'></body></html>"); servletout.close(); } public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException { response.setContentType("application/xml; charset=\"UTF8\""); PrintWriter servletout=response.getWriter(); servletout.println("<?xml version='1.0' encoding='UTF-8'? ><count>"+visit+"</count>"); servletout.close(); } } VisitCount.js:function init() { //window.alert("Sdsdsds"); window.setInterval("getVisits()",3000); return; } function getVisits() { //window.alert("Sdsdvzxc"); var connection; connection=new XMLHttpRequest(); if(connection)

{ connection.open("POST","/cs2k9b36/servlet/echo",true); //window.alert("waadup"); connection.onreadystatechange=function update() {updateVisits(connection);}; connection.setRequestHeader("ContentType","application/x-www-form-urlencoded"); connection.send(""); } return; } function updateVisits(connection) { if(connection.readyState==4&&connection.status==200) { var visits=document.getElementById("visits"); var count=connection.responseXML.documentElement; visits.childNodes[0].data=count.childNodes[0].data; } return; } WEB.XML:<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>servlet</display-name> <description> Sampath </description> <servlet> <servlet-name>hello</servlet-name> <servlet-class>xm</servlet-class> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/servlet/hello</url-pattern> </servlet-mapping> </web-app>

Output:-

RESULT: Thus the Experiment was executed and


verified.

You might also like