为了简明介绍相关类,参考tomcat5.5的文档,相关类不是最新的servlet相关类,基于早期相关类,没有那么复杂,更容易理解
Method Summary | |
void |
destroy() |
ServletConfig |
getServletConfig() |
java.lang.String |
getServletInfo() |
void |
init(ServletConfig config) |
void |
service(ServletRequest req, ServletResponse res) |
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) |
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) |
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) |
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) |
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) |
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 |
Method Summary | |
java.lang.String |
getInitParameter(java.lang.String name) |
java.util.Enumeration |
getInitParameterNames() |
ServletContext |
getServletContext() |
java.lang.String |
getServletName() |
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) |
Method Summary | |
void |
forward(ServletRequest request, ServletResponse response) 转发 |
void |
include(ServletRequest request, ServletResponse response) |
存在于服务器内存中
Method Summary | |
java.lang.Object |
getAttribute(java.lang.String name) |
java.util.Enumeration |
getAttributeNames() |
long |
getCreationTime() |
java.lang.String |
getId() |
long |
getLastAccessedTime() |
int |
getMaxInactiveInterval() |
ServletContext |
getServletContext() |
HttpSessionContext |
getSessionContext() Deprecated. As of Version 2.1, this method is deprecated and has no replacement. . |
java.lang.Object |
getValue(java.lang.String name) Deprecated. As of Version 2.2, this method is replaced by getAttribute(java.lang.String) . |
java.lang.String[] |
getValueNames() Deprecated. As of Version 2.2, this method is replaced by getAttributeNames() |
void |
invalidate() 主动是session失效 |
boolean |
isNew() |
void |
putValue(java.lang.String name, java.lang.Object value) Deprecated. As of Version 2.2, this method is replaced by setAttribute(java.lang.String, java.lang.Object) |
void |
removeAttribute(java.lang.String name) |
void |
removeValue(java.lang.String name) Deprecated. As of Version 2.2, this method is replaced by removeAttribute(java.lang.String) |
void |
setAttribute(java.lang.String name, java.lang.Object value) |
void |
setMaxInactiveInterval(int interval) 设置单个生命周期 |
Method Summary | |
java.lang.Object |
clone() |
java.lang.String |
getComment() |
java.lang.String |
getDomain() |
int |
getMaxAge() 获取生命周期 |
java.lang.String |
getName() |
java.lang.String |
getPath() |
boolean |
getSecure() |
java.lang.String |
getValue() |
int |
getVersion() |
void |
setComment(java.lang.String purpose) |
void |
setDomain(java.lang.String pattern) |
void |
setMaxAge(int expiry) |
void |
setPath(java.lang.String uri) |
void |
setSecure(boolean flag) |
void |
setValue(java.lang.String newValue) |
void |
setVersion(int v) |
◆ 线程同步
synchronized(this){
iNum++;
res.getWriter().println(iNum);
}
◆ 定向和转发
重定向HttpServletResponse.sendRedirect
转发RequestDispatcher.forward
同:都是跳转到另外一个资源,不能有PrintWriter输出到客户端
异:重定向可以跳转到任意的url,有两次请求(表现为有两次请求和回复,且浏览器中url为最后一个请求的url)
转发只能跳转到同一Web应用下其他资源路径下,只有一次请求(表现为有一次请求和回复,且浏览器中url开始请求的url)
Servlet API5.5文档
Servlet API8.5文档