方法:
1. void clear() ;
清除輸出緩沖區的內容,但是不輸出到客戶端。
2. void clearBuffer() ;
清除輸出緩沖區的內容,并輸出到客戶端。
3. void close() ;
關閉輸出流,清除所有內容。
4. void flush() ;
輸出緩沖區里面的數據。
5. int getBufferSize() ;
獲取以kb為單位的目前緩沖區大小。
6. int getRemaining() ;
獲取以kb為單位的緩沖區中未被占用的空間大小。
7. boolean isAutoFlush() ;
是否自動刷新緩沖區。
8. void newLine() ;
輸出一個換行字符。
9. void print( boolean b ) ;
void print( char c ) ;
void print( char[] s ) ;
void print( double d ) ;
void print( float f ) ;
void print( int i ) ;
void print( long l ) ;
void print( Object obj ) ;
void print( String s ) ;
將指定類型的數據輸出到Http流,不換行。
10. void println( boolean b ) ;
void println( char c ) ;
void println( char[] s ) ;
void println( double d ) ;
void println( float f ) ;
void println( int i ) ;
void println( long l ) ;
void println( Object obj ) ;
void println( String s ) ;
將指定類型的數據輸出到Http流,并輸出一個換行符。
11. Appendable append( char c ) ;
Appendable append( CharSequence cxq, int start, int end ) ;
Appendable append( CharSequence cxq ) ;
將一個字符或者實現了CharSequence接口的對象添加到輸出流的后面。
成員:
int DEFAULT_BUFFER = 0 - 缺省緩沖區大小
int NO_BUFFER = -1 - writer是否處于緩沖輸出狀態
int UNBOUNDED_BUFFER = -2 - 是否限制緩沖區大小
② request - javax.servlet.http.HttpServletRequest
request對象包含所有請求的信息,如請求的來源、標頭、cookies和請求相關的參數值等。
方法:
1. Object getAttribute( String name ) ;
返回由name指定的屬性值,該屬性不存在時返回null。
2. Enumeration getAttributeNames() ;
返回request對象的所有屬性名稱的集合。
3. String getAuthType() ;
返回用來保護servlet的認證方法的名稱,未受保護時返回null。
4. String getCharacterEncoding() ;
返回請求中的字符編碼方法,可以在response對象中設置。
5. int getContentLength() ;
返回請求的BODY的長度,不能確定長度時返回-1。可以在response中設置。
6. String getContentType() ;
返回在response中定義的內容類型。
7. String getContentPath() ;
返回請求的路徑。
8. Cookie[] getCookies() ;
返回客戶端所有的Cookie的數組。
9. Enumeration getHeaderNames() ;
返回所有HTTP頭的名稱的集合。
10. Enumeration getHeaders( String name ) ;
返回指定HTTP頭的所有值的集合。
11. String getHeader( String name ) ;
返回指定名稱的HTTP頭的信息。
12. long getDateHeader( String name ) ;
返回指定名稱的Data類型的HTTP頭的信息。
13. int getIntHeader( String name ) ;
返回指定名稱的Int類型的HTTP頭的信息。
14. ServletInputStream getInputStream() ;
返回請求的輸入流。
15. Locale getLocale() ;
返回當前頁的Locale對象,可以在response中設定。
16. Enumeration getLocales() ;
返回請求中所有的Locale對象的集合。
17. String getLocalName() ;
獲取響應請求的服務器端主機名。
18. String getLocalAddr() ;
獲取響應請求的服務器端地址。
19. int getLocalPort() ;
獲取響應請求的服務器端端口
20. String getMethod() ;
獲取客戶端向服務器端發送請求的方法(GET、POST)。
21. String getParameter( String name ) ;
獲取客戶端發送給服務器端的參數值。
22. Map getParameterMap() ;
該方法返回包含請求中所有參數的一個Map對象。
23. Enumeration getParameterNames() ;
返回請求中所有參數的集合。
24. String[] getParameterValues( String name ) ;
獲得請求中指定參數的所有值。
25. String getQueryString() ;
返回get方法傳遞的參數字符串,該方法不分解出單獨的參數。
26. String getPathInfo() ;
取出請求中處于ServletPath和QueryString之間的額外信息。
27. String getPathTranslated() ;
返回用getPathInfo()方法取得的路徑信息的實際路徑。
28. String getProtocol() ;
返回請求使用的協議。可以是HTTP1.1或者HTTP1.0。
29. BufferedReader getReader() ;
返回請求的輸入流對應的Reader對象,該方法和getInputStream()方法在一個頁面中只能調用一個。
30. String getRemoteAddr() ;
獲取發出請求的客戶端IP地址。
31. String getRemoteHost() ;
獲取發出請求的客戶端主機名
32. String getRemoteUser() ;
返回經過客戶端驗證的用戶名,未經驗證返回null。
33. int getRemotePort() ;
返回發出請求的客戶端主機端口。
34. String getRealPath( String path ) ;
返回給定虛擬路徑的物理路徑。
35. RequestDispatcher getRequestDispatcher( String path ) ;
按給定的路徑生成資源轉向處理適配器對象。
36. String getRequestedSessionId() ;
返回請求的session的標識。
37. String RequestURI() ;
返回發出請求的客戶端地址,但是不包括請求的參數字符串。
38. StringBuffer getRequestURI() ;
返回響應請求的服務器端地址
39. String getScheme() ;
獲取協議名稱,缺省值為HTTP協議。
40. String getServerName() ;
返回響應請求的服務器名稱。
41. String getServletPath() ;
獲取客戶端所請求的腳本文件的文件路徑。
42. int getServerPort() ;
獲取響應請求的服務器端主機端口號。
43. void removeAttribute( String name ) ;
在屬性列表中刪除指定名稱的屬性。
44. void setAttribute( String name, Object value ) ;
在屬性列表中添加/刪除指定的屬性。
45. void setCharacterEncoding( String name ) ;
設置請求的字符編碼格式。
46. HttpSession getSession() ;
HttpSession getSession( boolean create ) ;
獲取session,如果create為true,在無session的情況下創建一個。
47. boolean isRequestedSessionIdFromCookie() ;
檢查請求的會話ID是否為通過Cookie傳入。
48. boolean isRequestedSessionIdFromURL() ;
檢查請求的會話ID是否為通過URL傳入。
49. boolean isRequestedSessionIdValid() ;
檢查請求的會話ID是否仍然有效。
50. boolean isSecure() ;
檢查請求是否使用安全鏈接,如果HTTPS等。
51. boolean isUserInRole( String role ) ;
檢查已經通過驗證的用戶是否在是role所指定的角色。
52. Principal getUserPrincipal() ;
返回包含用戶登陸名的一個java.security.Principal對象。
成員:
String BASIC_AUTH = "BASIC" -
String CLIENT_CERT_AUTH = "CLIENT_CERT" -
String DIGEST_AUTH = "DIGEST" -
String FORM_AUTH = "FORM" -
③ response - javax.servlet.http.HttpServletResponse
response對象主要將JSP容器處理后的結果傳回到客戶端。
方法:
1. void addCookie( Cookie cookie ) ;
添加一個Cookie對象,保存客戶端信息。
2. void addDateHeader( String name, long value ) ;
添加一個日期類型的HTTP頭信息,覆蓋同名的HTTP頭信息。
3. void addHeader( String name, String value ) ;
添加一個HTTP頭,覆蓋同名的舊HTTP頭。
4. void addIntHeader( String name, int value ) ;
添加一個整型的HTTP頭,覆蓋同名的舊HTTP頭。
5. boolean containsHeader( String name ) ;
判斷指定的HTTP頭是否存在。
6. String encodeRedirectURL( String url ) ;
對sendRedirect()方法使用的URL進行編碼。
7. String encodeURL( String url ) ;
將URL予以編碼,回傳包含session ID的URL。
8. void flushBuffer() ;
強制把當前緩沖區的內容發送到客戶端。
9. int getBufferSize() ;
取得以kb為單位的緩沖區大小。
10. String getCharacterEncoding() ;
獲取響應的字符編碼格式。
11. String getContentType() ;
獲取響應的類型。
12. Locale getLocale() ;
獲取響應的Locale對象。
13. ServletOutputStream getOutputStream() ;
返回客戶端的輸出流對象。
14. PrintWriter getWriter() ;
獲取輸出流對應的writer對象。
15. boolean isCommitted() ;
判斷服務器端是否已經將數據輸出到客戶端。
16. void reset() ;
清空buffer中的所有內容。
17. void resetBuffer() ;
情況buffer中所有的內容,但是保留HTTP頭和狀態信息。
18. void sendError( int xc, String msg ) ;
void sendError( int xc ) ;
發送錯誤,包括狀態碼和錯誤信息。
19. void sendRedirect( String locationg ) ;
把響應發送到另外一個位置進行處理。
20. void setBufferSize( int size ) ;
設置以kb為單位的緩沖區大小。
21. void setCharacterEncoding( String charset ) ;
設置響應使用的字符編碼格式。
22. void setContentLength( int length ) ;
設置響應的BODY長度。
23. void setContentType( String type ) ;
設置響應的類型。
24. void setDateHeader( String name, long value ) ;
設置指定名稱的Data類型的HTTP頭的值。
25. void setHeader( String name, String value ) ;
設置指定名稱的HTTP頭的值。
26. void setIntHeader( String name, int value ) ;
設置指定名稱的int類型的HTTP頭的值。
27. void setStatus( int xc ) ;
設置響應狀態碼,新值會覆蓋當前值。
成員(HTTP狀態碼):
int SC_CONTINUE = 100 int SC_SWITCHING_PROTOCOLS = 101
int SC_OK = 200 int SC_NON_AUTHORITATIVE_INFORMATION = 203
int SC_ACCEPTED = 202 int SC_CREATED = 201
int SC_NO_CONTENT = 204 int SC_RESET_CONTENT = 205
int SC_PARTIAL_CONTENT = 206 int SC_MULTIPLE_CHOICES = 300
int SC_MOVED_PERMANENTLY = 301 int SC_MOVED_TEMPORARILY = 302
int SC_FOUND = 302 int SC_SEE_OTHER = 303
int SC_NOT_MODIFIED = 304 int SC_USE_PROXY = 305
int SC_TEMPORARY_REDIRECT = 307 int SC_BAD_REQUEST = 400
int SC_UNAUTHORIZED = 401 int SC_PAYMENT_REQUIRED = 402
int SC_FORBIDDEN = 403 int SC_NOT_FOUND = 404
int SC_METHOD_NOT_ALLOWED = 405 int SC_NOT_ACCEPTABLE = 406
int SC_PROXY_AUTHENTICATION_REQUIRED = 407 int SC_REQUEST_TIMEOUT = 408
int SC_CONFLICT = 409 int SC_GONE = 410
int SC_LENGTH_REQUIRED = 411 int SC_PRECONDITION_FAILED = 412
int SC_REQUEST_ENTITY_TOO_LARGE = 413 int SC_REQUEST_URI_TOO_LONG = 414
int SC_UNSUPPORTED_MEDIA_TYPE = 415 int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416
int SC_EXPECTATION_FAILED = 417 int SC_INTERNAL_SERVER_ERROR = 500
int SC_NOT_IMPLEMENTED = 501 int SC_BAD_GATEWAY = 502
int SC_SERVICE_UNAVAILABLE = 503 int SC_GATEWAY_TIMEOUT = 504
int SC_HTTP_VERSION_NOT_SUPPORTED = 505
④ session - javax.servlet.http.HttpSession
session對象表示目前個別用戶的會話狀態,用來識別每個用戶。
方法:
1. Object getAttribute( String name ) ;
獲取與指定名字相關聯的session屬性值。
2. Enumeration getAttributeNames() ;
取得session內所有屬性的集合。
3. long getCreationTime() ;
返回session的創建時間,最小單位千分之一秒。
4. String getId() ;
取得session標識。
5. long getLastAccessedTime() ;
返回與當前session相關的客戶端最后一次訪問的時間,由1970-01-01算起,單位毫秒。
6. int getMaxInactiveInterval( int interval ) ;
返回總時間,以秒為單位,表示session的有效時間(session不活動時間)。-1為永不過期。
7. ServletContext getServletContext() ;
返回一個該JSP頁面對應的ServletContext對象實例。
8. HttpSessionContext getSessionContext() ;
9. Object getValue( String name ) ;
取得指定名稱的session變量值,不推薦使用。
10. String[] getValueNames() ;
取得所有session變量的名稱的集合,不推薦使用。
11. void invalidate() ;
銷毀這個session對象。
12. boolean isNew() ;
判斷一個session是否由服務器產生,但是客戶端并沒有使用。
13. void pubValue( String name, Object value ) ;
添加一個session變量,不推薦使用。
14. void removeValue( String name ) ;
移除一個session變量的值,不推薦使用。
15. void setAttribute( String name, String value ) ;
設置指定名稱的session屬性值。
16. void setMaxInactiveInterval( int interval ) ;
設置session的有效期。
17. void removeAttribute( String name ) ;
移除指定名稱的session屬性。
⑤ pageContext - javax.servlet.jsp.PageContext
pageContext對象存儲本JSP頁面相關信息,如屬性、內建對象等。
方法:
1. void setAttribute( String name, Object value, int scope ) ;
void setAttribute( String name, Object value ) ;
在指定的共享范圍內設置屬性。
2. Object getAttribute( String name, int scope ) ;
Object getAttribute( String name ) ;
取得指定共享范圍內以name為名字的屬性值。
3. Object findAttribute( String name ) ;
按頁面、請求、會話和應用程序共享范圍搜索已命名的屬性。
4. void removeAttribute( String name, int scope ) ;
void removeAttribute( String name ) ;
移除指定名稱和共享范圍的屬性。
5. void forward( String url ) ;
將頁面導航到指定的URL。
6. Enumeration getAttributeNamesScope( int scope ) ;
取得指定共享范圍內的所有屬性名稱的集合。
7. int getAttributeScope( String name ) ;
取得指定屬性的共享范圍。
8. ErrorData getErrorDate() ;
取得頁面的errorData對象。
9. Exception getException() ;
取得頁面的exception對象。
10. ExpressionEvaluator getExpressionEvaluator() ;
取得頁面的expressionEvaluator對象。
11. JspWriter getOut() ;
取得頁面的out對象。
12. Object getPage() ;
取得頁面的page對象。
13. ServletRequest getRequest() ;
取得頁面的request對象。
14. ServletResponse getResponse() ;
取得頁面的response對象。
15. ServletConfig getConfig() ;
取得頁面的config對象。
16. ServletContext getServletContext() ;
取得頁面的servletContext對象。
17. HttpSession getSession() ;
取得頁面的session對象。
18. VariableResolver getVariableResolver() ;
取得頁面的variableResolver對象。
19. void include( String url, boolean flush ) ;
void include( String url ) ;
包含其他的資源,并指定是否自動刷新。
20. void release() ;
重置pageContext內部狀態,釋放所有內部引用。
21. void initialize( Servlet servlet, ServletRequest request, ServletResponse response,
String errorPageURL, boolean needSession, int bufferSize, boolean autoFlush ) ;
初始化未經初始化的pageContext對象。
22. BodyContext pushBody() ;
BodyContext pushBody( Writer writer ) ;
保存當前的out對象,并更新pageContext中page范圍內的out對象。
23. JspWrite popBody() ;
取出由pushBody()方法保存的out對象。
24. void handlePageException( Exception e ) ;
void handlePageException( Thrwoable t ) ;
成員:
int PAGE_SCOPE = 1 - 頁面共享范圍
int REQUEST_SCOPE = 2 - 請求共享范圍
int SESSION_SCOPE = 3 - 會話共享范圍
int APPLICATION_SCOPE = 4 - 應用程序共享范圍
String PAGE = "javax.servlet.jsp.jspPage"
String PAGECONTEXT = "javax.servlet.jsp.jspPageContext"
String REQUEST = "javax.servlet.jsp.jspRequest"
String RESPONSE = "javax.servlet.jsp.jspResponse"
String CONFIG = "javax.servlet.jsp.jspConfig"
String SESSION = "javax.servlet.jsp.jspSession"
String OUT = "javax.servlet.jsp.jspOut"
String APPLICATION = "javax.servlet.jsp.jspApplication"
String EXCEPTION = "javax.servlet.jsp.jspException"
⑥ application - javax.servlet.ServletContext
application主要功用在于取得或更改Servlet的設定。
方法:
1. Object getAttribute( String name ) ;
返回由name指定的application屬性。
2. Enumeration getAttributes() ;
返回所有的application屬性。
3. ServletContext getContext( String uripath ) ;
取得當前應用的ServletContext對象。
4. String getInitParameter( String name ) ;
返回由name指定的application屬性的初始值。
5. Enumeration getInitParameters() ;
返回所有的application屬性的初始值的集合。
6. int getMajorVersion() ;
返回servlet容器支持的Servlet API的版本號。
7. String getMimeType( String file ) ;
返回指定文件的類型,未知類型返回null。一般為"text/html"和"image/gif"。
8. int getMinorVersion() ;
返回servlet容器支持的Servlet API的副版本號。
9. String getRealPath( String path ) ;
返回給定虛擬路徑所對應物理路徑。
10. RequestDispatcher getNamedDispatcher( String name ) ;
為指定名字的Servlet對象返回一個RequestDispatcher對象的實例。
11. RequestDispatcher getRequestDispatcher( String path ) ;
返回一個RequestDispatcher對象的實例。
12. URL getResource( String path ) ;
返回指定的資源路徑對應的一個URL對象實例,參數要以"/"開頭。
13. InputStream getResourceAsStream( String path ) ;
返回一個由path指定位置的資源的InputStream對象實例。
14. Set getResourcePaths( String path ) ;
返回存儲在web-app中所有資源路徑的集合。
15. String getServerInfo() ;
取得應用服務器版本信息。
16. Servlet getServlet( String name ) ;
在ServletContext中檢索指定名稱的servlet。
17. Enumeration getServlets() ;
返回ServletContext中所有servlet的集合。
18. String getServletContextName() ;
返回本web應用的名稱。
19. Enumeration getServletContextNames() ;
返回ServletContext中所有servlet的名稱集合。
20. void log( Exception ex, String msg ) ;
void log( String msg, Throwable t ) ;
void log( String msg ) ;
把指定的信息寫入servlet log文件。
21. void removeAttribute( String name ) ;
移除指定名稱的application屬性。
22. void setAttribute( String name, Object value ) ;
設定指定的application屬性的值。
⑦ config - javax.servlet.ServletConfig
config對象用來存放Servlet初始的數據結構。
方法:
1. String getInitParameter( String name ) ;
返回名稱為name的促使參數的值。
2. Enumeration getInitParameters() ;
返回這個JSP所有的促使參數的名稱集合。
3. ServletContext getContext() ;
返回執行者的servlet上下文。
4. String getServletName() ;
返回servlet的名稱。
⑧ exception - java.lang.Throwable
錯誤對象,只有在JSP頁面的page指令中指定isErrorPage="true"后,才可以在本頁面使用exception對象。
方法:
1. Throwable fillInStackTrace() ;
將當前stack信息記錄到exception對象中。
2. String getLocalizedMessage() ;
取得本地語系的錯誤提示信息。
3. String getMessage()
取得錯誤提示信息。
4. StackTrackElement[] getStackTrace() ;
返回對象中記錄的call stack track信息。
5. Throwable initCause( Throwable cause ) ;
將另外一個異常對象嵌套進當前異常對象中。
6. Throwable getCause() ;
取出嵌套在當前異常對象中的異常。
7. void printStackTrace() ;
void printStackTrace( printStream s ) ;
void printStackTrace( printWriter s ) ;
打印出Throwable及其call stack trace信息。
8. void setStackTrace( StackTraceElement[] stackTrace )
設置對象的call stack trace信息。
⑨ page - javax.servlet.jsp.HttpJspPage
page對象代表JSP對象本身,或者說代表編譯后的servlet對象,
可以用( (javax.servlet.jsp.HttpJspPage)page )來取用它的方法和屬性。
不久以后,開發人員意識到將表達與商務邏輯混合在一起的復雜 JSP 頁不易于理解和維護。不能編寫 scriplet 的頁面制作人員所面臨的另一個問題是由于標準標記集而帶來的 JSP 限制。這些限制使得難點變成利用 JSP 實施自定義標記的機制來創建 JSP 自定義標記。
JSP 標準標記庫 (JSTL) 是自定義標記庫的集合,它將許多 JSP 應用程序通用的核心功能封裝為簡單的標記。它不再需要使用 JSP scriptlet
和表達式,而使用表達式的更高級語法。它還實現了通用目的的功能,如迭代和條件化、數據管理格式化、XML 操作、數據庫訪問、國際化和對本地化信息敏感的格式化標記以及 SQL 標記。JSTL 1.0 推出了 EL 的概念,但只限于 JSTL 標記。在 JSP 2.0 中,您可以使用帶模板文本的 EL,甚至可以通過 javax.servlet.jsp.el 獲得編程方式的訪問。el簡化了在JSP中訪問變量的方式,降低靜態HTML與Java代碼的耦合
EL ----JSP 2.0利器之一
在我們了解 JSTL 如何適應環境以及與 JSTL 表達式語言相關的限制以后,我們來看 JSP 2.0 的重要優點之一 — JSP 表達式語言 (EL)。我們將特別涉及到以下內容:
在使用頁面指示時,您可以通過將 isELEnabled 指示的值相應地設為“true”或“false”,指定是否支持 EL
JSP 表達式語言允許頁面制作人員使用簡單語法訪問組件,如:
${expr}
在以上的語法中,expr 代表有效的表達式。必須注意,該表達式可以與靜態文本混合,還可以與其他表達式結合成更大的表達式。
有效表達式可以包含文字、操作符、變量(對象引用)和函數調用。我們將分別了解這些有效表達式中的每一種:
${ “Hello world” } //輸出字符串常量
${ str } //輸出字符串變量str的值
${ 3 + 2 } //輸出3+2的結果
${ user.name } //輸出user對象的name屬性
${ user[“name”] } //同上
${ sessionScope[“user”].name } //同上
JSP 表達式語言定義可在表達式中使用的以下文字:
文字 | 文字的值 |
---|---|
Boolean |
true 和 false |
Integer |
與 Java 類似。可以包含任何正數或負數,例如 24、-45、567 |
Floating Point |
與 Java 類似。可以包含任何正的或負的浮點數,例如 -1.8E-45、4.567 |
String |
任何由單引號或雙引號限定的字符串。對于單引號、雙引號和反斜杠,使用反斜杠字符作為轉義序列。必須注意,如果在字符串兩端使用雙引號,則單引號不需要轉義。 |
Null | null |
讓我們來看一些使用文字作為有效表達式的示例:
${false} <%-- evaluates to false --%>
${3*8)
JSP 表達式語言提供以下操作符,其中大部分是 Java 中常用的操作符:
術語 | 定義 |
---|---|
算術型 |
+、-(二元)、*、/、div、%、mod、-(一元) |
邏輯型 |
and、&&、or、||、!、not |
關系型 |
==、eq、!=、ne、 |
空 |
空操作符是前綴操作,可用于確定值是否為空。 |
條件型 | A ?B :C。根據 A 賦值的結果來賦值 B 或 C。 |
讓我們來看一些使用操作符作為有效表達式的示例:
${ (6 * 5) + 5 } <%-- evaluates to 35 --%>
${empty name}
JSP 表達式語言定義了一組隱式對象,其中許多對象在 JSP scriplet 和表達式中可用:
術語 | 定義 |
---|---|
pageContext |
JSP 頁的上下文。它可以用于訪問 JSP 隱式對象,如請求、響應、會話、輸出、servletContext 等。例如,${pageContext.response} 為頁面的響應對象賦值。 |
此外,還提供幾個隱式對象,允許對以下對象進行簡易訪問:
術語 | 定義 |
---|---|
param |
將請求參數名稱映射到單個字符串參數值(通過調用 ServletRequest.getParameter (String name) 獲得)。getParameter (String) 方法返回帶有特定名稱的參數。表達式 $(param.name) 相當于 request.getParameter (name)。 |
paramValues |
將請求參數名稱映射到一個數值數組(通過調用 ServletRequest.getParameter (String name) 獲得)。它與 param 隱式對象非常類似,但它檢索一個字符串數組而不是單個值。表達式 ${paramvalues.name) 相當于 request.getParamterValues(name)。 |
header |
將請求頭名稱映射到單個字符串頭值(通過調用 ServletRequest.getHeader(String name) 獲得)。表達式 ${header.name} 相當于 request.getHeader(name)。 |
headerValues |
將請求頭名稱映射到一個數值數組(通過調用 ServletRequest.getHeaders(String) 獲得)。它與頭隱式對象非常類似。表達式 ${headerValues.name} 相當于 request.getHeaderValues(name)。 |
cookie | 將 cookie 名稱映射到單個 cookie 對象。向服務器發出的客戶端請求可以獲得一個或多個 cookie。表達式 ${cookie.name.value} 返回帶有特定名稱的第一個 cookie 值。如果請求包含多個同名的 cookie,則應該使用 ${headerValues.name} 表達式。 |
initParam | 將上下文初始化參數名稱映射到單個值(通過調用 ServletContext.getInitparameter(String name) 獲得)。 |
除了上述兩種類型的隱式對象之外,還有些對象允許訪問多種范圍的變量,如 Web 上下文、會話、請求、頁面:
術語 | 定義 |
---|---|
pageScope |
將頁面范圍的變量名稱映射到其值。例如,EL 表達式可以使用 ${pageScope.objectName} 訪問一個 JSP 中頁面范圍的對象,還可以使用 ${pageScope.objectName.attributeName} 訪問對象的屬性。 |
requestScope |
將請求范圍的變量名稱映射到其值。該對象允許訪問請求對象的屬性。例如,EL 表達式可以使用 ${requestScope.objectName} 訪問一個 JSP 請求范圍的對象,還可以使用 ${requestScope.objectName.attributeName} 訪問對象的屬性。 |
sessionScope |
將會話范圍的變量名稱映射到其值。該對象允許訪問會話對象的屬性。例如: <% session.put (name", "John Doe"); %> |
applicationScope |
將應用程序范圍的變量名稱映射到其值。該隱式對象允許訪問應用程序范圍的對象。 |
必須注意,當表達式根據名稱引用這些對象之一時,返回的是相應的對象而不是相應的屬性。例如:即使現有的 pageContext 屬性包含某些其他值,${pageContext} 也返回 PageContext 對象。 http://www.oracle.com/technology/global/cn/sample_code/tutorials/jsp20/simpleel.html
摘自 csdn:http://blog.csdn.net/anysky130/archive/2008/04/04/2252055.aspx? anysky130的專欄
Servlet三個要素:
1.必須繼承自HttpServlet
2.必須實現doGet()或者doPost()
3.必須在web.xml中配置Servlet
<servlet>
<servlet-name> </servlet-name>
<servlet-class> </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> </servlet-name>
<url-pattern> </url-pattern>
</servelt-mapping>
HttpServeltRrequest:請求對象
getParameter():獲得表單元素的值
getAttribute():獲得request范圍中的屬性值
setAttribute():設置reqeust范圍中的屬性值
setCharacterEncoding():設置字符編碼
HttpSerletResponse:相應對象
sendRedirect():外部跳轉
getWriter():獲得輸出流對象
setContentType("text/html; charset=utf-8"):設置相應內容格式和編碼
四種會話跟蹤方式:
1.Session
HttpSession session = request.getSession();
session.setAttribute("name", "zhangsan");
session.setAttribute("pwd", "aaa");
String name = (String) session.getAttribute("name");
2.cookie:
//創建Cookie
Cookie cookie = new Cookie("name", "zhangsan");
//設置Cookie的超時時間
cookie.setMaxAge(24 * 60 * 60 *60);
//把Cookie發送到客戶端
response.addCookie(cookie);
//得到客戶端發送的Cookie
Cookie [] cookies = request.getCookies();
for(int i=0; i <cookies.length; i++) {
?? Cookie temp = cookies;
?? String key = temp.getName();
?? String value = temp.getValue();
}
3.隱藏表單域
<input type="hidden" name="name" value="zhangsan" />
request.getParameter("name");
4.Url重寫
問號傳參
LoginServlet?username=zhangsan&pwd=123
String name = request.getParameter("username");
String pwd =request.getPareameter("pwd");
內部跳轉:
LoginServlet
request.getRequestDispatcher("index.jsp").forward(request, resposne);
外部跳轉:
response.sendRedirect("index.jsp");
內部跳轉是一次請求和一次響應
外部跳轉是兩次請求和兩次響應
ServletContext:Servlet上下文對象
它是一個公共區域,可以被所有的客戶端共享
setAttribute():向公共區域里放入數據
getAttribute():從公共區域里取數據
二:
三:三個標準范圍:request, session, ServletContext
?? 共同點:都有setAttribute(), getAttribute()
?? 區別:范圍不同,request? < session? < servletContext
四:四種會話跟蹤方式
五:服務器上的五大對象
?? request, response, servlet, session, servletContext
??
Jsp:Java Server Page
頁面構成:7種元素
1.靜態內容:html
2.指令:page, include, taglib:
<%@ 指令名 屬性1="屬性值1" 屬性2="屬性值2" %>
3.表達式: <%=表達式 %>
4.Scriptlet <% Java代碼 %>
5.聲明: <%! %>:變量和方法
6.動作: <jsp:動作名 屬性="屬性值"> </jsp:動作名>
7.注釋:
客戶端看不到的: <%-- --%>
客戶端可以看到的: <!-- -->
Jsp的執行過程:
1.轉譯:Jsp--->Servlet
2.編譯:Servlet---->.class
3.執行:.class
第一次訪問jsp的時候響應速度較慢,后面請求時響應速度快
腳本:
表達式: <%= %>
Scriptlet: <% %>
聲明: <%! %>
指令:
page:language, import, errorPage, isErrorpage
include:file
taglib:uri:指定標簽庫描述符的路徑 prefix:指定標簽的前綴
隱式對象:
分類:
1.輸入和輸出對象:request(HttpServletRequest),
???????????????? response(HttpServletResponse),
???????????????? out(JspWriter), servlet中的out是PrintWriter
2.作用域通信對象:pageContext, request,
???????????????? session(HttpSession),
???????????????? application(ServletContext)
3.Servlet對象:page(this), config
4.錯誤對象:exception
???
JavaBean:
一個標準的JavaBean有三個條件
1.共有的類
2.具有不帶參數的公共的構造方法
3.具有set()和get()方法
4.私有屬性
Jsp中的標準動作:
1.useBean:創建JavaBean的一個實例
<jsp:useBean id="stu" class="com.westaccp.test.Student" scope="page/session/application/request" />
2.setProperty:給JavaBean的屬性賦值
<jsp:setProperty name="stu" property="stuName" value="zhangsan" />
<jsp:setProperty name="stu" property="stuName" param="txtName" />
value和param不能同時使用
偷懶的方法: <jsp:setProperty name="stu" property="*" />
這個時候需要注意的是,表單元素的名字必須和JavaBean的屬性值
一模一樣
3.getProperty:獲得JvaBean的屬性值
<jsp:getProperty name="stu" property="stuName" />
4.forward:內部跳轉,相當于request.getRequestDispatcher().forward(request, response);
<jsp:forward page="index.jsp" />
5.include:包含
<jsp:include page="header.jsp" flush="true" />
表達式語言:
EL: Expression Language
語法格式: ${表達式 }
表示式 = 運算符 + 操作數
運算符:跟Java比較,多了一個empty, 少了一個賦值運算符
${empty ""} : true
${empty null} :true
操作數:
-->常量:布爾型(true/false), 整型, 浮點型, 字符串(可以用'', 還可以用""), Null
-->變量:
??? 1.指的是放在四個標準范圍里的屬性(page, request, session, application)
??? 2.在編準范圍內的搜索順序:page-->request--->session--->application
??? 3.怎么取得變量值:點運算符., 還以用[]
???? <%
????? request.setAttribute("name", "lisi");
??? %>
??? ${requestScope.name}
??? 或者
??? ${requestScope["name"]}
-->隱式對象
??? 1.pageContext:通過它可以訪問request, session, servletContext
??? 2.跟范圍由關的:pageScope, requestScope, sessionScope, applicationScope
??? 3.跟輸入有關的:param, paramValues
??? 4.其他的:header, cookie, headervalues,
EL表達式適用的場合:
1.可以在靜態文本中使用
2.與自定義標簽結合使用
3.和JavaBean結合使用
<jsp:userBean id="stu" class="com.westaccp.test.Student" scope="session" />
<jsp:setProperty name="stu" property="stuName" value="hello" />
${stu.stuName}
自定義標簽:
1.標簽處理程序實現
--->實現:繼承自BodyTagSupport或者TagSupport
????????? 一般會重寫doStartTag(), doEndTag(), doAfterBody()
--->描述:在標簽庫描述符文件中描述(.tld)
???? <taglib>
??????? <tlib-version>1.0 </tlib-version>
??????? <jsp-version>2.0 </jsp-version>
??????? <short-name>simpletag </short-name>
???
??????? <tag>
???????? <name>showbody </name>
???????? <tag-class>com.westaccp.test.ShowBodyTag </tag-class>
???????? <body-content>empty/jsp </body-content>
???????? <attribute>
????????? <name>color </name>
???????? </attribute>
??????? </tag>
???? </taglib>
--->使用: <%@ taglib uri="WEB-INF/mytag.tld" prefix="my" %>
?????????? <my:showbody />
2.標簽文件
--->實現和描述
???? 在.tag文件中實現
???? 設置主體內容: <%@ body-content="empty/scriptless" %>
???? 設置屬性: <%@ attribute name="name" required="true" rtexprvalue="true" %>
???? 有主體內容: <jsp:doBody scope="session" var="theBody" />
????? <%
??????? String body = (String) session.getAttribute("theBody");
???? %>
--->使用
???? WEB-INF/tags/sayhello.tag
????? <%@ taglib tagdir="/WEB-INF/tags/" prefix="you" %>
????? <you:sayhello />
????
標準標簽庫:
1.核心標簽庫
-->通用:
???? set: <c:set var="" value="" scope="" />
???? out: <c:out value="" />
???? remove:? <c:remove var="" scope="" />
-->條件:
???? if: <c:if test="">..... </c:if>
???? choose:? <c:choose>
???????????? <c:when test="">... </c:when>
???????????? <c:when test="">... </c:when>
???????????? <c:when test="">... </c:when>
??????????????? .....
???????????????? <c:otherwise>... </otherwise>??????????
????????????? </c:choose>
-->迭代:
??? forEach: <forEach var="" items="" varStatus="" begin="" end="">
??? foTokens: <foTodens var="" items="" delim=",; |"> </foTodens>
??? Java,C#;SQL ¦C
2.I18N與格式化標簽庫
-->setLocale:設置本地區域
-->bundle:設置資源包
-->setBundle:設置資源包
-->message:輸出消息
3.SQL標簽庫
-->setDataSource:設置數據源,用于獲得與數據庫的連接
-->query:執行查詢
-->update:執行增,刪,改
-->transaction:事務
-->param:參數
4.XML標簽庫
過濾器:
生命周期:
1.實例華:
2.初始化:init()
3.過濾:doFilter()
4.銷毀:destroy()
5.不可用
配置:
<filter>
<filter-name> </filter-name>
<filter-class> </filter-class>
</filter>
<filter-mapping>
<filter-name> </filter-name>
<url-pattern> </url-pattern>
</filter-mapping>
幾個重要的接口:
1.Filter:init(), doFilter(), destroy()
2.FilterChain: doFilter(request, response)
3.FilterConfig:getFilterName(), getInitParameter(),
過濾器鏈:--->1--->2--->3--->Servlet 請求
???????? <----1 <---2 <---3 <---??????? 響應
???????
MvC設計模式
1.ModelI:jsp+JavaBean
2.ModelII:jsp+Servlet+JavaBean
????????? jsp---view
????????? servlet---control
????????? javabean---model
MVC:
M--Model:模型:訪問后臺數據庫
V--view:視圖:展示
C--control:控制器:控制程序流程
ModelII和MVC的關系:
MVC是一種設計模式,ModelII它是MVC的一種具體的實現?