目录
10.2 JSTL标签库:(需要导入配置文件),JSTL依赖EL
10.3 Servlet系列之过滤器和监听器
10.3.1 过滤器:
过滤器案例:
作用:帮助我们在jsp页面中添加复杂的逻辑判断,避免逻辑代码和页面标签混为一谈
学习网站:https://www.runoob.com/jsp/jsp-jstl.html
JSTL标签库分为:
与上面一样,先导入jsp、servlet包:
新建服务器中:(如果要新建服务器就需要改)
将包放在该目录下:(jstl、standard包)
jsp文件:jstl
添加:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
//导包(美元转换)
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
Title //页面名称
//value:填写要输出的值
//default:默认值
<c:out value="哈哈" default="嘿嘿">c:out>
<c:out value="${ str}" default="嘿嘿">c:out>
<c:out value="${ str2}" default="嘿嘿">c:out>
=====================================================================
//向作用域设置属性值
(默认最后一个)
(如果没写scope,默认的是第一个)
//删除作用域的数据
//var:表示参数的key
//scope:删除指定作用域的数据
//逻辑判断标签
<%
int i = 10;
if(i>5){
%>
jstl is easy
<%}%>
=====================================================================
//test:填写逻辑判断表达式
//var:条件表达式的结果存储变量
//cope:结果变量存储的作用域
<c:set var="a" value="6">c:set>
//<c:if test="${a>5}">
<c:if test="${a>5}" var="flag" scope="request">
=====================================================================
小孩
青少年
青年
中年
i don't know
===============================================================================
//边框1px
1
111
2
222
3
333
===============================================================================
//相当于:for(int i = 0;i<=3;i++)
${sta.index}---${sta.count}----${sta.first}---${sta.last}
===============================================================================
//begin:起始值
//end:结束值
//step:步长
//varStatus:循环状态的变量值名称
//var:集合数据的每条记录的迭代值
//items:从作用域中获取的数据
<%
ArrayList list = new ArrayList();
list.add("aaa");
list.add("bbb");
list.add("ccc");
request.setAttribute("list",list);
%>
${i}
===============================================================================
<%
HashMap map = new HashMap();
map.put("1","111");
map.put("2","222");
map.put("3","333");
map.put("4","444");
map.put("5","555");
map.put("6","666");
request.setAttribute("map",map);
%>
//边框1px
${i.key}
${i.value}
===============================================================================
//边框1px
数字格式化:
格式化数字 (1):
格式化数字 (2):
格式化数字 (3):
格式化数字 (4):
格式化数字 (5):
格式化数字 (6):
格式化数字 (7):
格式化数字 (8):
美元 :
的生命周期:
导包:
web.xml文件:
FilterServlet
com.mashibing.servlet.FilterServlet
FilterServlet
/filter
java文件:FilterServlet(过滤程序)
public class FilterServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
System.out.println("我是filterservlet");
=====================================================================
HttpSession session = request.getSession();
session.setAttribute("user","user");
System.out.println("我是filterservlet");
response.getWriter().write("我是filterservlet");
}
}
FilterServlet2
com.mashibing.servlet.FilterServlet2
FilterServlet2
/filter2
java文件:FilterServlet2(过滤程序2)
public class FilterServlet2 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
System.out.println("我是filterservlet2");
response.getWriter().write("我是filterservlet2");
}
}
过滤器的生命周期:
========================= 第一个过滤器 =============================
java文件:MyFilter(过滤器)
重写其中的方法:
init():完成初始化功能 tomcat启动的时候执行一次
dofilter():进行处理 每次发送请求都会执行
destory():销毁功能 tomcat关闭的时候执行
public class MyFilter implements Filter {
============================== 初始化 ===============================
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("我是filter init"); //只执行一次
}
=============================== 处理 ================================
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("我是filter逻辑处理");
//添加此语句之后才能调用到对应的servlet
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("filter逻辑处理完成");
}
=============================== 销毁 ================================
@Override
public void destroy() {
System.out.println("我是filter销毁功能");
}
}
web.xml文件:(必须写最上面)
filter
com.mashibing.filter.MyFilter
filter
/*
========================= 第二个过滤器 =============================
java文件:MyFilter2(过滤器2)
public class MyFilter2 implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("第二个过滤器初始化");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("第二个过滤器开始执行");
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("第二个过滤器执行结束");
}
@Override
public void destroy() {
System.out.println("第二个过滤器销毁");
}
}
web.xml文件:(接着上面)
filter2
com.mashibing.filter.MyFilter
filter2
*/
filterchain:过滤器链
web.xml文件:(接着上面)
filter2
com.mashibing.filter.MyFilter2
filter2
*.do
建议:每个过滤器完成独立的功能,不要讲所有的逻辑处理放置到同一个过滤器中,耦合性高,不利于维护
web.xml文件:
//设置编码过滤器
encoding
com.mashibing.filter.EncodingFilter
encoding
/*
//登录过滤器
login
com.mashibing.filter.LoginFilter
login
*.do
LoginServlet
com.mashibing.servlet.LoginServlet
LoginServlet
/login.do
java文件:LoginServlet(登录程序)
public class LoginServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("loginservlet的处理过程");
response.getWriter().write("登陆成功");
}
}
java文件:EncodingFilter(编码过滤器)
public class EncodingFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("编码过滤器的初始化");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("编码过滤器的逻辑处理");
servletRequest.setCharacterEncoding("utf-8");
servletResponse.setContentType("text/html;charset=utf-8");
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("编码过滤器执行完成");
}
@Override
public void destroy() {
System.out.println("编码过滤器被销毁");
}
}
java文件:LoginFilter(登录过滤器)
public class LoginFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("登录过滤器初始化");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("登录过滤器处理");
HttpSession session = ((HttpServletRequest)servletRequest)
.getSession();
if(session.getAttribute("user")==null){
((HttpServletResponse)servletResponse).sendRedirect("login.jsp");
}else{filterChain.doFilter(servletRequest,servletResponse);}
System.out.println("登录过滤器处理完成");
}
@Override
public void destroy() {
System.out.println("登录过滤器被销毁");
}
}