0% found this document useful (0 votes)
49 views5 pages

Updation of Visitor Count Using Ajax: Ex - No:8 Date

This document describes updating a visitor count using AJAX. It includes: 1) An algorithm that outlines creating a servlet program, placing it in the classes directory, creating an XML program and placing it in the root directory, modifying web.xml, and compiling/executing the program. 2) Details about AJAX and how it allows asynchronous data retrieval from a server without interfering with the existing page. 3) A Java servlet program that increments the visitor count and returns it, along with JavaScript that uses XMLHttpRequest to periodically retrieve the updated count from the servlet and display it.

Uploaded by

saleemfawaaz
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
0% found this document useful (0 votes)
49 views5 pages

Updation of Visitor Count Using Ajax: Ex - No:8 Date

This document describes updating a visitor count using AJAX. It includes: 1) An algorithm that outlines creating a servlet program, placing it in the classes directory, creating an XML program and placing it in the root directory, modifying web.xml, and compiling/executing the program. 2) Details about AJAX and how it allows asynchronous data retrieval from a server without interfering with the existing page. 3) A Java servlet program that increments the visitor count and returns it, along with JavaScript that uses XMLHttpRequest to periodically retrieve the updated count from the servlet and display it.

Uploaded by

saleemfawaaz
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 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