Java——Web開發之JSP,EL與JSTL的使用

JSP:從用戶角度來看是一個網頁,從開發人員來看就是一個java類,繼承了servlet,所以實際上就是一個servlet。

 

1.jsp的三大指令

指令的寫法
    <%@ 指令名字 %>

1.page指令

  • language:表面jsp頁面可以寫java代碼
  • contentType:告訴瀏覽器這個文件是什麼內容類型以及使用什麼編碼

              contentType="text/html; charset=UTF-8"

              text/html MIMEType 這是一個文本,html網頁

  • pageEncoding:jsp的內容編碼
  • extends:用於指定jsp翻譯成java文件後繼承的父類,一般不用修改
  • import:導包,一般不手寫,用快捷鍵alt+/
  • session:值這能選true或false,用於控制在這個jsp頁面中是否能夠直接使用session對象
  • errorPage:指的是錯誤的頁面,值需要給錯誤的頁面路徑   ,用於指定錯誤的時候跑到哪一個頁面去
  • isErrorPage:用於聲明某個頁面到底是不是錯誤的頁面

2.include指令:包含另外一個jsp的內容進來

  • 靜態包含:把另外一個頁面的所有內容拿過來一起輸出,所有的標籤元素都包含進來
  • 動態包含:使用jsp的動態標籤(jsp:include),    只把它的運行結果拿過來 

3.taglib指令:

  • <%@ taglib prefix="" url="" %>
  • uri:標籤庫路徑
  • prefix:標籤庫的別名

 

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="index2.jsp" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>
	hello index<br>
	
	收到的參數爲:<br>
	<%=
		request.getParameter("age")
	 %>
</body>
</html>

2.JSP的動作標籤:

  • include:能包含第二個jsp文件
  • forward:請求轉發,前往到指定頁面
  • param:在包含或者跳轉到某個頁面的時候,加入這個參數

 

action.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="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>
	<%--
	<jsp:include page=""></jsp:include>
	<jsp:param value="" name=""/>
	<jsp:forward page=""></jsp:forward>
	 --%>
	 
	 <%--
	 能包含第二個jsp文件<br>
	<jsp:include page="index2.jsp"></jsp:include>
	hello action!<br>
	
	請求轉發,前往到指定頁面<br>
	<jsp:forward page="index.jsp"></jsp:forward>
	等同於
	<%
		request.getRequestDispatcher("index.jsp").forward(request,response);
	 %>
	 
	 --%>
	 
	 在index.jsp中獲取參數<br>
	 <jsp:forward page="index.jsp">
	 	<jsp:param value="22" name="age"/>
	 </jsp:forward>
	  
</body>
</html>

 

3.jsp內置對象:我們可以直接在jsp頁面中使用這些對象,不需要創建

  • 作用域對象:作用域指的是這些對象可以存值,它們的取值範圍有限定。通過setAttribute和getAttribute方法進行值的操作
  • pageContext(PageContext):作用域僅限於當前頁面
  • request(HttpServletRequest):作用域僅限於一次請求,只要服務器對該請求做出了響應,這個域中的值就沒有了
  • session(HttpSession):作用域限於一次會話(多次請求與響應)
  • application(ServletContext):整個工程都可以訪問,當服務器關閉後就不能訪問了
  •     
  • response(HttpResponse):與out同時出現時,先輸出的是response的內容
  • out(JspWriter):out對象輸出的內容放到response的緩衝區
  • config(ServletConfig):處理異常
  • page(Object):這個jsp翻譯成的java類的實例對象
  • exception(Throwable):獲取初始化的值

object.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="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>
	
	使用作用域來存儲數據<br>
	<%
	
		pageContext.setAttribute("name","page");
		request.setAttribute("name","request");
		session.setAttribute("name","session");
		application.setAttribute("name","application");
		
	 %>
	 取出四個作用域中的值<br>
	 <%=  pageContext.getAttribute("name")%>
	 <%=  request.getAttribute("name")%>
	 <%=  session.getAttribute("name")%>
	 <%=  application.getAttribute("name")%>
	
	跳轉到下一個頁面<br>
	<%
		//請求轉發,一次請求
		//request.getRequestDispatcher("object2.jsp").forward(request,response);
		//重定向,兩次請求
		response.sendRedirect("object2.jsp");
	 %>
</body>
</html>

object2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="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>
	hello object2<br>
	<%=  pageContext.getAttribute("name")%>
	<%=  request.getAttribute("name")%>
	<%=  session.getAttribute("name")%>
	<%=  application.getAttribute("name")%>
</body>
</html>

 

error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isErrorPage="true"%>
<!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>
	hello error<br>
	<%
		config.getInitParameter("name");
	 %>
	
	<%
		out.write("hello error1!");
	%>
	<br>
	<%
		response.getWriter().write("hello error2!");
	 %>
</body>
</html>

 

EL表達式:爲了簡化jsp代碼,實際上就是簡化在jsp裏面寫的java代碼。

寫法:${表達式}

EL表達式:爲了簡化jsp代碼,實際上就是簡化在jsp裏面寫的java代碼
寫法:${表達式}
1).普通情況下取值取出四個作用域中存放的值
2).如果域中所存的值是數組時的EL取值
3).如果域中所存的值是集合時的EL取值
4).如果域中所存的值是map時的EL取值
取值方式:如果有下標,那麼直接用[],沒有下標則用.的方式去取
EL表達式一般用在從一個對象中取出它的屬性值

el.jsp

<%@page import="jsp.domain.User"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="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>
	<%
		pageContext.setAttribute("name","page");
		request.setAttribute("name","request");
		session.setAttribute("name","session");
		application.setAttribute("name","application");
	 %>
	普通情況下取值<br>
	<%=  pageContext.getAttribute("name")%>
	<%=  request.getAttribute("name")%>
	<%=  session.getAttribute("name")%>
	<%=  application.getAttribute("name")%>
	<br>
	
	使用EL表達式取出作用域中的值<br>
	${ pageScope.name }------直接指定到這個作用域裏面去找這個name
	${ requestScope.name }
	${ sessionScope.name }
	${ applicationScope.name }
	
	<br>
	如果域中所存的值是數組時的EL取值<br>
	<%
		String[] array = {"aa","bb","cc","dd"};
		pageContext.setAttribute("array",array);
	 %>
	
	${array[0] },${array[1] },${array[2] },${array[3] }
	<br>
	
	如果域中所存的值是集合時的EL取值<br>
	<%
		List list=new ArrayList();
		list.add("11");
		list.add("22");
		list.add("33");
		pageContext.setAttribute("li",list);
	 %>
	 
	 ${li[0] },${li[1] },${li[2] }
	 <br>
	 
	 如果域中所存的值是map時的EL取值<br>
	<%
		Map map=new HashMap();
		map.put("a","1");
		map.put("b","2");
		map.put("c","3");
		map.put("c.qq","4");
		pageContext.setAttribute("map",map);
	 %>
	 
	 ${map.a },${map.b },${map.c },${map["c.qq"] }	
	 <br>
	
	如果域中所存的值是自定義User對象時的EL取值<br>
	<%
		User user=new User("aaaa",28);
		session.setAttribute("u",user);
	 %>
	${ u.name },${ u.age } <br>
	
	---判斷user對象是否爲空<br>
	${ empty u }
		
</body>
</html>

 

EL表達式的11個內置對象:${ 對象名.成員 }
作用域相關:pageScope,requestScope,sessionScope,applicationScope
請求頭相關:header,headerValues
請求參數相關:param,paramValues
全局初始化參數:initParam
其他:cookie,pageContext

 

JSTL:jsp的標籤庫,用來簡化jsp的代碼編寫,替換<%%>寫法,一般與EL表達式配合。

使用步驟:
1.導入JSTL支持jar文件jstl.jar和standard.jar(使用1.1的jstl,1.0不支持el表達式)
2.在jsp頁面上使用taglib來引入標籤庫

jstl.jsp

<%@page import="jsp.domain.User"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>
	聲明一個對象,對象的值,進行存儲 <br>
	<c:set var="name" value="qq"></c:set>//會默認存到page域裏<br>
	<c:set var="name2" value="qq2" scope="session"></c:set>
	${ name }<br>
	${ sessionScope.name2 }<br>
	
	<c:set var="age" value="12"></c:set>
	//判斷test裏面的EL表達式是否滿足,如果滿足,就執行c:if標籤中輸出,可以定義一個變量名flag,去接收前面表達式的值,然後存在session域中<br>
	<c:if test="${age>10 }" var="flag" scope="session">age大於10歲</c:if>	<br>		
	${flag }<br>
	
	//用於作遍歷,從1開始到10,得到的結果賦值給i,並且會存儲到page域中, step代表的是增幅爲2<br>
	<c:forEach begin="1" end="10" var="i" step="2"> 
		${i }
	</c:forEach>
	<br>
<%
	List list=new ArrayList();
	list.add(new User("aa",11));
	list.add(new User("bb",22));
	list.add(new User("cc",33));
	list.add(new User("dd",44));
	pageContext.setAttribute("list",list);
 %>
 <br>items表示要遍歷哪一個對象,這裏必須寫EL表達式,遍歷出來的每一個元素用user取接收<br>
	<c:forEach var="user" items="${list }">		
		${user.name },${user.age }....
	</c:forEach>
	
</body>
</html>

 

在測試的過程中用到自己定義的User類:

user.java

package jsp.domain;
public class User {
	private String name;
	private int age;
	public User(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	private User() {
		super();	
	}
}

 

感謝每一位閱讀的人~

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章