四大指令元素
1.頁面指令
頁面指令用來定義jsp文件中的全局屬性。一個頁面可以包含多個頁面指令
1 <%@ page 2 定義要使用的腳本語言:[language="java"] 3 需要引入的包:[import="java.util.Date, java.util.ArrayList"] 4 JSP字符編碼,頁面響應類型:[contentType="text/html";charset=CHARSET] 5 指定一個Http回話,該頁面是否參與:[session="true|false"] 6 指定到客戶輸出緩沖流模式:[buffer="none|8kb|otherBuffSize"] 7 緩沖區滿時,是否刷新:[autoFlush="true|false"] 8 是否能夠多線程:[isThreadSafe="true|false"] 9 關於jsp頁面信息:[info='text'] 10 當前頁是否可為其他頁面錯誤訊息頁:[isErrorPage="true|false"] 11 該頁面錯誤時使用的錯誤信息頁:[errorPage="relativeURL"] 12 [extends="package.class"] 13 制定EL是否被忽略:[isELIgnored="true|false"] 14 頁面字符編碼:[pageEncoding="peinfo"] 15 16 %>
2.include指令
1 <%@ include file="jspfilename" %>
該包含為靜態包含,file1包含file2,則將file2的代碼插入至file1的指定位置作為一個jspfile,然后再編譯。兩個文件中變量會復用。
3.taglib
我不懂
4.表達式語言
參考其他人的吧
三大腳本元素
1.聲明 <%! %>
聲明是一段java代碼,可以用來定義類或方法
1 <%! 2 class Student{ 3 private int id; 4 private String name; 5 6 public Student(){} 7 8 public Student(int id, String name){ 9 this.id = id; 10 this.name = name; 11 } 12 13 public String getName(){ 14 return name; 15 } 16 17 public int getId(){ 18 return id; 19 } 20 } 21 %>
2.scriplets <% %>
jsp頁面處理請求時執行的代碼,可以產生輸出,也可以時流程控制語句
<% Student s1 = new Student(1,"pig"); out.println(s1.getName()); %>
2.scriplets <%= some java expression %>
<%=s1.getId() %>
八大動作元素
1.param <jsp:param name="paramName" value="paramValue">
jsp:param操作被用來以“名-值”對的形式為其他標簽提供附加信息,通常與<jsp:include> <jsp:forward> <jsp:plugin>組合使用。
2.include <jsp:include page="fiilename" flush="true">
jsp:include操作允許在請求時間內,讓現成的jsp頁面中包含靜態或動態資源。被包含的對象只有對jsp writer對象的訪問權,並且不能設置頭或cookie。如果頁面輸出是緩沖的,那么緩沖區的刷新要優於包含的刷新。
實例1.
<!-- copyRight.jsp -->>
<hr/>
©2012 Slowalker Lee
<hr/>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="java.util.Date" %> <!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=ISO-8859-1"> <title>Insert title here</title> </head> <body> <%=new Date() %> <jsp:include page="copyRight.jsp"/> </body> </html>
實例2.
主頁面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<jsp:include page="tow.jsp">
<jsp:param name="name" value="slowalker"/>
</jsp:include>
</body>
</html>
<!-- 被包含頁面 --> <%="the parameter is " + request.getParameter("name")%>
運行結果:
the parameter is slowalker
3. <jsp:forward page="filename">
將請求轉發到另一個頁面。
驗證登錄的例子。
confirmLogIn.jsp
<!--confirmLogIn.jsp 登錄的主界面-->
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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=ISO-8859-1"> <title>Insert title here</title> </head> <body> <form method="post" action="checkLoginIn.jsp"> <table> <tr> <td>user name:</td><td><input type="text" name="name"></td> </tr> <tr> <td>password :</td><td><input type="password" name="passwd"></td> </tr> <tr><td><input type="submit" value="submit"></td></tr> </table> </form> </body> </html>
<!--登錄檢查 后台執行 --> <% int a = 0; try{ a = Integer.parseInt(request.getParameter("passwd")); }catch (NumberFormatException e){} if (request.getParameter("name").equals("slowalker") && (a == 123)){ %> <jsp:forward page="success.jsp"></jsp:forward> //賬戶密碼正確,跳轉至成功界面 <% }else{ %> <jsp:forward page="confirmLogIn..jsp"></jsp:forward> //賬號密碼不正確,跳轉至登錄界面重新填寫賬號密碼 <% } %>
<!-- 登錄成功界面-->
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>success</h1>
</body>
</html>
4.<jsp:useBean>
<jsp:useBean id="id" scope="page|request|session|application" typeSpec>
id:一個大小寫相關的名字,在所定義的范圍中確認Bean的變量
/ page:能夠在包含<jsp:useBean>標簽的JSP文件以及此文件中的所有靜態包含文件使用Bean
| request:在請求范圍內使用有效 request.getAttribute(name) name是bean實例化的名字
scope:此對象可使用的范圍 { session.getValues(name) name是bean實例化的名字
| session:從創建Bean開始,就可以在session有效范圍內使用這個Bean,這個Bean對JSP來說是共享的。
\ application:從創建Bean開始,就可以在aplication有效范圍內使用這個Bean,這個Bean對JSP來說是共享的。
application對象在應用服務器啟動時創建,直至服務器關閉。
application.getAttribute(name) name是bean實例化的名字
/ class="className" bean的類路徑和類名,這個class不能是抽象的。必須有一個公用的無參構造器。
| class="className" type="typeName" 使用instantiate方法從一個class中實例化一個Bean,同時可以指定Bean類型
typeSpec {
| beanName="beanName" type="typeName"
\ type="typeName"
1 package com.servlet.test; 2 3 public class TestBean { 4 public String userName; 5 public String password; 6 public int age; 7 8 public TestBean() {} //Bean中必須有一個無參構造器 9 10 public String getUserName() { 11 return userName; 12 } 13 14 public void setUserName(String userName) { 15 this.userName = userName; 16 } 17 18 public String getPassword() { 19 return password; 20 } 21 22 public void setPassword(String password) { 23 this.password = password; 24 } 25 26 public int getAge() { 27 return age; 28 } 29 30 public void setAge(int age) { 31 this.age = age; 32 } 33 34 35 }
1 <!--regisiter.html--> 2 <!DOCTYPE html> 3 <html> 4 <head> 5 <meta charset="UTF-8"> 6 <title>Insert title here</title> 7 </head> 8 <body> 9 <form method="post" action=register.jsp> 10 <table> 11 <tr> 12 <td>姓名:<input name="userName" type="text"/></td> <!--此處的userName,password,age必須和Bean中定義的屬性的大小寫一致--> 13 </tr> 14 <tr> 15 <td>密碼:<input name="password" type="password"/></td> 16 </tr> 17 <tr> 18 <td>年齡:<input name="age" type="text"/></td> 19 </tr> 20 <tr> 21 <td><input type="submit" value="submit"></td> 22 </tr> 23 </table> 24 </form>> 25 </body> 26 </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <jsp:useBean id="user" scope="page" class="com.servlet.test.TestBean" />
<!-- 實例化一個名為user的TestBean類的對象,有效范圍為本頁 -->
5 //<jsp:setProperty property="*" name="user"/> 6 <html> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 9 <title>Insert title here</title> 10 </head> 11 <body> 12 注冊成功<hr> 13 <hr> 14 使用Bean的屬性方法:<br> 15 用戶名:<%=user.getUserName() %> 16 密碼:<%=user.getPassword() %> 17 年齡:<%=user.getAge() %> //此處的用戶名 密碼 年齡均為之前填寫的值 18 19 </body> 20 </html>
關於Bean的猜想:調用一個包含Bean的頁面時,<jsp:useBean id="user" scope="page" class="com.servlet.test.TestBean" /> 此句實例化出一個名為user的TestBean對象
沒有搞懂useBean的執行過程
5.setProperty
6.getProperty
7.plugin
8.fallback