Cloud's SCWCD 1.4 筆記 | |||||||||||||||||||||||||||||||||||||||||
1:The Servelt Technology Model | |||||||||||||||||||||||||||||||||||||||||
1. | HttpServlet class:
public class MyServlet extends HttpServlet { public void init() throws ServletException { } protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doXXX(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } public void destroy() { } } | ||||||||||||||||||||||||||||||||||||||||
2. | Http的method POST method可用來處理上傳檔案的問題。 OPTIONS method可以列出目前處理的HTTP method為何(ex:TRACE,GET,POST)。 GET、PUT、HEAD method有idempotent的特質。 | ||||||||||||||||||||||||||||||||||||||||
3. | HTML FORM預設使用 HTTP GET request | ||||||||||||||||||||||||||||||||||||||||
4. | ServletResponse的method void setContentType(String type) PrintWriter getWriter() //取得 response 的 text stream 傳送字元資料 ServletOutputStream getOutputStream() //取得 response 的 binary stream 傳送任何資料 HttpServletResponse的method void addHeader(String headerName, String value) void addIntHeader(String headerName, int value) void addDateHeader(String headerName, long millisecs) void sendRedirect(String newURL) void sendError(int status_code) void sendError(int status_code, String Message) void setStatus(int sc) void setStatus(int sc, String Message) void addCookie(Cookie cookie) //輸入的參數型別是Cookie,要留意題意的陷阱 跟HTTP protocal相關的method屬於HttpServletResponse,其它的methods通通是屬於ServletResponse | ||||||||||||||||||||||||||||||||||||||||
5. | ServletRequest的method String getParameter(String paramName) String[] getParameterValues(String param) Enumeration getParameterNames() BufferedReader getReader() //取得 request 的 text stream 可用來接收表單上傳的文字檔案 ServletInputStream getIutputStream() //取得 request 的 binary stream 可用來接收表單上傳的任何檔案 HttpServletRequest的method String getHeader(String headerName) int getIntHeader(String name) long getDateHeader(String name) Enumeration getHeaders(String name) Enumeration getHeaderNames() Enumeration getHeaderValues(String headerName) public Cookie [] getCookies() //傳回的是Cookie的陣列,要留意題意的陷阱 跟HTTP protocal相關的method屬於HttpServletRequest,其它的methods通通是屬於ServletRequest | ||||||||||||||||||||||||||||||||||||||||
6. | Servlet life-cycle: Load class --> Creates instance of class --> calling the init method --> calling the service method --> calling the doXXX method --> calling the destroy method | ||||||||||||||||||||||||||||||||||||||||
7. | 繼承GenericServlet的class必需實作service() method 繼承HttpServlet的class可以不實作service()、doXXX() method | ||||||||||||||||||||||||||||||||||||||||
2: The Structure and Deployment of Web Applications | |||||||||||||||||||||||||||||||||||||||||
1. | 要背web.xml中紅字部份的義意與用法 <?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd” version=”2.4”> <display-name>A Simple Application</display-name> | ||||||||||||||||||||||||||||||||||||||||
2. | 要了解Web application的檔案目錄架構(ex:/WEB-INF/裡可以放那些東西...etc) | ||||||||||||||||||||||||||||||||||||||||
3: The Web Container Model | |||||||||||||||||||||||||||||||||||||||||
1. | 只有request scope的資料在傳送時是thread safe | ||||||||||||||||||||||||||||||||||||||||
2. | Filter class public class TimingFilter implements Filter { public void init(FilterConfig filterConfig) throws ServletException { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException,IOException { chain.doFilter(request, response); //這行的程式非必填的程式碼,也可以不寫 } public void destroy() { } } | ||||||||||||||||||||||||||||||||||||||||
3. | listener class 分成三類:ServletContextListener,HttpSessionListener,ServletRequestListener 注意Method的不同處:HttpSessionListener.sessionCreated(...),ServletContextListener.contextInitialized(...),ServletRequestListener.requestInitialized(...) | ||||||||||||||||||||||||||||||||||||||||
4. | attribute listener class 分成三類,名稱上相似:都叫XXXAttributeListener 提供的Method名稱一樣:attributeAdded(...),attributeRemoved(...),attributeReplaced(...) | ||||||||||||||||||||||||||||||||||||||||
5. | HttpSessionAttributeListener.某個Method(HttpSessionBindingEvent e) HttpSessionBindingListener.某個Method(HttpSessionBindingEvent e) 二者輸入參數的型別都必為HttpSessionBindingEvent | ||||||||||||||||||||||||||||||||||||||||
6. | HttpSessionBindingListener的使用不需在web.xml裡作宣告 ,要很清楚HttpSessionBindingListener的使用方式 | ||||||||||||||||||||||||||||||||||||||||
7. | listener本身是個Interface,所以可以實作一個class繼承多個不同的listener | ||||||||||||||||||||||||||||||||||||||||
8. | HttpSessionActivationListener被使用在多個JVM的環境下 | ||||||||||||||||||||||||||||||||||||||||
9. | RequestDispatcher rd=request.getRequestDispatcher(相對或絕對路徑) RequestDispatcher rd=getServletContext().getRequestDispatcher(絕對路徑) //如果設成相對路徑還是可以Compile,return值是null | ||||||||||||||||||||||||||||||||||||||||
10. | RequestDispatcher ServletRequest.getRequestDispatcher(String path) //來自ServletRequest,不是HttpServletRequest RequestDispatcher ServletContext.getRequestDispatcher(String path) | ||||||||||||||||||||||||||||||||||||||||
4: Session Management | |||||||||||||||||||||||||||||||||||||||||
1. | HttpSession的method Object getAttribute(String name) void setAttribute(String name, Object value) void removeAttribute(String name) Enumeration getAttributeNames() void invalidate() boolean isNew() void setMaxInactiveInterval(int seconds) //單位是秒,當值 < 0 時表示不會自動 timeout | ||||||||||||||||||||||||||||||||||||||||
2. | HttpServletRequest.getSession(false)在取得HttpSession時,如果HttpSession不存在,不會自動產生,會return null | ||||||||||||||||||||||||||||||||||||||||
3. | Sesssion tracking mechanisms:Cookies,SSL Sessions,URL Rewriting | ||||||||||||||||||||||||||||||||||||||||
4. | HttpServletResponse的URL rewriting Method String encodeURL(String url) String encodeRedirectURL(String url) | ||||||||||||||||||||||||||||||||||||||||
5: Web Application Security | |||||||||||||||||||||||||||||||||||||||||
1. | Web Application Security的特質有四種: (a)authentication(認証) (b)authorization(授權) (c)data integrity(資料完整性):指資料從發送方傳送到接收方,在傳送過程中資料不會被篡改。 (d)confidentiality(私密性):指除了資料接收方以外,其他人不會存取到這個敏感性資訊的能力及權限。 | ||||||||||||||||||||||||||||||||||||||||
2. | HttpServletRequest中與驗証使用者相關的Method String getRemoteUser() java.security.Principal getUserPrincipal() boolean isUserInRole(String role) | ||||||||||||||||||||||||||||||||||||||||
3. | Authentication types有下列四種 BASIC:傳輸的資料不加密(不安全),不需自訂輸入帳號與密碼的Form(Browser會pop up一個輸入的表單) DIGEST:傳輸的資料加密(安全),不需自訂輸入帳號與密碼的Form(Browser會pop up一個輸入的表單) FORM:傳輸的資料不加密(不安全),要自訂輸入帳號與密碼的Form CLIENT-CERT:用Public Key Certificate的機制(安全) | ||||||||||||||||||||||||||||||||||||||||
6: The JavaServer Pages (JSP) Technology Model | |||||||||||||||||||||||||||||||||||||||||
1. |
| ||||||||||||||||||||||||||||||||||||||||
2. |
| ||||||||||||||||||||||||||||||||||||||||
3. |
| ||||||||||||||||||||||||||||||||||||||||
4. | JSP page life cycle(JSP的本質是Servlet): JSP page translation --> JSP page compilation --> load class --> create instance --> call the jspInit method(可以被改寫)--> call the _jspService method(不能被改寫) --> call the jspDestroy method(可以被改寫) | ||||||||||||||||||||||||||||||||||||||||
5. |
| ||||||||||||||||||||||||||||||||||||||||
6. |
| ||||||||||||||||||||||||||||||||||||||||
7. | include directive不能指向 servlet,include action, forward action 可以指向 servlet | ||||||||||||||||||||||||||||||||||||||||
8. |
JSP轉譯成Servlet的class public void jspInit() { } public void _jspService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } public void jspDestroy() { } } | ||||||||||||||||||||||||||||||||||||||||
9. | <% = "Hello" ; %> //會產生exception,要了解exception的原因 | ||||||||||||||||||||||||||||||||||||||||
10. | 下列的JSP DECLARATION是合法的 <%! Hashtable ht = new Hashtable(); { ht.put("max", "10"); // { }拿掉會導致Compile error } %> | ||||||||||||||||||||||||||||||||||||||||
7: Building JSP Pages Using the Expression Language (EL) | |||||||||||||||||||||||||||||||||||||||||
1. |
| ||||||||||||||||||||||||||||||||||||||||
2. | EL operators: property access (the . operator), collection access (the [] operator). . operator只能用在物件型別為Map或JavaBean,[] operator則無任何限制 | ||||||||||||||||||||||||||||||||||||||||
3. | EL operators中比較需要注意之處 Relational : == , eq , != , ne , < , lt , > , gt , >= , ge , <= , le Empty : ${!empty param.Add} Conditional : A ? B : C | ||||||||||||||||||||||||||||||||||||||||
4. | EL function的寫法 package mypkg; public class MyLocales { ... public static boolean equals( String a, String b ) { //必為 public static return a.equals(b); } } | ||||||||||||||||||||||||||||||||||||||||
5. | EL function在xxx.tld中的宣告方式 <function> <name>equals</name> <function-class>mypkg.MyLocales</function-class> <function-signature>boolean equals( java.lang.String, java.lang.String )</function-signature> </function> | ||||||||||||||||||||||||||||||||||||||||
6. | EL expression的出題陷阱 ${10 * 1.5} //結果是15.0 ${123 / 0} //結果是Infinity ${0 / 0} //結果是NaN ${"" + 1} //結果是1 ${" " + 1 } //產生Exception ${123 % 0} //產生Exception ${0 % 0} //產生Exception | ||||||||||||||||||||||||||||||||||||||||
8: Building JSP Pages Using Standard Actions | |||||||||||||||||||||||||||||||||||||||||
1. |
| ||||||||||||||||||||||||||||||||||||||||
2. |
| ||||||||||||||||||||||||||||||||||||||||
3. | <jsp :useBean id="persion" type="scwcd.Person" class="scwcd.Employee" /> //scwcd.Person是scwcd.Employee的super class,利用此方法可以達到polymorphic的效果 | ||||||||||||||||||||||||||||||||||||||||
4. | 在<jsp:useBean ... />中 If type is used wtihout class,the bean must already exit.不然會產生exception. If class is used(with or without type) the class must Not be abstract or interface,and must have a public no-arg constructor <jsp:useBean id="mybean" beanName="my.app.MyBean" type="my.app.MyBean" />,beanName只能與type同時出現 | ||||||||||||||||||||||||||||||||||||||||
5. | <% out.flush() %> <jsp:forward .../> 此種寫法會使forward失效,不被執行,而且不會產生exception | ||||||||||||||||||||||||||||||||||||||||
9: Building JSP Pages Using Tag Libraries | |||||||||||||||||||||||||||||||||||||||||
1. | JSTL的Core Tag Library <c:out value="${username}" escapeXml="false" /> //要了解escapeXml代表的義意 <c:set var="four" scope="session" value="${3+1}" /> <c:remove var="dommed" scope="session" /> <c:catch var="e"> //var的值可以在catch block之外被使用,<c:catch>的功能與try...catch寫法中的catch相似 ... Program code that may throw an exception ... </c:catch> <c:if test="${e != null}"> The caught exception is <c:out value="${e}"/> </c:if> <c:if test="${e == null}"> No exception was thrown </c:if> <c:choose> <c:when test="${error1}"> Error1 </c:when> //一定要有test這個attribute <c:when test="${error2}"> Error2 </c:when> <c:when test="${error3}"> Error3 </c:when> <c:otherwise> Otherwise </c:otherwise> //不能有test這個attribute,此外<c:otherwise>這個tag非必需 </c:choose> <c:forEach items="${user.medicalConditions}" var="aliment"> <c:out value="${aliment}"/> //如果這行改成<%=aliment %>會產生exception,不能用scriptling來存取<c:forEach>中的值 </c:forEach> <c:forTokens items="a;b;c;d" delims=";" var="current"> <c:out value="${current}"/> </c:forTokens> <c:url value="buy.jsp"> //<c:url>的功能是url rewriting,不是url encoding,url帶參數時要用範例中的寫法 <c:param name="stock" value="IBM"/> </c:url> <c:import context="/other" url="/directory/target.jsp"/> //可以import其它Web application的的資料 <c:param name="first" value="one"/> <c:param name="second" value="two"/> </c:import> <c:redirect context="/brokerage" url="/buy.jsp"> //可以redirect其它Web application的的資料 <c:param name="stock" value="IBM"/> </c:redirect> | ||||||||||||||||||||||||||||||||||||||||
2. | <c:set target="${myDog}" property="dogName" value="可魯" /> target必需放Object,不能是String。利用此法可以設定Bean的property。 | ||||||||||||||||||||||||||||||||||||||||
10: Building a Custom Tag Library | |||||||||||||||||||||||||||||||||||||||||
1. | tag file與simple tag相似,tag file在執行前會先被轉換成simple tag才被執行。二者在接收到新的request時都會產生新的instance。二者的body content設定都不能設為JSP | ||||||||||||||||||||||||||||||||||||||||
2. | tag support與body tag support相似,二者都只產生一個instance被多個request共用(跟Servlet的架構相似) | ||||||||||||||||||||||||||||||||||||||||
3. | classic tag
| ||||||||||||||||||||||||||||||||||||||||
4. | tag support與body tag support用 PageContext class 取得 implicit variable simple tag用getJspContext() method取得 implicit variable | ||||||||||||||||||||||||||||||||||||||||
5. | 抓取parent tag的method Tag getParent() static Tag findAncestorWithClass (Tag from, java.lang.Class klass) | ||||||||||||||||||||||||||||||||||||||||
6. | Simple tag class的寫法 public class MySimpleTag extends SimpleTagSupport { StringWriter sw = new StringWriter(); public void doTag() throws JspException, IOException { getJspContex().getOut().write(sw.toString()); getJspBody().invoke(null); } } Tag support class的寫法(body tag support class的寫法與它相似) public class MyTag extends TagSupport { public int doStartTag() throws JspException{ ... return SKIP_BODY/EVAL_BODY_INCLUDE; } public int doAfterBody() throws JspException{ ... return SKIP_BODY/EVAL_BODY_AGAIN ; } public int doEndTag() throws JspException{ ... return SKIP_PAGE/EVAL_PAGE; } } 要了解simple tag與classic tag二者在寫法上的不同處 | ||||||||||||||||||||||||||||||||||||||||
7. | Tag file的寫法 <%@ attribute name="fontColor" required="true" rtexprvalue="true" %> <%@ tag body-content="empty/tagdependent/scriptless"%> <font color="${fontColor}"><jsp:doBody/></font><br> Tagfile可以使用的directives: taglib, include, tag, attribute, variable. | ||||||||||||||||||||||||||||||||||||||||
8. | tag class在xxx.tld的宣告方式 <uri>...</uri> <tag> <name>callEJB</name> <tag-class>com.example.web.tags.EJBCallTagHandler</tag-class> <body-content>empty/tagdependent/scriptless/JSP</body-content> //設成empty時,如果JSP檔裡的tag body卻包含資料,會在執行時產生Exception //設成scriptless時,如果JSP檔裡的tag body卻包含scripting的敘述,會在執行時產生Exception //設成tagdependent的話表示不作任何處理,直接將本體內容傳給標籤庫,由標籤庫自行處理本體內容 <attribute> <name>user</name> <required>true/false/yes/no</required> //設成true時,如果JSP檔裡面沒有設定這個attribute會在執行時產生Exception <rtexprvalue>true</rtexprvalue> //設成false時,如果JSP檔裡面有rtexprvalue的寫法會在執行時產生Exception </attribute> </tag> | ||||||||||||||||||||||||||||||||||||||||
9. | SkipPageException stops only the page thata directly invoked the simple tag | ||||||||||||||||||||||||||||||||||||||||
10. | Simp tag life cycle Load class --> Instantiate class --> call setJspContext() method --> call setParent() method --> call attribute setters(不一定會執行) --> call setJspBody()(不一定會執行) --> call doTag() | ||||||||||||||||||||||||||||||||||||||||
11. | classic tag life cycle Load class --> Instantiate class --> call setPageContext() method --> call setParent() method --> call attribute setters(不一定會執行) --> call doStartTag() --> call doAfterBody() --> call doEndTag() | ||||||||||||||||||||||||||||||||||||||||
12. | SimpleTag在xxx.tld中的<body-content>設定不能設為JSP,body content不可以使用sriptlet的語法 Tag file的<%@ tag body-content="..."%>不能設為JSP,body content不可以使用sriptlet的語法 | ||||||||||||||||||||||||||||||||||||||||
13. | <prefix:sometag> <jsp:attribute name='attrA'>val1</jsp:attribute> //不能寫成<jsp:attribute name='attrA' value='val1' />,要注意 </prefix:sometag> | ||||||||||||||||||||||||||||||||||||||||
14. | public class MyTag extends TagSupport { public int doStartTag() throws JspException{ if(somecondition) { return EVAL_BODY_INCLUDE; } else { PageContext.forward(...) or PageContext.include(...) //有些考題會在這部份作陷井,要注意 } } ...etc } | ||||||||||||||||||||||||||||||||||||||||
15. | If we extend BodyTagSupport, then we use BodyTagSupport.getPreviousOut() to get the "previous" or "enclosing" JspWriter. | ||||||||||||||||||||||||||||||||||||||||
16. | 如果tag class被包在jar file裡面。在使用tag class之前不需在web.xml作taglib的設定。只要在JSP檔裡宣告<%@ taglib prefix...etc %>就可以使用這個tag | ||||||||||||||||||||||||||||||||||||||||
11: J2EE Patterns | |||||||||||||||||||||||||||||||||||||||||
1. | Business Delegate Pattern(參照HeadFirstSevlet&JSP p:746) | ||||||||||||||||||||||||||||||||||||||||
2. | Service Locator Pattern(參照HeadFirstSevlet&JSP p:747) | ||||||||||||||||||||||||||||||||||||||||
3. | Transfer Object Pattern(參照HeadFirstSevlet&JSP p:748) | ||||||||||||||||||||||||||||||||||||||||
4. | Intercepting Filter Pattern(參照HeadFirstSevlet&JSP p:749) | ||||||||||||||||||||||||||||||||||||||||
5. | MVC Pattern(參照HeadFirstSevlet&JSP p:750) | ||||||||||||||||||||||||||||||||||||||||
6. | Front Controller Pattern(參照HeadFirstSevlet&JSP p:751) | ||||||||||||||||||||||||||||||||||||||||
12: 雜七雜八無法分類的部份 | |||||||||||||||||||||||||||||||||||||||||
1. | Servlet與Filter class寫法相似,二者在web.xml的設定方式亦相似 | ||||||||||||||||||||||||||||||||||||||||
2. | EL function與tag class二者在xxx.tld的設定方式相似 | ||||||||||||||||||||||||||||||||||||||||
3. | 要能分出下面三者的差異 <%@ include file="myTest.jsp" %> //Static的include方式 <jsp:include page="myTest.jsp"/> //dynamic的include方式 <c:import url="/myTest.jsp" context="/OtherSite"/> //dynamic的include方式,而且可以inlcude不同Web application的資料 | ||||||||||||||||||||||||||||||||||||||||
4. | 在Deployment Descriptoer中呼叫的先後順序:listener->filter->servlet | ||||||||||||||||||||||||||||||||||||||||
5. | Both - GenericServlet class and ServletContext interface, provide methods log(String msg) and log(String, Throwable) that write the information to a log file. | ||||||||||||||||||||||||||||||||||||||||
6. | JSP Page directive中比較少見的attribute:language,extends,buffer,autoFlush,info | ||||||||||||||||||||||||||||||||||||||||
7. | java.net.URL ServletContext.getResource() java.io.InputStream ServletContext.getResourceAsStream() | ||||||||||||||||||||||||||||||||||||||||
8. | ServletException provides the getRootCause() method that returns the wrapped exception. It's return type is Throwable, so you have to cast it to appropriate business exception type. | ||||||||||||||||||||||||||||||||||||||||
9. | <servlet-mapping> <servlet-name>XServlet</servlet-name> <url-pattern>*.x</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>YServlet</servlet-name> <url-pattern>/y/*</url-pattern> </servlet-mapping>
要了解HeadFirstSevlet&JSP p:588的考題練習 | ||||||||||||||||||||||||||||||||||||||||
10. | Request URL = protocol://host:port + contextpath + requestpath + pathinfo |
註:紅字部份是特別要注意的考題陷井