Java之servlet二
阿新 • • 發佈:2018-11-30
相關類
- Servlet
- ServletRequest
- ServletResponse
- HttpServlet
- HttpServletRequest
- HttpServletResponse
- ServletConfig
- ServletContext
- RequestDispatcher
- PrintWriter
- HttpSession
Servlet
Method Summary | |
void |
destroy() |
|
getServletConfig() |
java.lang.String |
getServletInfo() |
void |
init(ServletConfig config) |
void |
service(ServletRequest req, ServletResponse res) |
ServletRequest
Method Summary |
|
java.lang.Object |
getAttribute(java.lang.String name) |
java.util.Enumeration |
getAttributeNames() |
java.lang.String |
getCharacterEncoding() |
int |
getContentLength() |
java.lang.String |
getContentType() |
ServletInputStream |
getInputStream() |
java.lang.String |
getLocalAddr() |
java.util.Locale |
getLocale() |
java.util.Enumeration |
getLocales() |
java.lang.String |
getLocalName() |
int |
getLocalPort() |
java.lang.String |
getParameter(java.lang.String name) 獲取提交的引數值 |
java.util.Map |
getParameterMap() |
java.util.Enumeration |
getParameterNames() |
java.lang.String[] |
getParameterValues(java.lang.String name) |
java.lang.String |
getProtocol() |
java.io.BufferedReader |
getReader() |
java.lang.String |
getRealPath(java.lang.String path) |
java.lang.String |
getRemoteAddr() |
java.lang.String |
getRemoteHost() |
int |
getRemotePort() |
RequestDispatcher |
getRequestDispatcher(java.lang.String path) |
java.lang.String |
getScheme() |
java.lang.String |
getServerName() |
int |
getServerPort() |
boolean |
isSecure() |
void |
removeAttribute(java.lang.String name) |
void |
setAttribute(java.lang.String name, java.lang.Object o) |
void |
setCharacterEncoding(java.lang.String env) |
ServletResponse
Method Summary | |
void |
flushBuffer() |
int |
getBufferSize() |
java.lang.String |
getCharacterEncoding() 獲取字元編碼,如utf-8 |
java.lang.String |
getContentType() 獲取內容型別,如text/html;charset=utf-8 |
java.util.Locale |
getLocale() |
ServletOutputStream |
getOutputStream() |
java.io.PrintWriter |
getWriter() |
boolean |
isCommitted() |
void |
reset() |
void |
resetBuffer() |
void |
setBufferSize(int size) |
void |
setCharacterEncoding(java.lang.String charset) 設定字元編碼 |
void |
setContentLength(int len) |
void |
setContentType(java.lang.String type) 設定內容型別 |
void |
setLocale(java.util.Locale loc) |
GenericServlet
Method Summary | |
void |
destroy() |
java.lang.String |
getInitParameter(java.lang.String name) 獲取初始化引數名對應的值 |
java.util.Enumeration |
getInitParameterNames() 獲取初始化引數名列表,web.xml中servlet節點中init-param節點資訊 |
ServletConfig |
getServletConfig() 同Servlet |
ServletContext |
getServletContext() |
java.lang.String |
getServletInfo() 同Servlet,自己實現,保護作者,版權等等 |
java.lang.String |
getServletName() 獲取servlet名,web.xml中servlet節點中servlet-name註冊的名稱 |
void |
init() |
void |
init(ServletConfig config) |
void |
log(java.lang.String msg) 記錄在logs資料夾中 |
void |
log(java.lang.String message, java.lang.Throwable t) |
abstract void |
service(ServletRequest req, ServletResponse res) |
HttpServlet
Method Summary | |
protected void |
doDelete(HttpServletRequest req, HttpServletResponse resp) |
protected void |
doGet(HttpServletRequest req, HttpServletResponse resp) |
protected void |
doHead(HttpServletRequest req, HttpServletResponse resp) |
protected void |
doOptions(HttpServletRequest req, HttpServletResponse resp) |
protected void |
doPost(HttpServletRequest req, HttpServletResponse resp) |
protected void |
doPut(HttpServletRequest req, HttpServletResponse resp) |
protected void |
doTrace(HttpServletRequest req, HttpServletResponse resp) |
protected long |
getLastModified(HttpServletRequest req) |
protected void |
service(HttpServletRequest req, HttpServletResponse resp) |
void |
service(ServletRequest req, ServletResponse res) |
HttpServletRequest
Method Summary | |
java.lang.String |
getAuthType() |
java.lang.String |
getContextPath() |
Cookie[] |
getCookies() |
long |
getDateHeader(java.lang.String name) |
java.lang.String |
getHeader(java.lang.String name) |
java.util.Enumeration |
getHeaderNames() |
java.util.Enumeration |
getHeaders(java.lang.String name) |
int |
getIntHeader(java.lang.String name) |
java.lang.String |
getMethod() |
java.lang.String |
getPathInfo() |
java.lang.String |
getPathTranslated() |
java.lang.String |
getQueryString() |
java.lang.String |
getRemoteUser() |
java.lang.String |
getRequestedSessionId() |
java.lang.String |
getRequestURI() |
java.lang.StringBuffer |
getRequestURL() |
java.lang.String |
getServletPath() |
HttpSession |
getSession() |
HttpSession |
getSession(boolean create) |
java.security.Principal |
getUserPrincipal() |
boolean |
isRequestedSessionIdFromCookie() |
boolean |
isRequestedSessionIdFromUrl() Deprecated. As of Version 2.1 of the Java Servlet API, use isRequestedSessionIdFromURL() instead. |
boolean |
isRequestedSessionIdFromURL() |
boolean |
isRequestedSessionIdValid() |
boolean |
isUserInRole(java.lang.String role) |
HttpServletResponse
Method Summary | |
void |
addCookie(Cookie cookie) |
void |
addDateHeader(java.lang.String name, long date) |
void |
addHeader(java.lang.String name, java.lang.String value) 設定回覆訊息行鍵值 |
void |
addIntHeader(java.lang.String name, int value) |
boolean |
containsHeader(java.lang.String name) |
java.lang.String |
encodeRedirectUrl(java.lang.String url) Deprecated. As of version 2.1 |
java.lang.String |
encodeRedirectURL(java.lang.String url) |
java.lang.String |
encodeUrl(java.lang.String url) Deprecated. As of version 2.1 |
java.lang.String |
encodeURL(java.lang.String url) |
void |
sendError(int sc) |
void |
sendError(int sc, java.lang.String msg) |
void |
sendRedirect(java.lang.String location) 重定向 |
void |
setDateHeader(java.lang.String name, long date) |
void |
setHeader(java.lang.String name, java.lang.String value) |
void |
setIntHeader(java.lang.String name, int value) |
void |
setStatus(int sc) |
void |
setStatus(int sc, java.lang.String sm) Deprecated As of version 2.1 |
ServletConfig
Method Summary | |
java.lang.String |
getInitParameter(java.lang.String name) |
java.util.Enumeration |
getInitParameterNames() |
ServletContext |
getServletContext() |
java.lang.String |
getServletName() |
ServletContext
Method Summary | |
java.lang.Object |
getAttribute(java.lang.String name) |
java.util.Enumeration |
getAttributeNames() |
ServletContext |
getContext(java.lang.String uripath) |
java.lang.String |
getInitParameter(java.lang.String name) |
java.util.Enumeration |
getInitParameterNames() |
int |
getMajorVersion() |
java.lang.String |
getMimeType(java.lang.String file) |
int |
getMinorVersion() |
RequestDispatcher |
getNamedDispatcher(java.lang.String name) |
java.lang.String |
getRealPath(java.lang.String path) 將資源相對路徑轉換成系統絕對路徑 |
RequestDispatcher |
getRequestDispatcher(java.lang.String path) |
java.net.URL |
getResource(java.lang.String path) |
java.io.InputStream |
getResourceAsStream(java.lang.String path) 將資源轉換成輸入流 |
java.util.Set |
getResourcePaths(java.lang.String path) |
java.lang.String |
getServerInfo() |
Servlet |
getServlet(java.lang.String name) Deprecated. As of Java Servlet API 2.1 |
java.lang.String |
getServletContextName() |
java.util.Enumeration |
getServletNames() Deprecated. As of Java Servlet API 2.1 |
java.util.Enumeration |
getServlets() Deprecated. As of Java Servlet API 2.0 |
void |
log(java.lang.Exception exception, java.lang.String msg) Deprecated. As of Java Servlet API 2.1 |
void |
log(java.lang.String msg) |
void |
log(java.lang.String message, java.lang.Throwable throwable) |
void |
removeAttribute(java.lang.String name) |
void |
setAttribute(java.lang.String name, java.lang.Object object) |
RequestDispatcher
Method Summary | |
void |
forward(ServletRequest request, ServletResponse response) 轉發 |
void |
include(ServletRequest request, ServletResponse response) |
PrintWriter
HttpSession
執行緒同步
synchronized(this){
iNum++;
res.getWriter().println(iNum);
}
重定向和轉發
重定向HttpServletResponse.sendRedirect
轉發RequestDispatcher.forward
同:都是跳轉到另外一個資源,不能有PrintWriter輸出到客戶端
異:重定向可以跳轉到任意的url,有兩次請求(表現為有兩次請求和回覆,且瀏覽器中url為最後一個請求的url)
轉發只能跳轉到同一Web應用下其他資源路徑下,只有一次請求(表現為有一次請求和回覆,且瀏覽器中url開始請求的url)