Servlet 实现网页计数器
创建CounterSerlet, 使用getServletContext,ServletContext 从request.getSession().getServletContext();获得
@WebServlet("/CounterServlet")
public class CounterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public CounterServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = request.getSession().getServletContext();
if(null == context.getAttribute("counter")){
context.setAttribute("counter", 1);
}else {
int count = (Integer)context.getAttribute("counter");
context.setAttribute("counter", count + 1);
}
request.getRequestDispatcher("counter.jsp").forward(request, response);
}
/**
* @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);
}
}
2. counter.jsp
jsp里对应的对象就是application
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%= application.getAttribute("counter") %>
</body>
</html>
Servlet 实现网页计数器的更多相关文章
- 【PHP】php+txt实现网页计数器(限IP统计方式和不限IP统计方式)
一般的网页计数器制作实现思路:首先设定存放统计数据的文件(counter.txt)——读取文件中的内容存入字符串——自加操作——以写入方式打开文件写入数据——从文件中输出统计数据——关闭文件. 代码: ...
- Servlet 点击计数器
网页点击计数器 很多时候,您可能有兴趣知道网站的某个特定页面上的总点击量.使用 Servlet 来计算这些点击量是非常简单的,因为一个 Servlet 的生命周期是由它运行所在的容器控制的. 以下是实 ...
- 2017.11.4 JavaWeb-----基于JavaBean+JSP求任意两数代数和(改进的在JSP页面中无JSP脚本代码的)+网页计数器JavaBean的设计与使用
修改后的JSP中不含有JSP脚本代码这使得JSP程序的清晰性.简单 1.设计JavaBean 的Add.java 类 package beans; public class Add { private ...
- 用servlet在网页中打印字符串(初接触)、servlet调用过程
一.servlet是什么: 二.在官方文档中点servlet 这就是servlet的方法,这里说一下什么叫生命周期的方法(life-cycle methods):就是这个对象一旦创生之后一定会执行的方 ...
- Servlet 教程 各个知识点简单概括
Servlet 教程 http://www.ziqiangxuetang.com/servlet/servlet-cookies-handling.html Servlet 教程 Servlet 教程 ...
- servlet-servletContext网站计数器
1.在项目中新建文件夹新建文件nums.txt 2.在web.xml文件配置 <servlet> <description>This is the descriptio ...
- Servlet 教程——检视阅读
Servlet 教程--检视阅读 参考 Servlet教程--菜鸟--蓝本 Servlet教程--w3cschool Servlet教程--易百 servlet依赖maven依赖: <!--se ...
- Java Servlet——改进的CGI
一.关于Servlet 在上一篇随笔中,我们看到了CGI存在的不足,其每次请求都需加载和运行一个CGI程序.若使用Java编写CGI程序,需要为每个请求都启动一个系统进程以及JVM,其执行效率大大降低 ...
- 网页设计——2. html入门
开始正式的课程讲解了,首先来看看课程体系: Java EE(java 企业应用程序版本) java2 有三个版本:J2 SE(标准版),J2 EE(企业版).J2 ME(微缩版). 我们要掌握J2EE ...
随机推荐
- 【转】在SQL Server中创建用户角色及授权(使用SQL语句)
1. 首先在 SQL Server 服务器级别,创建登陆帐户(create login) --创建登陆帐户(create login) create login dba with password=' ...
- Vue--Vue.nextTick()的使用
Vue.nextTick()是比较常用到的API Vue官网对它的解释是:在下次 DOM 更新循环结束之后执行延迟回调.在修改数据之后立即使用这个方法,获取更新后的 DOM. 首先要明白Vue的响应式 ...
- python数据类型高阶
python是近年来使用最广泛的一种编程语言,不管是做web开发,还是网络爬虫,亦或是数据分析等,大家都在选择python来完成这些任务:我想最重要一点就是python学起来很简单,另一个点就是pyt ...
- Xshell5 Xftp安装图解
1Xshell5 Xftp_5安装图解 2.1Xshell5安装 2.2Xftp安装
- 51nod1693
题解: 首先将问题转化,可以发现操作改为两种 一种是s*=k,代价为k,一种是s--,代价为1 转化成图论,spfa跑最短路 然后更据一些证明,代价1的k<=13且为质数,并且不可能操作2连续5 ...
- [转载]python的常用代码模板
URL:http://blog.csdn.net/xingjiarong/article/details/50651235
- python函数def
#函数就是实现一个特定功能的程序块 #下面这个函数实现了打印helloworld的功能 def print_hello(): print('hello world!') #这是调用方法 print_h ...
- IO文件相关操作
IO编程 IO 即Input/Output input stream 就是数据从外面(磁盘.网络)流进内存,output stream 就是数据从内存流到外面去. 通常cpu 和 内存的速度远远高于 ...
- DevExpress WPF入门指南:DXWindow应用
[DevExpress v17.2 版本更新公开课]点击免费报名 DevExpress WPF Window control有一点非常棒,就是可以和其他视觉主题保持统一性.DXWindow class ...
- float、clear、overflow
浮动: float: none|left|right 作用使得标签失去块级标签的独占一行效果,向某个方向靠拢 标签浮动了,也需要占地方,有时候出现未浮动的div覆盖部分浮动div是浏览器的bug情况 ...