Simple JSP exercises-website counters and jsp exercise counters

Source: Internet
Author: User

Simple JSP exercises-website counters and jsp exercise counters

<% @ Page contentType = "text/html; charset = gb2312" %> <% @ page import = "javax. servlet. * "%> 

The program uses the synchronize keyword to serialize the counting function (serialized in some books) to ensure that there is no conflict when the two clients simultaneously access the webpage and modify the counting value; getServletContext () method to obtain the application object, because some Web servers do not directly support the application object, you must first obtain its context; if it is still the first client to access, the number obtained in the preceding code is null, so the initial value is 1. Otherwise, 1 is added. If a new session is used, the counting function is called, obtain the Count value and display it.

It can be found that when the page is refreshed, its value does not increase. Only when all the windows of the website are closed and accessed again, it will increase by 1, because this is a new session.



Simple jsp web page counters

That's definitely not enough, because the resolution range of your web server (tomacat or apache) does not include your image location. The simplest way is to create a folder for storing images in the project root directory, for example, images, and put all the images you use in the folder.
Modify it to: out. print (".

How to add counters for jsp websites

A JSP Website access counter using TXT text (without any database)

// Counter. java: A bean for reading and writing files
======================================
// The txt format counter for website read/write operations
Package net.com. util;

Import java. io .*;

Public class Counter extends Object {
Private String currentRecord = null; // variable for saving text
Private BufferedReader file; // BufferedReader object, used to read file data
Private String path; // complete file path name
Public Counter (){
}
// The ReadFile method is used to read the data in the file filePath and return the data.
Public String ReadFile (String filePath) throws FileNotFoundException
{
Path = filePath;
// Create a new BufferedReader object
File = new BufferedReader (new FileReader (path ));
String returnStr = null;
Try
{
// Read a row of data and save it to the currentRecord variable
CurrentRecord = file. readLine ();
}
Catch (IOException e)
{// Handle errors
System. out. println ("Data reading error .");
}
If (currentRecord = null)
// If the file is empty
ReturnStr = "no record ";
Else
{// The file is not empty.
ReturnStr = currentRecord;
}
// Return the data to read the file
Return returnStr;
}
// The ReadFile method is used to write the counter + 1 data to the filePath of the text file.
// To increase the count
Public void WriteFile (String filePath, String counter) throws FileNotFoundException
{
Path = filePath;
// Convert counter to int type and add one
Int Writestr = Integer. parseInt (counter) + 1;
Try {
// Create a PrintWriter object for writing data to a file
PrintWriter pw = new PrintWriter (new FileOutputStream (filePath ));
// Print the integer Writestr in text format
Pw. println (Writestr );
// Clear the PrintWriter object
Pw. close ();
} Catch (IOException e ){
// Handle errors
System. out. println ("file writing error" + e. getMessage ());
}
}

}

... The remaining full text>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.