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)