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
原创粉丝点击