You are on page 1of 18

SCWCD API

FREDERIC ESNAULT

SERVLETS 3
SERVLET 3
javax.servlet 3
interface : Servlet 3
interface : ServletConfig 3
abstract class :GenericServlet 3
javax.servlet.http 4
class : HttpServlet 4
SERVLETCONTEXT 4
Accessibility 5
REQUEST 5
javax.servlet 5
interface : ServletRequest 5
javax.servlet.http 6
interface : HttpServletRequest 6
class: HttpServlerRequestRequestWrapper 7
RESPONSE 7
javax.servlet 7
interface : ServletResponse 7
javax.servlet.http 7
interface : HttpServletResponse 7
class: HttpServlerRequestResponseWrapper 8
FILTERS 8
javax.servlet 8
interface : Filter 8
interface : FilterChain 9
interface : FilterConfig 9
SESSIONS 9
javax.servlet.http 9
interface : HttpSession 9
DISPATCHER 10
javax.servlet 10
LISTENERS 10
javax.servlet 10
interface : ServletContextListener 10
interface : ServletContextAttributeListener 11
interface : ServletRequestListener 11
interface : ServletContextAttributeListener 11
javax.servlet.http 11
interface : HttpSessionListener 11
interface : HttpSessionActivationListener 12
interface : HttpSessionBindingListener 12
interface : HttpSessionAttributeListener 12
CUSTOM TAGS 13
PACKAGE 13
COMMON API 13
interface : JspTag 13
SIMPLE TAGS 13
interface : SimpleTag 13
1
class : SimpleTagSupport 13
class : JspFragment 14
CLASSIC TAGS 14
interface : Tag 14
interface : IterationTag 14
interface : BodyTag 14
class : TagSupport 15
class : BodyTagSupport 15
abstract class : BodyContent 15
JSP 16
JSP SERVLET 16
javax.servlet.jsp 16
interface : JspPage 16
interface : HttpJspPage 16
IMPLICIT OBJECTS 16
javax.servlet.jsp 17
class : JspWriter 17
abstract class : JspContext 17
class : PageContext 17

2
Servlets

Servlet

The servlet class extended by (almost) all servlets in web applications around is the HttpServlet class,
from the javax.servlet.http package. This class has a complex implementation/inheritance hierarchy
described in following sections.

javax.servlet

interface : Servlet
This is the Servlet hierarchy basis, defining the principal methods of a Servlet.

void init ServletConfig


ServletException
void service ServletRequest, ServletResponse
ServletException, IOException
void destroy -
-
ServletConfig getServletConfig -
-
String getServletInfo -
-

interface : ServletConfig
Servlet configuration interface, defining method to access configuration parameters.

String getInitParameter String


-
Enumeration getInitParameterNames -
-
ServletContext getServletContext -
-
ServletConfig getServletConfig -
-
String getServletName -
-

The init parameters retrieved via this interface are the servlet init parameters.

abstract class :GenericServlet


implements Servlet, ServletConfig

GenericServlet provides implementation of both interfaces methods except for the service()
method, which is not implemented.

3
javax.servlet.http

class : HttpServlet
extends GenericServlet

This class provides implementation for the service() method of the Servlet interface, and adds
HTTP specific methods.

void service HttpServletRequest, HttpServletResponse


ServletException, IOException
void doGet HttpServletRequest, HttpServletResponse
ServletException, IOException
void doPost HttpServletRequest, HttpServletResponse
ServletException, IOException
void doPut HttpServletRequest, HttpServletResponse
ServletException, IOException
void doDelete HttpServletRequest, HttpServletResponse
ServletException, IOException
void doHead HttpServletRequest, HttpServletResponse
ServletException, IOException
void doTrace HttpServletRequest, HttpServletResponse
ServletException, IOException
void doOptions HttpServletRequest, HttpServletResponse
ServletException, IOException
long getLastModified HttpServletRequest
-

ServletContext

Defines a set of methods the servlet uses to communicate with its servlet container.

Object getAttribute String


-
Enumeration getAttributeNames -
-
void setAttribute String, Object
-
void removeAttribute String
-
String getInitParameter String
-
Enumeration getInitParameterNames -
-
RequestDispatcher getRequestDispatcher String
-
RequestDispatcher getNamedDispatcher String
-
URL getResource String
MalformedURLException
InputStream getResourceAsStream String
-
4
The init parameters retrieved via this class are the context init parameters.

Accessibility

The servlet context object may be obtained via three objects :


• ServletConfig object : getServletConfig().getServletContext();
• GenericServlet : getServletContext();
• HttpSession : getSession().getServletContext();

Request

The request object encapsulates all the information defining the client request.

javax.servlet

interface : ServletRequest
Provides client request information to the servlet. The ServletRequest object is constructed by the
container and passed to the servlet’s service method.

Object getAttribute String


-
Enumeration getAttributeNames -
-
void setAttribute String, Object
-
void removeAttribute String
-
String getParameter String
-
Enumeration getParameterNames -
-
Map getParameterMap -
-
String[] getParameterValues String
-
InputStream getInputStream -
IllegalStateException, IOException
BufferedReader getReader -
IllegalStateException, UnsupportedEncodingException, IOException
String getLocalName -
-
int getLocalPort -
-
String getRemoteHost -
-
int getRemotePort -
-
int getServerPort -
-
5
boolean isSecure -
-
RequestDispatcher getRequestDispatcher String
-

For getInputStream() and getReader() methods, an IllegalStateException is thrown


when a method is called when the other has already been called. For example, if
getInputStream() is called and getReader() is called after, then the last call raises an
IllegalStateException.

javax.servlet.http

interface : HttpServletRequest
extends ServletRequest

Provides methods to retrieve HTTP requests specific information.

String BASIC_AUTH BASIC


String DIGEST_AUTH DIGEST
String CLIENT_CERT_AUTH CLIENT_CERT
String FORM_AUTH FORM
String getAuthType -
-
Cookie[] getCookies -
-
String getHeader String
-
Enumeration getHeaderNames -
-
Enumeration getHeaders String
-
int getIntHeader String
-
Date getDateHeader String
-
String getMethod -
-
String getQueryString -
-
String getRemoteUser -
-
Principal getUserPrincipal -
-
HttpSession getSession -
-
HttpSession getSession boolean
-
int getRemotePort -
-
int getServerPort -
-
String getContextPath -
-

6
String getServletPath -
-
String getPathInfo -
-
boolean isUserInRole String
-

class: HttpServlerRequestRequestWrapper
implements HttpServletRequest
extends ServletRequestWrapper

This class provides full implementation of the HttpServletRequest methods. It may be extended to
implement the Decorator/Wrapper pattern, and used in a Filter object.

Response

The response object encapsulates the information of the response sent to the client.

javax.servlet

interface : ServletResponse
Assists the servlet to send a response to the client. Response object is constructed by the
container and passed to the servlet’s service() method.

void flushBuffer -
-
String getContentType -
-
void setContentType String
-
void setContentLength int
-
ServletOutputStream getOutputStream -
IllegalStateException
PrintWriter getWriter -
IllegalStateException
boolean isCommited -
-
void reset -
IllegalStateException

javax.servlet.http

interface : HttpServletResponse
extends ServletResponse

7
Provides methods to handle HTTP specific response informations.

void addCookie Cookie


-
String addHeader String, String
-
void addIntHeader String, int
-
void addDateHeader String, Date
-
void setHeader String, String
-
void setIntHeader String, int
-
void setDateHeader String, date
-
String encodeRedirectURL String
-
String encodeURL String
-
boolean sendError int
IllegalStateException, IOException
void sendError int, String
IllegalStateException, IOException
void sendRedirect String
IllegalStateException, IOExcep tion
void setStatus int
-

The encodeRedirectURL() methods create an encoded URL to be passed as the


sendRedirect() method parameter.

class: HttpServlerRequestResponseWrapper
implements HttpServletResponse
extends ServletResponseWrapper

This class provides full implementation of the HttpServletResponse methods. It may be extended
to implement the Decorator/Wrapper pattern, and used in a Filter object.

Filters

Filters provide a way to intercept/modify requests and/or responses.

javax.servlet

interface : Filter
The filter interface to implement.

8
void init FilterConfig
ServletException
void doFilter ServletRequest,ServletResponse, FilterChain
ServletException, IOException
void destroy -
-

Call from a filter to the next filter/servlet is made using FilterChain object : a Filter does NOT know
about who is before and after it.

interface : FilterChain
The FilterChain interface provides information about Filter ordering and provides a way to process
through the Filters in the chain.

void doFilter ServletRequest, ServletResponse


-

interface : FilterConfig
Provides initialization parameters described in the deployment descriptor and a reference to the
ServletContext object.

void getFilterName -
-
void getInitParameter String
-
void getInitParameterNames -
-
ServletContext getServletContext -
-

Sessions

javax.servlet.http

interface : HttpSession
Provides a way to identify a user across more than one page.

Object getAttribute String


IllegalStateException
Enumeration getAttributeNames -
IllegalStateException
void setAttribute String, Object
IllegalStateException
void removeAttribute String
IllegalStateException
long getCreationTime -
IllegalStateException
long getLastAccessedTime -
IllegalStateException
9
int getMaxInactiveInterval -
-
void setMaxInactiveInterval int
-
void isNew -
IllegalStateException
void invalidate -
IllegalStateException

For getMAxInactiveInterval() method :


ƒ 0 invalidates session immediately;
ƒ negative indicates session will never time out.

Dispatcher

javax.servlet
A RequestDispatcher is used to transmit request processing between web application resources.
The RequestDispatcher object may be obtained in three ways :
- via the ServletContext, with a context root relative path (leading slash mandatory) :
getServletContext().getRequestDispatcher(“/foo/bar.jsp”);

-via the ServletContext, with a servlet name as a parameter, as defined in the DD :


getServletContext().getNamedDispatcher(“ControlerServlet”);

-via the HttpServletRequest, with a request relative path (no leading slash required) :
request.getRequestDispatcher(“confirm.jsp”);

void forward ServletRequest, ServletResponse


ServletException, IOException, IllegalStateException
void include ServletRequest, ServletResponse
ServletException, IOException

The forward() method throws an IllegalStateException if the response has already been
committed.

Listeners

The listener interfaces may be implemented to allow custom object to receive notifications of web
application lifecycle events.

javax.servlet

interface : ServletContextListener
Provides notifications about servlet context lifecycle events.

void contextDestroyed ServletContextEvent


-
void contextInitialized ServletContextEvent
-

10
interface : ServletContextAttributeListener
Provides notifications about servlet context attribute events.

void attributeAdded ServletContextAttributeEvent


-
void attributeRemoved ServletContextAttributeEvent
-
void attributeReplaced ServletContextAttributeEvent
-

The attribute event are notified AFTER the event occured. It means :
- when attributeAdded() is called, the attribute is already available;
- when attributeRemoved() is called, the attribute has already been removed, which
means it’s no longer available;
- when attributeReplaced() is called, the attribute has already been replaced, which
means it’s old value is lost.

interface : ServletRequestListener
Provides notifications about request context lifecycle events.

void requestDestroyed ServletContextEvent


-
void requestInitialized ServletContextEvent
-

interface : ServletContextAttributeListener
Provides notifications about servlet request attribute events.

void attributeAdded ServletContextAttributeEvent


-
void attributeRemoved ServletContextAttributeEvent
-
void attributeReplaced ServletContextAttributeEvent
-

Same note as in ServletContextAttributeListener.

javax.servlet.http

interface : HttpSessionListener
Provides notifications about HttpSession lifecycle events.

void sessionDestroyed HttpSessionEvent


-
void sessionCreated HttpSessionEvent
-

11
interface : HttpSessionActivationListener
Provides notifications about HttpSession activation events.

void sessionWillPassivate HttpSessionEvent


-
void sessionDidActivate HttpSessionEvent
-

The HttpSessionActivationListener must NOT be configured in web application


deployment descriptor.

interface : HttpSessionBindingListener
Provides notifications about binding/unbinding to/from a HttpSession.

void sessionDestroyed HttpSessionBindingEvent


-
void sessionCreated HttpSessionBindingEvent
-

The HttpSessionBindingListener must NOT be configured in web application deployment


descriptor.

interface : HttpSessionAttributeListener
Provides notifications about HttpSession attribute events.

void attributeAdded HttpSessionBindingEvent


-
void attributeRemoved HttpSessionBindingEvent
-
void attributeReplaced HttpSessionBindingEvent
-

12
Custom tags

Package

All custom tags classes and interfaces belong to one and only package :

javax.servlet.jsp.tagext

Common api

interface : JspTag
Introduced as a base hierarchy object for simple and classic tags.

Simple tags

interface : SimpleTag
implements JspTag

Simple tag handlers interface.

void doTag -
JspException, SkipPageException
JspTag getParent -
-
void setJspBody JspFragment
-
void setJspContext JspContext
-
void setParent JspTag
-

class : SimpleTagSupport
implements SimpleTag

A base class for defining Simple Tags.

JspTag findAncestorWithClass JspTag, Class


-
JspFragment getJspBody -
-
JspContext getJspContext -
-

13
class : JspFragment
Encapsulate a fragment of JSP code, to be invoked as many times as needed.

JspContext getJspContext -
-
void invoke Writer
JspException, SkipPageException

Classic tags

interface : Tag
implements JspTag

Classic tag interface.

int EVAL_BODY_INCLUDE -
int EVAL_PAGE -
int SKIP_BODY -
int SKIP_PAGE -
int doEndTag -
JspException
int doStartTag -
JspException
Tag getParent -
-
void release -
-
void setPageContext PageContext
-
void setParent Tag
-

interface : IterationTag
implements JspTag, Tag

int EVAL_BODY_AGAIN -
int doAfterBody -
JspException

interface : BodyTag
implements JspTag, Tag, IterationTag

int EVAL_BODY_BUFFERED -
void doInitBody -
-
void setBodyContent BodyContent
-

14
class : TagSupport
implements JspTag, Tag, IterationTag

Tag findAncestorWithClass Tag, Class


-
Object getValue String
-
Enumeration getValues -
-
void setValue String, Object
-

class : BodyTagSupport
implements JspTag, Tag, IterationTag, BodyTag
extends TagSupport

BodyContent getBodyContent -
-
JspWriter getPreviousOut -
-
void release -
-

abstract class : BodyContent


extends JspWriter

An encapsulation of the evaluation of the body of an action, so it”s available to a tag handler. It
provides methods to read the content, to convert it to Strings, and to clear it.

void clearBody -
-
JspWriter getEnclosingWriter -
-
Reader getReader 1 -
-
String getString 1 -
-
void writeOut 1 Writer
-
1
: method is abstract.

15
JSP

Jsp Servlet

javax.servlet.jsp

interface : JspPage
implements Servlet

Equivalent to servlet’s GenericServlet interface. It defines the two lifecycle methods, and leaves
the service method implementation to the HTTP specific subinterface.

void jspInit -
-
void jspDestroy -
-

Both jspInit() and jspDestroy() may be overriden by a declaration directive in the JSP
page.

interface : HttpJspPage
implements Servlet, JspPage

Implements HTTP specific version of JspPage, adding the service method.

void _jspService HttpServletREquest, HttpServletResponse


ServletException, IOException

Implicit objects

The JSP specific classes used by implicit JSP objects are :

JspWriter out ;
HttpServletRequest request ;
HttpServletResponse response ;
HttpSession session ;
ServletContext application ;
ServletConfig config ;
JspException exception ;
PageContext pageContext ;
Object page ;

The page object is actually a Servlet object.

16
javax.servlet.jsp

class : JspWriter
extends java.io.Writer

Equivalent to the PrintWriter available to a servlet, but in a different package.

abstract class : JspContext


Base class for the PageContext class, abstract to allow a non–“request/response servlet” context.

Object findAttribute String


-
Object getAttribute String
-
Object getAttribute String, int
-
Enumeration getAttributeNamesInScope int
-
int getAttributeScope String
-
JspWriter getOut -
-
void removeAttribute String
-
void removeAttribute String, int
-
void setAttribute String, Object
-
void setAttribute String, Object, int
-

class : PageContext
extends JspContext

Provides JspContext implementation to be used in a servlet environement.

17
int APPLICATION_SCOPE -
int SESSION_SCOPE -
int REQUEST_SCOPE -
int PAGE_SCOPE -
void include String
ServletException, IOException
void include String, boolean
ServletException, IOException
void forward String
ServletException, IOException, IllegalStateException
HttpSession getSession -
-
ServletContext getServletContext -
-
void getServletConfig -
-
ServletRequest getRequest -
-
ServletResponse getResponse -
-
Exception getException -
-
Object getPage -
-

18