JSP、Servlet、EL表达式简单实现网站统计
来源:互联网 发布:司法考试鲁迅姓周 知乎 编辑:程序博客网 时间:2024/05/21 10:31
1、先看我的项目结构
2、依赖jar包,使用tomcat/lib/jsp-api.jar、servlet-api.jar
3、准备index.jsp页面
<%@ page language="java" contentType="text/html; charset=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><body><div align="right">网站统计:总共<%=application.getAttribute("totle") == null ? 0 : application.getAttribute("totle")%>次</div><br><form action="${pageContext.request.contextPath}/hello" method="post" ><input type="text" name="userName" value="" autocomplete="off"><br><input type="password" name="userPswd" value="" ><br><input type="submit" value="提交"></form></body></body></html>
4、web.xml文件配置
<?xml version="1.0" encoding="ISO-8859-1"?><web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <servlet> <servlet-name>HelloServlet</servlet-name> <servlet-class>com.hhj.test.ServletTest</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping></web-app>
5、书写ServletTest类
package com.hhj.test;import java.io.IOException;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;public class ServletTest extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {this.doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {//实现用户访问量大的统计,比如限制用户20次的抽奖,使用session对象//直接从req中获取session,有两种:getSession() 和getSession(boolean true/false)//getSession() === getSession(true) 如果已经存在session就使用旧的session,如果没有则创建一个新session,//getSession(false)如果已经存在session用旧的,如果没有返回null//实现网站的总体统计量,使用context对象ServletContext context = req.getSession().getServletContext();String totle = (String) context.getAttribute("totle");if (null == totle) {context.setAttribute("totle", "1");} else {int sum = Integer.parseInt(totle);sum++;context.setAttribute("totle", Integer.toString(sum));}int count = 0;HttpSession session = req.getSession();String limitUserCount = (String) session.getAttribute("limitUserCount");if (null == limitUserCount) {session.setAttribute("limitUserCount", "1");} else {count = Integer.parseInt(limitUserCount);count++;session.setAttribute("limitUserCount", Integer.toString(count));}if (count < 3) {req.getRequestDispatcher("/myPage/success.jsp").forward(req, resp);} else {resp.sendRedirect("myPage/over.jsp");}}}
6、准备跳转的页面
1.success.jsp
<%@ page language="java" contentType="text/html; charset=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><div align="right">网站统计:总共<%=application.getAttribute("totle")==null ? 0 : application.getAttribute("totle")%>次个人访问量:<%=session.getAttribute("limitUserCount") == null ? 0 : session.getAttribute("limitUserCount")%>${sessionScope.limitUserCount}<!-- sessionScope为El表达式内置的session对象 --></div><br>${param.userName}<!-- param为EL表达式接收输入的内置对象 --><%=request.getParameter("userName") %>登陆成功,访问次数为<%=session.getAttribute("limitUserCount") == null ? 0 : session.getAttribute("limitUserCount")%></body></html>
2.over.jsp
<%@ page language="java" contentType="text/html; charset=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><div align="right">网站统计:总共<%=application.getAttribute("totle")%>次</div><br>超过100次访问,当前访问次数为<%=session.getAttribute("limitUserCount") %></body></html>
显示结果为:
JSP内置9大对象:
对象名 类名
requestHttpServletRequest
responseHttpServletResponse
pageContextpageContext
sessionHttpSession
applicationservletContext
outjspWriter
configservletConfig
pageObject
exceptionThrowable
EL表达式四大范围对象:
pageScope < requestScope < sessionScope < applicationScope
用法为${pageScope .name},其他三个类似
EL表达式输入对象:
param: $(param . name) 相当于 request.getParameter (name)
paramValues: ${paramvalues. name) 相当于 request.getParamterValues(name)
header: ${header. name} 相当于 request.getHeader(name)
headerValues: ${headerValues. name} 相当于 request.getHeaderValues(name)
0 0
- JSP、Servlet、EL表达式简单实现网站统计
- [Servlet&JSP] 表达式语言EL
- 简单谈谈JSP/EL表达式
- 品茶看<Servlet&JSP>-EL表达式
- EL表达式语言(Servlet+jsp)
- 项目实战之使用Servlet+JSP+XML(EL表达式版)实现联系人功能
- JSP简单练习-EL表达式操作JavaBean
- 2.2Head First Servlet & Jsp——EL表达式原理
- JavaEE学习笔记之Servlet/JSP—EL表达式
- jsp+servlet四、El表达式以及C标签
- Servlet学习笔记--统计网站的访问量(jsp/servlet)
- Servlet-Jsp、EL、JSTL
- 一些servlet/jsp/el
- servlet之el表达式
- 简单网站人数统计实现
- Servlet监听器实现网站在线人数统计
- Servlet过滤器实现统计网站访问量
- JSP表达式语言(EL)
- 111111111111111
- ios中对KVO、KVC的一些理解
- Scala学习笔记4 (Object-Oriented Programming)
- 33333333333333333
- Android Tricks
- JSP、Servlet、EL表达式简单实现网站统计
- 光传感器和距离传感器TMD22713源代码执行过程分析
- flex android开发中关于请求报文技术操作问题
- BST删除操作
- tips
- 构架师之路
- php中global和$GLOBALS[]的分析
- Android刷机教程——ZIP包
- 解析各大电子商务网站订单号的生成方式