對presentation logic也可以reuse
Tag handler分為兩種: classic tag interface & Simple tag interface
使用tag library需要URI
URI有三種:
absolute http://xx.xx:8080/xx
root relative /xx/xx
Non-root relative xx/xx
如果是用JSP syntax:
<%@ taglib prefix="test" url="sampleLib.tld"%>
如果是用XML syntax:
<jsp:root
xmlns:jsp="http://..........."
xmlns:test="sampleLib.tld"
version="2.0">
JSP PAGE.....
< /jsp:root>
由於要避免一旦改了tld檔,每個jsp的路徑都得改
所以可以在web.xml設定
如果tld檔被放在JAR檔裡面的話,要取名為taglib.tld
taglib map
分為explicit mapping,implicit mapping,well-know mapping
explicit lib:
在web.xml裡面宣告
<taglib>
<taglib-uri>
http://xx.xx/studykit
</taglib-uri>
<taglib-location>
/myLibs/studyKit.tld 或 yourLibs/sample.jar
</taglib-location>
</taglib>
< >
mapping 次序
如果有找到對應的mapping
root relative就從application找起
non-root relative就從application/WEB-INF找起
如果在web.xml中沒找到對應的mapping
absolute就產生error
root relative就從application找起
non-root relative就從jsp檔案的地方找起
tag分為四種
Empty tag
tag with attributes
tag with JSP code (body)
tag with nested tag (switch & case)
JSP Standard Tag Library(JSTL)
下面有core,xml,fmt,sql,functions等library
這邊只介紹core
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
catch:
一般exception是送到error page,如果tag之間的code發生exception,會傳到var裡定義的variable
out:
有點像expression
set:
EL不能從各種scope中set跟remove attribute
所以可以用這個action來代替
可以使用
<c:set var="num" value="${4x4}"/>
或
<c:set var="num">
${4x4}
</c:set>
而用在Map或JavaBean的時候
<c:set target="customer1" property="zipcode">
44124
</c:set>
或
<c:set target="customer1" property="zipcode" value="44124"/>
remove:
<c:remove var="num" scope="session"/>
如果沒設scope,預設page->request->session->application
if:
用test來設定true和false,沒有 else
choose:
裡面包when跟otherwise,跟switch-case一樣
forEach:
兩種用法
第一種
用begin end step來做for迴圈
第二種
類似for (int element : collection)
只是變成
<c:forEach var="element" items="${collection}">
<c:set var="element" value="100"/>
</c:forEach>
forToken:
類似forEach,不過是用在String上,用delims隔開
參數有var,items,delims
url,import,redirect:
可以夾param在tag之間(c:param name=xx value=xx)
tld檔
taglib
tlib
jsp-version
short-name
uri 可以implicit設定uri,在JAR檔的狀況會自動產生mapping
tag 可以多個,也可以不同tag用同個class
name
tag-class
body-content
description
attribute
name
required 是否mandatory
rtexprvalue 是否可以用requestion time expression
body-content有三種
empty 中間不可以有東西,不過可以<></>
JSP 可以有text,html,script,action,或別的custom tag
tagdependent 用別的語言
2008年10月2日 星期四
JSTL
JavaBean
JavaBean的條件
1.有個public且沒有argument的constructor
2.每個變數都有public getXXX和setXXX的function
如果有implement java.io.Serializable,就可以將JavaBean給serialize
副檔名會用.ser, 檔案會被放在/WEB-INF或其子目錄下
然後可以用java.beans.Beans.instantiate()來實例化
jsp:useBean
id 例如address
scope (預設是page scope)
class 例如BusinessAddress
type 例如AddressBean (可用父class或inteface)
beanName 例如businessData.John 或 AddressBean
搭配組合:
class
type
class 和 type
beanName 和 type
(beanName可以餵request time expression,class不行)
在用beanName的時候
還是會先check 設定的scope裡面有沒有要的bean(所以才需要type)
沒有的話,才去檔案裡找
只用type的話
如果依據id找到的bean不合type,會丟出ClassCastException
如果找不到,會丟出java.lang.InstantiaionException
用jsp:useBean包起來的指令,可用來作initial
這些指令只有在scope 裡找不到bean的時候,才會執行
jsp:setProperty
name 同useBean的id
property property名稱
value 手動assign
param 從request得到
value和param不能同時有
都沒有的話代表用跟property同名的param
此時也可以將property設為* , 則會從request中設所有的property
如果request中找不到,則就沒事發生
jsp:getProperty
只有name跟property
jsp:getProperty只能用來print
如果要用在程式中或request time expression
只能透過Java的function來呼叫,也就是getXXX()
由於request傳遞的parameter只能是String
所以如果JavaBean裡面不是String的話,jsp engine會自動作轉換
除此之外,一般的狀況也會作自動轉換
但對於request time expression, javaBean就不提供自動轉換的功能
而JavaBean也可以接受array
一般request裡面的parameter有多個value的時候
Javabean若是一般variable,便會只接受第一個
要是是Array,便會全部接受
(String to Char的轉換是只取String的第一個字母)
而將array get出來則只會print出array的位址
(這時就得用Java的function來呼叫)
2008年10月1日 星期三
EL in JSP
EL是用 ${vara} 來print出vara
可是EL不能declare variable
只能用現有的implicit varialbe
要用
EL的implicit variable:
pageContext
pageScope
requestScope
sessionScope
applicationScope
param
paramValues
header
headerValues
cookies
除了pagecontext以外,都是Map
它並沒有給直接的access到HttpSession等...
EL operators
${header["host"]} 跟
${header['host']} 跟
${header.host} 是一樣的
tag library descriptor
要用function的話,要用tld
裡面包括
function
name
function-class
function-signature
然後在JSP裡面用directive的taglib prefix="myString" uri="URL....."
便可以用${myString:functionName()}來呼叫
2008年9月28日 星期日
JSP
JSP有兩種Syntax: JSP page與JSP document
這兩種不能寫在同一個檔案
JSP page: Standard JSP syntax format
Directive <%@ %>
page
import,session,errorPage,isErrorPage等....
include
static的include
taglib
Declaration <%! %>
只執行一次,可在此複寫jspInit()跟jspDestory()
Scriptlet <% %>
一般code
Expression <%= %>
後面不加分號
Action <jsp:actionName />
tag lib也這樣call
Comment <%-- --%>
JSP document: XML syntax format
整個code要用
<jsp:root xmlns:jsp="http://......"
xmlns:myLib="http://......" //這個不一定要
version="....">
</jsp:root>
包起來
原本的三個directives 只剩兩個page與include
<jsp:directive.page ..... />
<jsp:directive.include ..... />
因為taglib在root中宣告
而其他指令也變成
<jsp:declarative> Code </jsp:declarative>
<jsp:sciptlet> Code </jsp:sciptlet>
<jsp:expression> Code </jsp:expression>
request time attribute expression則是 %= ...%
例如 <jsp:include page="%=pageURL%">
另外在XML格式中 static文字輸出要指令(http tag不用)
<html><body>
<jsp:text> 文字HERE </jsp:text>
</body></html>
JSP implicit variable
application
session
request
response
out
page
pageContext
config
exception
p.s. 這些implicit variable只能在_jspService()中使用
也就是只能在sciptlets或expressions中,不能在declaration中
原本jsp檔是不需要deployment的,可以直接用.jsp來access
若要使用deployment來使用一些initial parameter
就會得使用/servlet/servletName來access
除了request、session、application三個scope以外
JSP還提供了page這個scope(pageContext這個variable)
並提供一些方便的function,不用透過request等三個scope variable來取用parameter
如 void setAttribute(String name,Object object,int scope);
其中scope為 APPLICATION_SCOPE SESSION_SCOPE......等
JSP include
分為兩種, static include 與dynamic include
static include使用directive的指令,在translate time就include好
也就是<%@ include file="xxx.jsp" %> 或 < jsp:directive.include file="xxx.jsp">
這種只能include HTML, XML , JSP, 而不能include servlet
而dynamic的include就等同於request用getRequestDispatcher的forward跟include
或用pageContext.include 跟 pageContext.forward
指令為<jsp:include page="xxx.jsp" flush="true"/>
forward則沒有flush , flush不使用的話,預設為false
dynamic的include與forward則可以對servlet 來include
在<jsp:include page="xxx.jsp"> </jsp:include>裡面可以包
<jsp:param name="xxx" value="xxx"/>
來從request中加入parameter,如果原本已經有這個parameter
會將新的插在前面,後面的要用getParameterValues才出的來
另外如果新插入的也有重複,順序是跟呼叫的順序一樣
例如request傳A , include加入b,c , 結果印出來會是 b,c,a
注意!! static的是用file , dynamic是用page
< >
2008年9月27日 星期六
Secure Web Application
Key Word:
Authentication: 證實,證明(確定user的身分)
Authorization: 授權,批准(確定user的權限)
Data integrity: 防止資料在傳輸中被變更,用hash code
Confidential: 資料保密
Auditing : 紀錄log
Authentication 方法
HTTP Basic 最一般,沒加密
HTTP Digest 有加密,可是只有ie可用
HTTPS Client 用SSL加密,但需要certification
HTTP FORM-based FORM版的Basic
user的name,password,role在conf\tomcat-users.xml中定義
而authentication的方法在web.xml中定義
Declarative Security方法
Authentication: 證實,證明(確定user的身分)
Authorization: 授權,批准(確定user的權限)
Data integrity: 防止資料在傳輸中被變更,用hash code
Confidential: 資料保密
Auditing : 紀錄log
Authentication 方法
HTTP Basic 最一般,沒加密
HTTP Digest 有加密,可是只有ie可用
HTTPS Client 用SSL加密,但需要certification
HTTP FORM-based FORM版的Basic
user的name,password,role在conf\tomcat-users.xml中定義
而authentication的方法在web.xml中定義
<login-config>
<auth-method> BASIC or FORM ... </auth-method>
如果是BASIC
<realm-name> sales </realm-name>
如果是FORM
<form-login-config>
<form-login-page> /formlogin.html </form-login-page>
<form-error-page> /formerror.html </form-error-page>
</form-login-config>
</login-config>
Declarative Security方法
<security-constraint>
<web-resource-collection>
<web-resource-name>
<url-pattern> /servlet/SaleReportServlet/* </url-pattern>
<url-pattern> /servlet/HttpReportServlet/* </url-pattern>
<http0-method> POST </http-method>
</web-resource-name>
</web-resource-collection>
待續
</security-constraint>
訂閱:
文章 (Atom)