Question

In JAVA assume you are designing a website, your boss asks you to design a program...

In JAVA

assume you are designing a website, your boss asks you to design a program name HitCounter that can count how many hits received in the past minute. you may assume that each hit come with a time stamp, hits are recieved in a chronological order. An example is monotonically increasing, earliest stamp starts at 1,and no hits arrive at the same time.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi, As for web applications, you need to code your requirement in a servlet class.

Explanation of code:

Here, I have written a simple servlet which increments the count by 1 each time when the website user hit that website. I am displaying the hits in last minute. after the minute pass by again the count starts from 1.

I have initialized the counter and the current tiime in the init() method and the rest of the code follows in the doGet() method.

CODE:

PageHitCounter.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.*;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class PageHitCounter extends HttpServlet {

private int hitCount;
private int currentMinute;

public void init() {
//getting current minute
Calendar calendar = Calendar.getInstance();
currentMinute = calendar.get(Calendar.MINUTE);
  
// Reset hit counter.
hitCount = 0;
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

// Set response content type
response.setContentType("text/html");

// This method executes whenever the servlet is hit
// increment hitCount
hitCount++;
PrintWriter out = response.getWriter();
String title = "Total Number of Hits";
String docType = "<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
Calendar calendar = Calendar.getInstance();

int newMinute = calendar.get(Calendar.MINUTE);
  
if (newMinute != currentMinute)
{
System.out.println("in if");
hitCount=1;
currentMinute=newMinute;
  
}
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor = \"#f0f0f0\">\n" +
"<h1 align = \"center\">" + title + "</h1>\n" +
"<h2 align = \"center\">" + hitCount + "</h2>\n" +
"</body>"+
"</html>"
);
}

public void destroy() {
// This is optional step but if you like you
// can write hitCount value in your database.
}
}

Web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>PageHitCounter</servlet-name>
<servlet-class>PageHitCounter</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PageHitCounter</servlet-name>
<url-pattern>/PageHitCounter</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>

OUTPUT:

Total Number of Hits

Please do comment in case of queries. Thanks!!!!!

Add a comment
Know the answer?
Add Answer to:
In JAVA assume you are designing a website, your boss asks you to design a program...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Count Occurrences in Seven Integers Using Java Single Dimension Arrays In this assignment, you will design...

    Count Occurrences in Seven Integers Using Java Single Dimension Arrays In this assignment, you will design and code a Java console application that reads in seven integer values and prints out the number of occurrences of each value. The application uses the Java single dimension array construct to implement its functionality. Your program output should look like the sample output provided in the "Count Occurrences in Seven Integers Using Java Single Dimension Arrays Instructions" course file resource. Full instructions for...

  • Information Memo: Benefit Enrollment Time Your boss, Pauline M. Wu, Human Resources, asks you to draft...

    Information Memo: Benefit Enrollment Time Your boss, Pauline M. Wu, Human Resources, asks you to draft a memo for her signature. It announces the open enrollment period for employees to change their benefit coverage. This is an important announcement because employees may make changes to their benefits only during this period. The cutoff date is November 29. all applications must be in by that time. Ms. Wu is very concerned about the attitude of employees in the past. They don't...

  • Help with java . For this project, you will design and implement a program that analyzes...

    Help with java . For this project, you will design and implement a program that analyzes baby name popularities in data provided by the Social Security Administration. Every 10 years, the data gives the 1,000 most popular boy and girl names for kids born in the United States. The data can be boiled down to a single text file as shown below. On each line we have the name, followed by the rank of that name in 1900, 1910, 1920,...

  • You will write a two-class Java program that implements the Game of 21. This is a...

    You will write a two-class Java program that implements the Game of 21. This is a fairly simple game where a player plays against a “dealer”. The player will receive two and optionally three numbers. Each number is randomly generated in the range 1 to 11 inclusive (in notation: [1,11]). The player’s score is the sum of these numbers. The dealer will receive two random numbers, also in [1,11]. The player wins if its score is greater than the dealer’s...

  • Java please At this point in your life most of you have or have had a...

    Java please At this point in your life most of you have or have had a job. One of the aspects of a job is dealing with taxes. Benjamin Franklin said “in this world nothing can be said to be certain, except death and taxes.” There are many types of taxes. Some of the most familiar taxes are state sales tax, federal and state income tax and local property tax. Write a program that will compute and print the federal,...

  • For this lab you will write a Java program that plays a simple Guess The Word...

    For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...

  • Help Please on JAVA Project: Validating the input is where I need help. Thank you Requirements...

    Help Please on JAVA Project: Validating the input is where I need help. Thank you Requirements description: Assume you work part-time at a sandwich store. As the only employee who knows java programming, you help to write a sandwich ordering application for the store. The following is a brief requirement description with some sample output. 1. Selecting bread When the program starts, it first shows a list/menu of sandwich breads and their prices, then asks a user to select a...

  • (2 bookmarks) In JAVA You have been asked to write a program that can manage candidates...

    (2 bookmarks) In JAVA You have been asked to write a program that can manage candidates for an upcoming election. This program needs to allow the user to enter candidates and then record votes as they come in and then calculate results and determine the winner. This program will have three classes: Candidate, Results and ElectionApp Candidate Class: This class records the information for each candidate that is running for office. Instance variables: first name last name office they are...

  • Today is Thursday , three days before your meeting begins. You are on-site in Rio de...

    Today is Thursday , three days before your meeting begins. You are on-site in Rio de Janeiro , and all is going well so far. Your destination management company (DMC) has been keeping track of the inevitable arrival changes. Your team is getting welcome packets together, incorporating last-minute changes into the day-by-day schedule, checking VIP amenities, and preparing for the next morning's arrival of your boss and several other senior managers. Good news! Your project coordinator has just verified that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT