Appendix B. HTTP Servlet API Quick ReferenceThe javax.servlet.http package allows development of servlets that support the HTTP protocol. While the core functionality in the javax.servlet package provides everything necessary for web development, the specialized classes in this package automate many otherwise tedious tasks. For example, the abstract HttpServlet class includes support for different HTTP request methods and headers. The HttpServletRequest and HttpServletResponse interfaces allow additional direct interaction with the web server, while HttpSession provides built-in session tracking functionality. The Cookie class allows you to quickly set up and process HTTP cookies, and the HttpUtils class does the same for query strings. Figure B-1 shows the class hierarchy of the javax.servlet.http package.Figure B-1. The javax.servlet.http package
SynopsisClass Name: javax.servlet.http.CookieSuperclass: java.lang.Object Immediate Subclasses: None Interfaces Implemented: java.lang.Cloneable Availability: New as of Servlet API 2.0; found in JSDK 2.0, JWS 1.1; an earlier version previously in sun.* hierarchy DescriptionThe Cookie class provides an easy way for servlets to read, create, and manipulate HTTP-style cookies, which allow servlets to store small amounts of data on the client. Cookies are generally used for session tracking or storing small amounts of user-specific configuration information. For more information, consult Chapter 7, "Session Tracking".A servlet uses the getCookies() method of HttpServletRequest to retrieve cookies submitted as part of a client request. The addCookie() method of HttpServletResponse sends a new cookie to the browser. Because cookies are set using HTTP headers, addCookie() must be called before any output is sent to the client. The original Servlet API 1.0 lacked this Cookie class, although the Java Web Server included a Sun-specific sun.servlet.util.Cookie class that worked in roughly the same manner. The only significant difference is that the retrieval and creation methods were static components of the Cookie class itself, rather than being part of the HttpServletRequest and HttpServletResponse interfaces. Class Summariespublic class Cookie implements java.lang.Cloneable { // Constructors public Cookie(String name, String value); // Instance Methods public Object clone(); public String getComment(); public String getDomain(); public int getMaxAge(); public String getName(); public String getPath(); public boolean getSecure(); public String getValue(); public int getVersion(); public void setComment(String purpose); public void setDomain(String pattern); public void setMaxAge(int expiry); public void setPath(String uri); public void setSecure(boolean flag); public void setValue(String newValue); public void setVersion(int v); } ConstructorsCookie()public Cookie(String name, String value) Instance Methodsclone()public Object clone() getComment()public String getComment() getDomain()public String getDomain() getMaxAge()public int getMaxAge() getPath()public String getPath() getSecure()public boolean getSecure() getName()public String getName() getValue()public String getValue() getVersion()public int getVersion() setComment()public void setComment(String purpose) setDomain()public void setDomain(String pattern) setMaxAge()public void setMaxAge(int expiry) setPath()public void setPath(String uri) setSecure()public void setSecure(boolean flag) setValue()public void setValue(String newValue) setVersion()public void setVersion(int v)
SynopsisClass Name: javax.servlet.http.HttpServletSuperclass: javax.servlet.GenericServlet Immediate Subclasses: None Interfaces Implemented: javax.servlet.Servlet, java.io.Serializable Availability: Servlet API 1.0 and later DescriptionHttpServlet is an abstract class that serves as a framework for developing HTTP (World Wide Web) servlets. The public service() method dispatches requests to an HTTP-specific, protected service() method. You may either extend the HTTP-specific service() method (which is then used to handle all types of HTTP requests) or leave the default service method alone and allow it to dispatch requests to particular handler functions for each HTTP submission type: doGet(), doPost(), and so on. Because the default HTTP servlet implementation handles dispatching to these methods, if you override the protected service() method, you must either handle the dispatching manually or not use the handler functions for HTTP request methods.Class Summarypublic abstract class HttpServlet extends javax.servlet.GenericServlet implements javax.servlet.Servlet, java.io.Serializable { // Constructors public HttpServlet(); // Public Instance Methods public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException; // Protected Instance Methods protected void doDelete(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException; // New in 2.0 protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException; protected void doOptions(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException; // New in 2.0 protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException; protected void doPut(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException; // New in 2.0 protected void doTrace(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException; // New in 2.0 protected long getLastModified(HttpServletRequest req); protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException; } ConstructorsHttpServlet()public HttpServlet() Public Instance Methodsservice()public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException Protected Instance MethodsdoDelete()protected void doDelete(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException doGet()protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException doPost()protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException doPut()protected void doPut(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException doOptions()protected void doOptions(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException doTrace()protected void doTrace(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException getLastModified()protected long getLastModified(HttpServletRequest req) service()protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
SynopsisInterface Name: javax.servlet.http.HttpServletRequestSuperinterface: javax.servlet.ServletRequest Immediate Subinterfaces: None Implemented By: None Availability: Servlet API 1.0 and later DescriptionHttpServletRequest extends the basic ServletRequest class, providing additional functionality for HTTP (World Wide Web) servlets. It includes support for cookies and session tracking and access to HTTP header information. HttpServletRequest also parses incoming HTTP form data and stores it as servlet parameters.The server passes an HttpServletRequest object to the service method of an HttpServlet. Certain methods in this interface have suffered from documentation and implementation inconsistencies. Discrepancies have been noted where possible. Interface Declarationpublic interface HttpServletRequest extends javax.servlet.ServletRequest { // Methods public abstract String getAuthType(); public abstract Cookie[] getCookies(); // New in 2.0 public abstract long getDateHeader(String name); public abstract String getHeader(String name); public abstract Enumeration getHeaderNames(); public abstract int getIntHeader(String name); public abstract String getMethod(); public abstract String getPathInfo(); public abstract String getPathTranslated(); public abstract String getQueryString(); public abstract String getRemoteUser(); public abstract String getRequestedSessionId(); // New in 2.0 public abstract String getRequestURI(); public abstract String getServletPath(); public abstract HttpSession getSession(boolean create); // New in 2.0 public abstract boolean isRequestedSessionIdFromCookie(); // New in 2.0 public abstract boolean isRequestedSessionIdFromUrl(); // New in 2.0 public abstract boolean isRequestedSessionIdValid(); // New in 2.0 } MethodsgetAuthType()public abstract String getAuthType() getCookies()public abstract Cookie[] getCookies() getDateHeader()public abstract long getDateHeader(String name) getHeader()public abstract String getHeader(String name) getHeaderNames()public abstract Enumeration getHeaderNames() getIntHeader()public abstract int getIntHeader(String name) getMethod()public abstract String getMethod() getPathInfo()public abstract String getPathInfo() getPathTranslated()public abstract String getPathTranslated() getQueryString()public abstract String getQueryString() getRemoteUser()public abstract String getRemoteUser() getRequestedSessionId()public abstract String getRequestedSessionId() getRequestURI()public abstract String getRequestURI() getServletPath()public abstract String getServletPath() getSession()public abstract HttpSession getSession(boolean create) isRequestedSessionIdFromCookie()public abstract boolean isRequestedSessionIdFromCookie() isRequestedSessionIdFromUrl()public abstract boolean isRequestedSessionIdFromUrl() isRequestedSessionIdValid()public abstract boolean isRequestedSessionIdValid()
SynopsisInterface Name: javax.servlet.http.HttpServletResponseSuperinterface: javax.servlet.ServletResponse Immediate Subinterfaces: None Implemented By: None Availability: Servlet API 1.0 and later DescriptionHttpServletResponse extends the ServletResponse class to allow manipulation of HTTP protocol-specific data, including response headers and status codes. It also defines a series of constants that represent various HTTP status codes and includes helper functions for session tracking operations.Interface Declarationpublic interface HttpServletResponse extends javax.servlet.ServletResponse { // Constants public static final int SC_ACCEPTED; public static final int SC_BAD_GATEWAY; public static final int SC_BAD_REQUEST; public static final int SC_CONFLICT; public static final int SC_CREATED; public static final int SC_CONTINUE; // New in 2.0 public static final int SC_FORBIDDEN; public static final int SC_GATEWAY_TIMEOUT; // New in 2.0 public static final int SC_GONE; // New in 2.0 public static final int SC_HTTP_VERSION_NOT_SUPPORTED; // New in 2.0 public static final int SC_INTERNAL_SERVER_ERROR; public static final int SC_LENGTH_REQUIRED; // New in 2.0 public static final int SC_METHOD_NOT_ALLOWED; // New in 2.0 public static final int SC_MOVED_PERMANENTLY; public static final int SC_MOVED_TEMPORARILY; public static final int SC_MULTIPLE_CHOICES; // New in 2.0 public static final int SC_NO_CONTENT; public static final int SC_NON_AUTHORITATIVE_INFORMATION; // New in 2.0 public static final int SC_NOT_ACCEPTABLE; // New in 2.0 public static final int SC_NOT_FOUND; public static final int SC_NOT_IMPLEMENTED; public static final int SC_NOT_MODIFIED; public static final int SC_OK; public static final int SC_PARTIAL_CONTENT; // New in 2.0 public static final int SC_PAYMENT_REQUIRED; // New in 2.0 public static final int SC_PRECONDITION_FAILED; // New in 2.0 public static final int SC_PROXY_AUTHENTICATION_REQUIRED; // New in 2.0 public static final int SC_REQUEST_ENTITY_TOO_LARGE; // New in 2.0 public static final int SC_REQUEST_TIMEOUT; // New in 2.0 public static final int SC_REQUEST_URI_TOO_LONG; // New in 2.0 public static final int SC_RESET_CONTENT; // New in 2.0 public static final int SC_SEE_OTHER; // New in 2.0 public static final int SC_SERVICE_UNAVAILABLE; public static final int SC_SWITCHING_PROTOCOLS; // New in 2.0 public static final int SC_UNAUTHORIZED; public static final int SC_UNSUPPORTED_MEDIA_TYPE; // New in 2.0 public static final int SC_USE_PROXY; // New in 2.0 // Methods public abstract void addCookie(Cookie cookie); // New in 2.0 public abstract boolean containsHeader(String name); public abstract String encodeRedirectUrl(String url); // New in 2.0 public abstract String encodeUrl(String url); // New in 2.0 public abstract void sendError(int sc) throws IOException; public abstract void sendError(int sc, String msg) throws IOException; public abstract void sendRedirect(String location) throws IOException; public abstract void setDateHeader(String name, long date); public abstract void setHeader(String name, String value); public abstract void setIntHeader(String name, int value); public abstract void setStatus(int sc); public abstract void setStatus(int sc, String sm); } Constants:Appendix C, "HTTP Status Codes", contains complete descriptions of all the SC_XXX status codes.MethodsaddCookie()public abstract void addCookie(Cookie cookie) containsHeader()public abstract boolean containsHeader(String name) encodeRedirectUrl()public abstract String encodeRedirectUrl(String url) encodeUrl()public abstract String encodeUrl(String url) sendError()public abstract void sendError(int sc) throws IOException public abstract void sendError(int sc, String msg) throws IOException sendRedirect()public abstract void sendRedirect(String location) throws IOException setDateHeader()public abstract void setDateHeader(String name, long date) setHeader()public abstract void setHeader(String name, String value) setIntHeader()public abstract void setIntHeader(String name, int value) setStatus()public abstract void setStatus(int sc) public abstract void setStatus(int sc, String sm)
SynopsisInterface Name: javax.servlet.http.HttpSessionSuperinterface: None Immediate Subinterfaces: None Implemented By: None Availability: New as of the Servlet API 2.0; found in JSDK 2.0, JWS 1.1 DescriptionThe HttpSession interface provides a mechanism for identifying return visitors to a web site. For a detailed introduction to session tracking, see Chapter 7, "Session Tracking". The HttpSession interface itself allows servlets to view and manipulate session-specific information, such as creation time and the unique session identifier. It also includes methods to bind objects to the session for later retrieval, allowing "shopping cart" and other applications to hold onto dataA servlet obtains an HttpSession object from the getSession() method of HttpServletRequest. Specific session behavior, such as the amount of idle time before a session is destroyed, depends on the server. While any object can be bound to a session, lots of high-traffic servlets binding large objects to their sessions will impose a heavy resource burden on the server. With most implementations, this can be alleviated by binding only objects that implement the java.io.Serializable interface (this includes all of the data type objects in the core Java API). Some servers have the ability to write Serializable objects to disk to save memory. Unserializable objects, such as java.sql.Connection, must be retained in memory. Interface Declarationpublic interface HttpSession { // Methods public abstract long getCreationTime(); public abstract String getId(); public abstract long getLastAccessedTime(); public abstract HttpSessionContext getSessionContext(); public abstract Object getValue(String name); public abstract String[] getValueNames(); public abstract void invalidate(); public abstract boolean isNew(); public abstract void putValue(String name, Object value); public abstract void removeValue(String name); } MethodsgetCreationTime()public abstract long getCreationTime() getId()public abstract String getId() getLastAccessTime()public abstract long getLastAccessedTime() getSessionContext()public abstract HttpSessionContext getSessionContext() getValue()public abstract Object getValue(String name) getValueNames()public abstract String[] getValueNames() invalidate()public abstract void invalidate() isNew()public abstract boolean isNew() putValue()public abstract void putValue(String name, Object value) removeValue()public abstract void removeValue(String name)
SynopsisClass Name: javax.servlet.http.HttpSessionBindingEventSuperclass: java.util.EventObject Immediate Subclasses: None Interfaces Implemented: None Availability: New as of the Servlet API 2.0; found in JSDK 2.0, JWS 1.1 DescriptionAn HttpSessionBindingEvent is passed to an HttpSessionBindingListener when the listener object is bound to or unbound from a session.Class Summarypublic class HttpSessionBindingEvent extends java.util.EventObject { // Constructors public HttpSessionBindingEvent(HttpSession session, String name); // Instance Methods public String getName(); public HttpSession getSession(); } ConstructorsHttpSessionBindingEvent()public HttpSessionBindingEvent(HttpSession session, String name) Instance MethodsgetName()public String getName() getSession()public HttpSession getSession()
SynopsisInterface Name: javax.servlet.http.HttpSessionBindingListenerSuperinterface: java.util.EventListener Immediate Subinterfaces: None Implemented By: None Availability: New as of the Servlet API 2.0; found in JSDK 2.0, JWS 1.1 DescriptionAn object that implements HttpSessionBindingListener is notified via calls to valueBound() and valueUnbound() when it is bound to or unbound from an HttpSession. Among other things, this interface allows orderly cleanup session-specific resources, such as database connections.Interface Declarationpublic interface HttpSessionBindingListener extends java.util.EventListener { // Methods public abstract void valueBound(HttpSessionBindingEvent event); public abstract void valueUnbound(HttpSessionBindingEvent event); } MethodsvalueBound()public abstract void valueBound(HttpSessionBindingEvent event) valueUnbound()public abstract void valueUnbound(HttpSessionBindingEvent event)
SynopsisInterface Name:javax.servlet.http.HttpSessionContextSuperinterface: None Immediate Subinterfaces: None Implemented By: None Availability: New as of the Servlet API 2.0; found in JSDK 2.0, JWS 1.1 DescriptionHttpSessionContext provides access to all of the currently active sessions on the server. This can be useful for servlets that weed out inactive sessions, display statistics, or otherwise share information. A servlet obtains an HttpSessionContext object from the getSessionContext() method of HttpSession.Interface Declarationpublic interface HttpSessionContext { // Methods public abstract Enumeration getIds(); public abstract HttpSession getSession(String sessionId); } MethodsgetIds()public abstract Enumeration getIds() getSession()public abstract HttpSession getSession(String sessionId)
SynopsisClass Name: javax.servlet.http.HttpUtilsSuperclass: java.lang.Object Immediate Subclasses: None Interfaces Implemented: None Availability: Servlet API 1.0 and later DescriptionA container object for a handful of potentially useful HTTP-oriented methods.Class Summarypublic class HttpUtils { // Constructors public HttpUtils(); // Class Methods public static StringBuffer getRequestURL(HttpServletRequest req); public static Hashtable parsePostData(int len, ServletInputStream in); public static Hashtable parseQueryString(String s); } ConstructorsHttpUtils()public HttpUtils() getRequestURL()public static StringBuffer getRequestURL(HttpServletRequest req) parsePostData()public static Hashtable parsePostData(int len, ServletInputStream in) parseQueryString()public static Hashtable parseQueryString(String s)
Copyright © 2001 O'Reilly & Associates. All rights reserved.
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|