JSP、Servlet、EL表达式简单实现网站统计

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 {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected 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大对象:

对象名 类名


request HttpServletRequest

response HttpServletResponse

pageContext pageContext

session HttpSession

application servletContext

out jspWriter

config servletConfig

page Object

exception Throwable


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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值