<%@ page contentType="text/html;charset=UTF-8" language="java" %>EL表達(dá)式快速入門 <%-- 設(shè)置共享數(shù)據(jù)--%><% request.setAttribute("username","zhangsan"); %>java 代碼塊:<%out.println("username");%>
java 表達(dá)式:<%=request.getAttribute("username")%>
EL 表達(dá)式:${username}
1.3 EL 表達(dá)式獲取數(shù)據(jù)<%@ 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表達(dá)式獲取不同類型數(shù)據(jù) <%-- 1. 獲取基本數(shù)據(jù)類型--%><% pageContext.setAttribute("num",10); %>基本數(shù)據(jù)類型:${num}
<%-- 2. 獲取自定義數(shù)據(jù)類型--%><%
Student stu = new Student("張三",23);
pageContext.setAttribute("stu",stu);
%>自定義數(shù)據(jù)類型:${stu}
學(xué)生姓名:${stu.name}
學(xué)生年齡:${stu.age}<%-- 3. 獲取數(shù)組類型--%><%
String[] arr = {"hello","world"};
pageContext.setAttribute("arr",arr);
%>數(shù)組:${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}
第一個(gè)學(xué)生對(duì)象:${map.stu01}
第一個(gè)學(xué)生對(duì)象的姓名:${map.stu01.name}
1.4 EL 表達(dá)式注意事項(xiàng)<%@ page contentType="text/html;charset=UTF-8" language="java" %>EL表達(dá)式運(yùn)算符 <%-- empty--%><%
String str1 = null;
String str2 = "";
int[] arr = {};
%>${empty str1}
${empty str2}
${empty arr}
<%-- 三元表達(dá)式--%><%
pageContext.setAttribute("gender","women");
%>gender == "men" ? "checked":""} />男gender == "women" ? "checked":""} />女
1.6 EL 表達(dá)式使用細(xì)節(jié)<%@ page contentType="text/html;charset=UTF-8" language="java" %>EL使用細(xì)節(jié) <%
// pageContext.setAttribute("username","張三");
request.setAttribute("username","張三");
// session.setAttribute("username","張三");
// application.setAttribute("username","張三");
%>${username}
<%--獲取 JSP 其他八個(gè)隱式對(duì)象,并調(diào)用其方法--%>${pageContext.request.contextPath}
1.7 EL 表達(dá)式隱式對(duì)象<%@ page contentType="text/html;charset=UTF-8" language="java" %>EL表達(dá)式11個(gè)隱式對(duì)象 <%--pageContext對(duì)象 可以獲取其他三個(gè)域?qū)ο蠛蚃SP中八個(gè)隱式對(duì)象--%>${pageContext.request.contextPath}
<%--applicationScope sessionScope requestScope pageScope 操作四大域?qū)ο笾械臄?shù)據(jù)--%><% request.setAttribute("username","zhangsan"); %>${username}
${requestScope.username}
<%--header headerValues 獲取請(qǐng)求頭數(shù)據(jù)--%>${header["connection"]}
${headerValues["connection"][0]}
<%--param paramValues 獲取請(qǐng)求參數(shù)數(shù)據(jù)--%>${param.username}
${paramValues.hobby[0]}
${paramValues.hobby[1]}
<%--initParam 獲取全局配置參數(shù)--%>${initParam["pname"]}
<%--cookie 獲取cookie信息--%>${cookie}
<%--獲取Map集合--%>${cookie.JSESSIONID}
<%--獲取map集合中第二個(gè)元素--%>${cookie.JSESSIONID.name}
<%--獲取cookie對(duì)象的名稱--%>${cookie.JSESSIONID.value}<%--獲取cookie對(duì)象的值--%>
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"%>流程控制 <%-- 向域?qū)ο笾刑砑訑?shù)據(jù)--%>${pageContext.setAttribute("score","T")}
<%-- 對(duì)成績進(jìn)行判斷--%> 優(yōu)秀 <%-- 對(duì)成績進(jìn)行多條件判斷--%>優(yōu)秀 良好 及格 較差 成績非法
<%@ page contentType="text/html;charset=UTF-8" language="java" %><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>循環(huán) <%
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執(zhí)行了...");
resp.getWriter().write("ServletDemo01執(zhí)行了...");
}
@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執(zhí)行了...");
resp.getWriter().write("ServletDemo02執(zhí)行了...");
}
@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執(zhí)行了...");
servletResponse.setContentType("text/html;charset=UTF-8");
filterChain.doFilter(servletRequest,servletResponse);
}
}
3.5 過濾器使用細(xì)節(jié)@WebFilter(攔截路徑)
public class FilterDemo03 implements Filter {@Override
public void init(FilterConfig filterConfig) throws ServletException {System.out.println("對(duì)象創(chuàng)建了...");
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執(zhí)行了...");
servletResponse.setContentType("text/html;charset=UTF-8");
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {System.out.println("對(duì)象銷毀了...");
}
}
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("監(jiān)聽到了對(duì)象的創(chuàng)建...");
ServletContext servletContext = sce.getServletContext();
System.out.println(servletContext);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {System.out.println("監(jiān)聽到了對(duì)象的銷毀...");
}
}
@WebListener
public class ServletContextAttributeListenerDemo implements ServletContextAttributeListener {@Override
public void attributeAdded(ServletContextAttributeEvent scae) {System.out.println("監(jiān)聽到了屬性的添加...");
ServletContext servletContext = scae.getServletContext();
Object username = servletContext.getAttribute("username");
System.out.println(username);
}
@Override
public void attributeRemoved(ServletContextAttributeEvent scae) {System.out.println("監(jiān)聽到了屬性的移除...");
ServletContext servletContext = scae.getServletContext();
Object username = servletContext.getAttribute("username");
System.out.println(username);
}
@Override
public void attributeReplaced(ServletContextAttributeEvent scae) {System.out.println("監(jiān)聽到了屬性的修改...");
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("監(jiān)聽到了對(duì)象的創(chuàng)建...");
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("監(jiān)聽到了對(duì)象的銷毀...");
}
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧