十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
<%@ page contentType="text/html;charset=UTF-8" language="java" %>EL表达式快速入门 <%-- 设置共享数据--%><% request.setAttribute("username","zhangsan"); %>java 代码块:<%out.println("username");%>
java 表达式:<%=request.getAttribute("username")%>
EL 表达式:${username}
1.3 EL 表达式获取数据<%@ page import="com.txt.bean.Student" %><%@ page import="java.util.ArrayList" %><%@ page import="java.util.HashMap" %><%@ page contentType="text/html;charset=UTF-8" language="java" %>EL表达式获取不同类型数据 <%-- 1. 获取基本数据类型--%><% pageContext.setAttribute("num",10); %>基本数据类型:${num}
<%-- 2. 获取自定义数据类型--%><%
Student stu = new Student("张三",23);
pageContext.setAttribute("stu",stu);
%>自定义数据类型:${stu}
学生姓名:${stu.name}
学生年龄:${stu.age}<%-- 3. 获取数组类型--%><%
String[] arr = {"hello","world"};
pageContext.setAttribute("arr",arr);
%>数组:${arr}
0索引元素:${arr[0]}
1索引元素:${arr[1]}
<%-- 4. 获取List集合--%><%
ArrayListlist = new ArrayList<>();
list.add("aaa");
list.add("bbb");
pageContext.setAttribute("list",list);
%>List集合:${list}
0索引元素:${list[0]}
<%-- 5. 获取Map集合--%><%
HashMapmap = new HashMap<>();
map.put("stu01",new Student("张三",23));
map.put("stu02",new Student("李四",24));
pageContext.setAttribute("map",map);
%>Map集合:${map}
第一个学生对象:${map.stu01}
第一个学生对象的姓名:${map.stu01.name}
1.4 EL 表达式注意事项<%@ page contentType="text/html;charset=UTF-8" language="java" %>EL表达式运算符 <%-- empty--%><%
String str1 = null;
String str2 = "";
int[] arr = {};
%>${empty str1}
${empty str2}
${empty arr}
<%-- 三元表达式--%><%
pageContext.setAttribute("gender","women");
%>gender == "men" ? "checked":""} />男gender == "women" ? "checked":""} />女
1.6 EL 表达式使用细节<%@ page contentType="text/html;charset=UTF-8" language="java" %>EL使用细节 <%
// pageContext.setAttribute("username","张三");
request.setAttribute("username","张三");
// session.setAttribute("username","张三");
// application.setAttribute("username","张三");
%>${username}
<%--获取 JSP 其他八个隐式对象,并调用其方法--%>${pageContext.request.contextPath}
1.7 EL 表达式隐式对象<%@ page contentType="text/html;charset=UTF-8" language="java" %>EL表达式11个隐式对象 <%--pageContext对象 可以获取其他三个域对象和JSP中八个隐式对象--%>${pageContext.request.contextPath}
<%--applicationScope sessionScope requestScope pageScope 操作四大域对象中的数据--%><% request.setAttribute("username","zhangsan"); %>${username}
${requestScope.username}
<%--header headerValues 获取请求头数据--%>${header["connection"]}
${headerValues["connection"][0]}
<%--param paramValues 获取请求参数数据--%>${param.username}
${paramValues.hobby[0]}
${paramValues.hobby[1]}
<%--initParam 获取全局配置参数--%>${initParam["pname"]}
<%--cookie 获取cookie信息--%>${cookie}
<%--获取Map集合--%>${cookie.JSESSIONID}
<%--获取map集合中第二个元素--%>${cookie.JSESSIONID.name}
<%--获取cookie对象的名称--%>${cookie.JSESSIONID.value}<%--获取cookie对象的值--%>
2 JSTL
2.1 JSTL 介绍<%@ page contentType="text/html;charset=UTF-8" language="java" %><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>流程控制 <%-- 向域对象中添加数据--%>${pageContext.setAttribute("score","T")}
<%-- 对成绩进行判断--%> 优秀 <%-- 对成绩进行多条件判断--%>优秀 良好 及格 较差 成绩非法
<%@ page contentType="text/html;charset=UTF-8" language="java" %><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>循环 <%
ArrayListlist = new ArrayList<>();
list.add("aa");
list.add("bb");
list.add("cc");
list.add("dd");
pageContext.setAttribute("list",list);
%>${str}
3 Filter
3.1 过滤器介绍@WebServlet("/servletDemo01")
public class ServletDemo01 extends HttpServlet {@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("ServletDemo01执行了...");
resp.getWriter().write("ServletDemo01执行了...");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);
}
}
@WebServlet("/servletDemo02")
public class ServletDemo02 extends HttpServlet {@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("ServletDemo02执行了...");
resp.getWriter().write("ServletDemo02执行了...");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);
}
}
@WebFilter("/*")
public class FilterDemo01 implements Filter {@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {System.out.println("filterDemo01执行了...");
servletResponse.setContentType("text/html;charset=UTF-8");
filterChain.doFilter(servletRequest,servletResponse);
}
}
3.5 过滤器使用细节@WebFilter(拦截路径)
public class FilterDemo03 implements Filter {@Override
public void init(FilterConfig filterConfig) throws ServletException {System.out.println("对象创建了...");
String filterName = filterConfig.getFilterName();
System.out.println(filterName);
String username = filterConfig.getInitParameter("username");
System.out.println(username);
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {System.out.println("filterDemo01执行了...");
servletResponse.setContentType("text/html;charset=UTF-8");
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {System.out.println("对象销毁了...");
}
}
filterDemo03 com.txt.filter.FilterDemo03 username zhangsan filterDemo03 /*
3.8 过滤器的五种拦截行为@WebListener
public class ServletContextListenerDemo implements ServletContextListener {@Override
public void contextInitialized(ServletContextEvent sce) {System.out.println("监听到了对象的创建...");
ServletContext servletContext = sce.getServletContext();
System.out.println(servletContext);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {System.out.println("监听到了对象的销毁...");
}
}
@WebListener
public class ServletContextAttributeListenerDemo implements ServletContextAttributeListener {@Override
public void attributeAdded(ServletContextAttributeEvent scae) {System.out.println("监听到了属性的添加...");
ServletContext servletContext = scae.getServletContext();
Object username = servletContext.getAttribute("username");
System.out.println(username);
}
@Override
public void attributeRemoved(ServletContextAttributeEvent scae) {System.out.println("监听到了属性的移除...");
ServletContext servletContext = scae.getServletContext();
Object username = servletContext.getAttribute("username");
System.out.println(username);
}
@Override
public void attributeReplaced(ServletContextAttributeEvent scae) {System.out.println("监听到了属性的修改...");
ServletContext servletContext = scae.getServletContext();
Object username = servletContext.getAttribute("username");
System.out.println(username);
}
}
@WebListener
public class ServletContextListenerDemo implements ServletContextListener {@Override
public void contextInitialized(ServletContextEvent sce) {System.out.println("监听到了对象的创建...");
ServletContext servletContext = sce.getServletContext();
servletContext.setAttribute("username","zhangsan");
servletContext.setAttribute("username","lisi");
servletContext.removeAttribute("username");
// System.out.println(servletContext);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {System.out.println("监听到了对象的销毁...");
}
}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧