uniapp开发app框架在提升开发效率中的独特优势与应用探索
609
2023-01-30
Servlet实现统计页面访问次数功能
本文实例为大家分享了Servlet实现统计页面访问次数的具体代码,供大家参考,具体内容如下
实现思路:
1.新建一个CallServlet类继承HttpServlet,重写doGet()和doPost()方法;
2.在doPost方法中调用doGet()方法,在doGet()方法中实现统计网站被访问次数的功能,用户每请求一次servlet,使得访问次数times加1;
3.获取ServletContext,通过它的功能记住上一次访问后的次数。
在web.xml中进行路由配置:
//CallServlet为处理前后端交互的后端类
CallServlet类:
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created with IntelliJ IDEA
* Details about unstoppable_t:
* User: Administrator
* Date: 2021-04-07
* Time: 14:57
*/
//获得网站被访问的次数
public class CallServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
ServletContext context = getServletContext();
Integer timeUPOgkoxlPjs = (Integer) context.getAttribute("times");
if (times == null) {
times = new IntUPOgkoxlPjeger(1);
} else {
times = new Integer(times.intValue() + 1);
}
PrintWriter out= resp.getWriter();
out.println("
out.println("页面访问统计");
out.println("
out.println("当前页面被访问了");
out.println(""+times+"次");
context.setAttribute("times",times);
}
@OverrideUPOgkoxlPj
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
前端展示结果:
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~