javaweb04_JSP(Java Server Pages)
JSP入門:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
<p>Hello,BLU!<p>
<%!
public void test(){
System.out.println("test方法");
}
%>
<br>
<% int a=1,b=2; %>
<% System.out.println(a+b); %>
<% test(); %>
<%= a+b %>
<br>
<%= new Date().toLocaleString() %>
<br>
<% out.print("Hi, BLU!"); %>
</body>
</html>


JSP的page指令:
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*"%>
contentType="text/html; charset=UTF-8"
對應:
response.setContentType("text/html; charset=UTF-8");
頁面元素:
<%! %> 中的內容會翻譯在生成的Servlet類中
<% %> 中的內容會翻譯在Servlet的Service方法中
<%= %> 和 <%out.println()%> 效果相同
直接寫在中的值會翻譯到Servlet的Service方法中的 out.write("") 中,作為輸出內容,
include包含其他頁面的兩種方式:
<%@include file=“footer.jsp” %>
<jsp:include page=“footer.jsp” />
注:<%@include 會導致兩個jsp合并成為同一個java檔案,而<jsp:include仍然是兩個檔案,所以存在傳參問題
傳參方法:
<jsp:include page="footer.jsp">
<jsp:param name="year" value="2017" />
</jsp:include>
<%=request.getParameter("year")%>
JSP的9大內置物件(不需要顯示定義,直接就可以使用的物件):
- PageContext 代表當前頁面作用域
- Request 請求
- Response 回應
- Session 會話
- Application 即【ServletContext】
- config 即【ServletConfig】
- out 輸出內容
- page 代表當前Servlet實體,相當于this
- exception 例外
exception 的使用:
- try.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" errorPage="catch.jsp"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
int[] a = new int[10];
a[20] = 5;
%>
</body>
</html>
- catch.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" errorPage="catch.jsp" isErrorPage="true"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%=exception%>
</body>
</html>

JSP有4個作用域:
pageContext 當前頁面有效
requestContext 一次請求有效
sessionContext 當前會話有效
applicationContext 全域有效,所有用戶共享
<%
pageContext.setAttribute("name1", "BLU1");
request.setAttribute("name2", "BLU2");
session.setAttribute("name3", "BLU3");
application.setAttribute("name4", "BLU4");
%>
<%
String name1 = (String) pageContext.getAttribute("name1");
String name2 = (String) pageContext.findAttribute("name2");
String name3 = (String) pageContext.findAttribute("name3");
String name4 = (String) pageContext.findAttribute("name4");
%>
<br>
<%=name1 %>
<%=name2 %>
<%=name3 %>
<%=name4 %>
getAttribute和findAttribute的區別:
findAttribute是pageContext的方法,它會依次在page,request,session(如果有效的話)和application內查找
而getAttribute只會在當前scope(page)中查找
JSTL JSP Standard Tag Library 標準標簽庫
- 使用JSTL需要以下兩個包:
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
- 使用JSTL:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
<c:set var="name" value="${'BLU'}" scope="request"/>
<c:out value="${name}"/><br>
<c:remove var="name" scope="request"/><br>
<c:out value="${name}"/><br>
</body>
</html>
首先需要通過指令宣告使用jstl的標簽前綴:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="name" value="${'BLU'}" scope="request"/>
相當于:
<%request.setAttribute("name","BLU")%>
<c:out value="${name}"/><br>
相當于:
<%=request.getAttribute("name")%>
<c:remove var="name" scope="request"/><br>
相當于:
<%request.removeAttribute("name")%>

<body>
<c:set var="money" value="900" scope="request" />
<c:if test="${money<500}"><p>poverty</p></c:if>
<c:if test="${!(money<500)}"><p>rich</p></c:if>
<% pageContext.setAttribute("weapon", null); %>
<c:if test="${empty weapon}"><p>沒有裝備武器</p></c:if>
</body>
<body>
<%
List<String> names = new ArrayList<String>();
names.add("zhangsan");
names.add("lisi");
names.add("wangwu");
names.add("BLU");
names.add("joker");
request.setAttribute("names",names);
%>
<c:forEach items="${names}" var="name" varStatus="st">
<tr>
<td><c:out value="${st.count}" /></td>
<td><c:out value="${name}" /></td>
</tr>
</c:forEach>
</body>
items="${names}" 指定要遍歷的集合
var="name" 指定遍歷出來的元素名
varStatus="st" 表示遍歷的狀態
EL運算式
- 使用EL運算式:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
<c:set var="name" value="BLU" scope="request" />
通過標簽獲取name: <c:out value="${name}" /> <br>
通過 EL 獲取name: ${name}
</body>
</html>
使用EL運算式需要在<%@page 標簽里加上isELIgnored="false"
通過EL取值:
${name}
EL運算式可以從pageContext,request,session,application四個作用域中按照從高到低的優先級順序獲取取到值
pageContext>request>session>application
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/112502.html
標籤:其他
上一篇:Day12JavaWeb【Maven】idea與maven ***
下一篇:使用SpringCloud實作Java分布式開發【part-4】:Feign服務呼叫的介紹及使用、Feign的負載均衡和熔斷器、請求壓縮和日期級別的配置
