GTU WTAD Practical 14 : Write a Servlet which displays a message and also displays how many times the message has been displayed (how many times the page has been visited)

GTU WTAD Practical 14 :  Write a Servlet which displays a message and also displays how many times the message has been displayed (how many times the page has been visited).

GTU WTAD Practical 14 : Write a Servlet which displays a message and also displays how many times the message has been displayed (how many times the page has been visited)

When you visit some website it says that you have visited this website N times. So the same funcationality you can implement using Servlet to count number of visits by perticular user using the following code. This Servlet code is sometime used to findout that user has surf more number of pages and you can give some special offer as loyal customer, etc.

File Name : index.jsp

File Location : Tomcat_Apache\webapps\gtu14\

<%
response.sendRedirect(“./HitCount”);
%>

 

 

File Name : web.xml

File Location : Tomcat_Apache\webapps\gtu14\WEB-INF

 

<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<web-app xmlns=”http://java.sun.com/xml/ns/javaee”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd”
version=”3.0″
metadata-complete=”true”>

<display-name>GTU WTAD Practical 14  </display-name>
<description>Write a Servlet which displays a message and also displays how many times the message has been displayed (how many times the page has been visited).</description>

<servlet>
<servlet-name>HitCount</servlet-name>
<servlet-class>HitCount</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>HitCount</servlet-name>
<url-pattern>/HitCount</url-pattern>
</servlet-mapping>

</web-app>

 

File Name : HitCount.java

File Location : Tomcat_Apache\webapps\gtu14\WEB-INF\classes

 

/*
GTU WTAD Practical 14 :  Write a Servlet which displays a message and also displays how many times the message has been displayed (how many times the page has been visited).
*/

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HitCount extends HttpServlet
{
static int count;

public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
response.setContentType(“text/html”);
PrintWriter out = response.getWriter();
out.println(“<html><head><title>GTU WTAD Practical 14 : Write a Servlet which displays a message and also displays how many times the message has been displayed (how many times the page has been visited).</title></head><body><center><h1>GTU WTAD Practical 14 :  Write a Servlet which displays a message and also displays how many times the message has been displayed (how many times the page has been visited).</h1></center><br><br>”);

count++;
out.println(“<h1>” + count + ”  : time u have visit for this Page</h1> “);

out.println(“</body></html>”);
}
}

 Output

GTU WTAD Practical 14 : Write a Servlet which displays a message and also displays how many times the message has been displayed (how many times the page has been visited).

Check out WTAD Practical 15 :

Click here for WTAD Practical 16

Adarsh Patel

My Self Adarsh Suryakant Patel. Currently I am working as Assistant Professor at Parul Instiute of Engineering and Technology, MCA Department, Limda, Vadodara. Major Responsibility : * Lectures * Organizing Training and Expert Sessions for * Placement and Recruitment * Faculty Representative