In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use servlet to achieve statistics of web page visits, the article is very detailed, has a certain reference value, interested friends must read it!
I. basic knowledge
(1) the difference between ServletContext and ServletConfig
ServletContext as the shared data of the whole web application
ServletConfig is only used as the data sharing of the current servlet. When the next servlet accesses, it will not be accessible.
Second, code implementation
Display the statistics displayed on the HTML page:
Import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class countServlet1 * / @ WebServlet ("/ countServlet1") public class countServlet1 extends HttpServlet {private static final long serialVersionUID = 1L / * * @ see HttpServlet#HttpServlet () * / public countServlet1 () {super (); / / TODO Auto-generated constructor stub} / * @ see HttpServlet#doGet (HttpServletRequest request, HttpServletResponse response) * / protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {/ / set character encoding request.setCharacterEncoding ("utf-8") Response.setCharacterEncoding ("utf-8"); response.setContentType ("text/html; charset=utf-8"); / / get global shared data ServletContext servletContext = this.getServletContext (); / / get counter count Integer count = (Integer) servletContext.getAttribute ("count") / / if the acquired calculator object is empty, this is the first visit, and put count into servletCount if (servletContext.getAttribute ("count") = = null) {count = 1; servletContext.setAttribute ("count", count) } else {/ / otherwise it is not the first time to visit, add 1 to the login counter to update servletContext.setAttribute ("count", count+1);} / / display the number of logins on the page PrintWriter out = response.getWriter () Out.print ("\ r\ n" + "\ r\ n" + "login times statistics\ r\ n" + "\ r\ n" + "); out.print (") Out.print ("you are the first" + servletContext.getAttribute ("count") + "visitor"); out.print (") Out.print ("\ r\ n" + "} / * * @ see HttpServlet#doPost (HttpServletRequest request, HttpServletResponse response) * / protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {/ / TODO Auto-generated method stub doGet (request, response);} 3. The number of times displayed in different browsers
(1) the number of times displayed in eclipse
(2) the number of times displayed in Firefox
(3) the number of times displayed in 360
The above is all the contents of the article "how to use servlet to count the number of web page visits". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.