Servlet

Structure for Deployment Descriptor (web.xml)

  • Posted on: 4 August 2013
  • By: Zhijun Chen

Deployment Descriptor

* - Optional(0 or more)        ? – Optional(0 or 1)              + - 1 or more         1 – Required(exactly 1)

Note: All elements under web app are marked as *.

<web-app>

<description>*</description>

<display-name>*</display-name>

<icon>

       <small-icon>*</small-icon>

       <large-icon>*</large-icon>

</icon>

<distributable>*</distributable>

<context-param>

       <description>*</description>

       <param-name>1</param-name>

       <param-value>1</param-value>

</context-param>

<filter>

       <description>*</description>

       <display-name>*</display-name>

       <icon>*</icon>

       <filter-name>1</filter-name>

       <filter-class>1</filter-class>

       <init-param>*

              <description>*</description>

              <param-name>1</param-name>

              <param-value>1</param-value>

       </init-param>

</filter>

<filter-mapping>

       <filter-name>1</filter-name>

       <url-pattern>1, xor to servlet-name</url-pattern>

       <servlet-name>1, xor to url-pattern</servlet-name>

       <dispatcher>0 to 4, FORWARD, INCLUDE, REQUEST, ERROR</dispatcher>

</filter-mapping>

<listener>

       <description>*</description>

<display-name>*</display-name>

<icon>*</icon>

<listener-class>1</listener-class>

</listener>

<servlet>

       <description>*</description>

       <display-name>*</display-name>

       <icon>*</icon>

       <servlet-name>1</servlet-name>

       <servlet-class>1, xor to jsp-file</servlet-class>

       <jsp-file>1, xor to servlet-class</jsp-file>

       <init-param>*

              <description>*</description>

              <param-name>1</param-name>

              <param-value>1</param-value>

       </init-param>

       <load-on-startup>?</load-on-startup>

       <run-as>?</run-as>

       <security-role-ref>*

              <description>*</description>

              <role-name>1</role-name>

              <role-link>?</role-link>

</security-role-ref>

</servlet>

<servlet-mapping>

       <servlet-name>1</servlet-name>

       <url-pattern>1</url-pattern>

</servlet-mapping>

<session-config>

       <session-timeout>?, in minutes</session-timeout>

</session-config>

<mime-mapping>

       <extension>1</extension>

       <mime-type>1</mime-type>

</mime-mapping>

<welcome-file-list>

       <welcome-file>+</welcome-file>

</welcome-file-list>

<error-page>

       <error-code>1, xor to exception-type</error-code>

       <exception-type>1, xor to error-code</exception-type>

       <location>1</location>

</error-page>

<jsp-config>

       <description>*</description>

       <display-name>*</display-name>

       <icon>*</icon>

       <taglib>*

       <taglib-uri>1</taglib-uri>

       <taglib-location>1</taglib-location>

</taglib>

<jsp-property-group>*

       <description>*</description>

       <display-name>*</display-name>

       <icon>*</icon>

       <url-pattern>+</url-pattern>

       <el-ignored>?</el-ignored>

       <page-encoding>?</page-coding>

       <scripting-invalid>?</scripting-invalid>

       <is-xml>?</is-xml>

       <include-prelude>*</include-prelude>

       <include-coda>*</include-coda>

</jsp-property-group>

</jsp-config>

<security-constraint>

       <display-name>*</display-name>

       <web-resource-collection>+

              <web-resource-name>1</web-resource-name>

              <url-pattern>+</url-pattern>

              <http-method>*, GET, POST, PUT, DELETE, etc.</http-method>

       </web-resource-collection>

       <auth-constraint>?

              <role-name>*</role-name>

       </auth-constraint>

<user-data-constraint>

       <transport-guarantee>

NONE, INTEGRAL, CONFIDENTIAL

</transport-gurantee>

</user-data-constraint>

</security-constraint>

<login-config>

       <auth-method>?, BASIC, DIGEST, CLIENT-CERT, FORM</auth-method>

       <realm-name>?, not required for CLIENT-CERT, FORM</realm-name>

       <form-login-config> only for FORM auth-method

              <form-login-page>1</form-login-page>

              <form-error-page>1</form-error-page>

       </form-login-config>

</login-config>

<security-role>

       <role-name>1</role-name>

</security-role>

<env-entry></env-entry>

<ejb-ref>

       <ejb-ref-name></ejb-ref-name>

       <ejb-ref-type></ejb-ref-type>

       <ejb-ref-home></ejb-ref-home>

       <ejb-ref-remote></ejb-ref-remote>

</ejb-ref>

<ejb-local-ref></ejb-local-ref>

<service-ref></service-ref>

<resource-ref>

       <description>*</description>

       <res-ref-name></res-ref-name>

       <res-type></res-type>

       <res-auth></res-auth>

</resource-ref>

<resource-env-ref></resource-env-ref>

</web-app>

Servlet Notes for OCEJWCD 6 Exam

  • Posted on: 4 August 2013
  • By: Zhijun Chen

Here are the main notes for Servlet part of Oracle Certified Expert, Java EE 6 Web Component Developer Exam.

javax.servlet.Servlet (Interface)

  • void init(ServletConfig)
  • void init()
  • void destroy()
  • void service(ServletRequest, ServletResponse)

 javax.servlet.GenericServlet (Class implements Servlet)

  • ServletConfig getServletConfig()
  • ServletContext getServletContext() [== getServletConfig().getServletContext()]
  • String getInitParameter(String name) [== getServletConfig().getInitParameter(String name)]
  • Enumeration getInitParameterNames() [== getServletConfig().getInitParameterNames()]
  • String getServletName() [== getServletConfig().getServletName()]

javax.servlet,http.HttpServlet (Class extends GenericServlet)

  • protected void service(HttpServletRequest, HttpServletResponse)
  • protected void doXxx(HttpServletRequest, HttpServletResponse) throws ServletException, IOException (doPost, doGet, doHead, doPut, doDelete, doTrace, doOptions)

javax.servlet.ServletContext (Interface)

  • Object getAttribute(String name)
  • Enumeration getAttributeNames()
  • void removeAttribute(String name)
  • void setAttribute(String name, Object value)
  • String getServletContextName()
  • String getRealPath(String path)
  • Set getResourcePaths(Sting path)
  • ServletContext getContext(String appUrl)
  • java.net.URL getResource(String path)
  • java.io.Inputstream getResourceAsStream(String path)
  • String getMimeType(String path)
  • RequestDispatcher getRequestDispatcher(String path)
  • RequestDispatcher getNamedDispatcher(String name)
  • String getInitParameter(String name)
  • Enumeration getInitParameterNames()
  • ServletRegistration.Dynamic addServlet(String name, String servletClass)
  • FilterRegistration.Dynamic addFilter(String name, String filterClass)
  • ServletRegistration.Dynamic addServlet(String name, Class<? extends Servlet> servletClass)
  • FilterRegistration.Dynamic addFilter(String name, Class<? extends Filter> filterClass)
  • <T extends Servlet> T createServlet(Class<T> servletClass)
  • <T extends Filter> T createFilter(Class<T> filterClass)
  • ServletRegistration.Dynamic addServlet(String name, Servlet s)
  • FilterRegistration.Dynamic addFilter(String name, Filter filter)
  • void addListener(Class<? extends java.util.EventListener> listenerImplementationClass)
  • void addListener(String listenerImplementationClassName)
  • FilterRegistration getFilterRegistration(String filterName)
  • Map<String, ? extends FilterRegistration> getFilterRegistrations()
  • ServletRegistration getServletRegistration(String servletName)
  • Map<String, ? extends ServletRegistration> getServletRegistrations()
  • boolean setInitParameters(String name, String value)
  • void declareRoles(String… roleNames)
  • JspConfigDescriptor getJspConfigDescriptor()
  • SessionCookieConfig getSessionCookieConfig()
  • Set<SessionTrackingMode> getDefaultSessionTrackingModes()
  • Set<SessionTrackingMode> getEffectiveSessionTrackingModes()
  • void setSessionTrackingModes(Set<SessionTrackingMode> modes)
  • int getEffectiveMajorVersion()
  • int getEffectiveMinorVersion()
  • int getMajorVersion()
  • int getMinorVersion()
  • String getServerInfo()

Note: SessionTrackingMode includes COOKIE, URL, SSL.

javax.servlet.Registration (Inteface)

  • String getClassName()
  • String getName()
  • String getInitParameter(String name)
  • Map<String, String> getInitParameters()
  • boolean setInitParameter(String name, String value)
  • Set<String> setInitParameters(Map<String, String> initParams)

javax.servlet.Registration.Dynamic (Interface extends Registration)

  • void setAsyncSupported(boolean isAsyncSupported)

javax.servlet.ServletRegistration (Interface extends Registration)

  • Set<String> addMapping(String… urlPatterns)
  • Collection<String> getMappings()
  • String getRunAsRole()

javax.servlet.FilterRegistration (Interface extends Registration)

  • void addMappingForServletNames(EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter, String… servletNames)
  • void addMappingForUrlPatterns(EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter, String… urlPatterns)
  • Collection<String> getServletNameMappings()
  • Collection<String> getUrlPatternMappings()

javax.servlet.FilterRegistration.Dynamic (Interface extends FilterRegistration and Registration.Dynamic) [No additional methods]

javax.servlet.ServletRegistration.Dynamic (Interface extends ServletRegistration and Registration.Dynamic)

  • void setLoadOnStartup(int loadOnStartup)
  • void setRunAsRole(String roleName)
  • void setMultipartConfig(MultipartConfigElement multipartConfig)
  • Set<String> setServletSecurity(ServletSecurityElement constraint)

javax.servlet.ServletConfig (Interface)

  • String getServletName()
  • ServletContext getServletContext()
  • String getInitParameter(String name)
  • Enumeration getInitParameterNames()

javax.servlet.ServletContextListener (Interface)

  • void contextInitialized(ServletContextEvent event)
  • void contextDestroyed(ServletContextEvent event)

javax.servlet.ServletContextEvent (Class)

  • ServletContext getServletContext()

javax.servlet.ServletContextAttributeListener (Interface)

  • void attributeAdded(ServletContextAttributeEvent event)
  • void attributeRemoved(ServletContextAttributeEvent event)
  • void attributeReplaced(ServletContextAttributeEvent event)

javax.servlet.ServletContextAttributeEvent (Class extends ServletContextEvent)

  • ServletContext getServletContext()
  • String getName()
  • Object getValue()

javax.servlet.ServletRequest (Interface)

  • Object getAttribute(String name)
  • Enumeration getAttributeNames()
  • void removeAttribute(String name)
  • void setAttribute(String name, Object value)
  • String[] getParameterValues(String name)
  • String getParameter(String name)
  • Enumeration getParameterNames()
  • Map getParameterMap()
  • ServletInputStream getInputStream()
  • Reader getReader()
  • RequestDispatcher getRequestDispatcher(String path)
  • boolean isAsyncSupported()
  • boolean isAsyncStarted()
  • AsyncContext startAsync(ServletRequest, ServletResponse)
  • AsyncContext startAsync()
  • AsyncContext getAsyncContext()

javax.servlet.http.HttpServletRequest (Interface extends ServletRequest)

  • Enumeration getHeaders(String name)
  • String getHeader(String name)
  • int getIntHeader(String name)
  • long getDateHeader(String name)
  • String getAuthType()
  • String getRemoteUser()
  • Principal getUserPrincipal()
  • boolean isUserInRole(String role)
  • Cookie[] getCookies()
  • String getRequestedSessionId()
  • HttpSession getSession()
  • HttpSession getSession(boolean create)
  • boolean isRequestedSessionIdFromCookie()
  • boolean isRequestedSessionIdFromURL()
  • boolean isRequestedSessionIdValid()
  • Collection<Part> getParts()
  • Part getPart(String name)

javax.servlet.http.Part (Interface)[a part or form item that was received within a multipart/form-data POST request]

  • String getName()
  • long getSize()
  • String getContentType()
  • String getHeader(String name)
  • Collection<String> getHeaders(String name)
  • Collection<String> getHeaderNames()
  • InputStream getInputStream()
  • void write(String path)
  • void delete()

javax.servlet.ServletResponse (Interface)

  • ServletOutputStream getOutputStream()
  • PrintWriter getWriter()
  • void flushBuffer()
  • int getBufferSize()
  • boolean isCommitted()
  • void reset()
  • void resetBuffer()
  • void setBufferSize(int size)
  • void setCharacterEncoding(String enc)
  • String getCharacterEncoding()
  • void setContentType(String type)
  • String getContentType()
  • void setLocale(Locale locale)
  • Locale getLocale()
  • void setContentLength(int size)

javax.servlet.http.HttpServletResponse (Interface extends ServletResponse)

  • void addHeader(String name, String value)
  • void setHeader(String name, String value)
  • boolean containsHeader(String name)
  • void addIntHeader(String name, int value)
  • void setIntHeader(String name, int value)
  • void addDateHeader(String name, int value)
  • void setDateHeader(String name, int value)
  • void setStatus(int status)
  • void sendError(int code)
  • void sendError(int code, String message)
  • void sendRedirect(String path)
  • String encodeURL(String url)
  • String encodeRedirectURL(String url)
  • void addCookie(Cookie cookie)

javax.servlet.ServletRequestListener (Interface)

  • void requestInitialized(ServletRequestEvent event)
  • void requestDestroyed(ServletRequestEvent event)

javax.servlet.ServletRequestEvent (Class)

  • ServletContext getServletContext()
  • ServletRequest getServletRequest()

javax.servlet.ServletRequestAttributeListener (Interface)

  • void attributeAdded(ServletRequestAttributeEvent event)
  • void attributeRemoved(ServletRequestAttributeEvent event)
  • void attributeReplaced(ServletRequestAttributeEvent event)

javax.servlet.ServletRequestAttributeEvent (Class extends ServletRequestEvent)

  • String getName()
  • Object getValue()

javax.servlet.RequestDispatcher (Interface)

  • void forward(ServletRequest req, ServletResponse resp)
  • void include(ServletRequest req, ServletResponse resp)

javax.servlet.ServletRequestWrapper (Class implements ServletRequest)

  • public ServletRequestWrapper(ServletRequest encapsulatedReq)
  • ServletRequest getRequest()
  • void setRequest(ServletRequest encapsulatedReq)

javax.servlet.http.HttpServletRequestWrapper (Class extends ServletRequestWrapper implements HttpServletRequest)

public HttpServletRequestWrapper(HttpServletRequest wrappedReq)

 

javax.servlet.ServletResponseWrapper (Class implements ServletResponse)

  • public ServletResponseWrapper(ServletResponse encapsulatedResp)
  • ServletResponse getResponse()
  • void setResponse(ServletResponse encapsulatedResp)

javax.servlet.http.HttpServletResponseWrapper (Class extends ServletResponseWrapper implements HttpServletResponse)

  • public HttpServletResponseWrapper(HttpServletResponse wrappedResp)

javax.servlet.Filter (Interface)

  • void init(FilterConfig)
  • void destroy()
  • void doFilter(ServletRequest, ServletResponse, FilterChain)

javax.servlet.FilterChain (Interface)

  • void doFilter(ServletRequest, ServletResponse)

javax.servlet.FilterConfig (Interface)

  • ServletContext getServletContext()
  • String getInitParameter(String name)
  • Enumeration getInitParameterNames()
  • String getFilterName()

javax.servlet.AsyncContext (Interface)

  • ServletRequest getRequest()
  • ServletResponse getResponse()
  • boolean hasOriginalRequestAndResponse()
  • void setTimeout(long milliseconds) [<=0 to disable]
  • long getTimeout()
  • void start(Runnable task)
  • void dispatch(String path)
  • void dispatch(ServletContext context, String path)
  • void dispatch()
  • void complete()
  • <T extends AyncListener> T createListener(Class<T> clazz)
  • void addListener(AsyncListener l)
  • void addListener(AsyncLisnter l, ServletRequest request, ServletResponse response)

javax.servlet.AsyncListener (Interface)

  • void onStartAsync(AsyncEvent event)
  • void onComplete(AsyncEvent event)
  • void onTimout(AsyncEvent event)
  • void onError(AsyncEvent event)

javax.servlet.AsyncEvent (Class)

  • AsyncContext getAsyncContext()
  • Throwable getThrowable()
  • SerlvetRequest getSuppliedRequest()
  • ServletResponse getSuppliedResponse()

javax.servlet.http.HttpSession (Interface)

  • Object getAttribute(String name)
  • Enumeration getAttributeNames()
  • void setAttribute(String name, Object value)
  • void removeAttribute(String name)
  • long getCreationTime()
  • String getId()
  • long getLastAccessedTime()
  • int getMaxInactiveInterval()
  • void invalidate()
  • boolean isNew()
  • void setMaxInactiveInterval(int timeoutSeconds) [0 times out at the end of req-res cycle, <0 never time out]
  • ServletContext getServletContext()

javax.servlet.http.HttpSessionListener (Interface)

  • void sessionCreated(HttpSessionEvent event)
  • void sessionDestroyed(HttpSessionEvent event)

javax.servlet.http.HttpSessionEvent (Class)

  • HttpSession getSession()

javax.servlet.http.HttpSessionActivationListener (Interface)

  • void sessionDidActivate(HttpSessionEvent event)
  • void sessionWillPassivate(HttpSessionEvent event)

javax.servlet.http.HttpSessionAttributeListener (Interface)

  • void attributeAdded(HttpSessionBindingEvent event)
  • void attributeRemoved(HttpSessionBindingEvent event)
  • void attributeReplaced(HttpSessionBindingEvent event)

javax.servlet.http.HttpSessionBindingEvent (Class extends HttpSessionEvent)

  • String getName()
  • Object getValue()

javax.servlet.http.HttpSessionBindingListener (Interface) [Implemented by classes bound into sessions as attributes]

  • void valueBound(HttpSessionBindingEvent event)
  • void valueUnbound(HttpSessionBindingEvent event)

javax.servlet.annotation.@WebServlet(

       boolean asyncSupported = false,

       String description = “”,

       String displayName = “”,

       String largeIcon = “”,

       String smallIcon = “”,

       int loadOnStartup = -1,

       String name = “”,

       String[] urlPatterns = {},

       String[] value = {},

       @WebInitParam[] initParams = {}

)

Note: value is the default element if no others are supplied, the same role as urlPatterns. (value XOR urlPatterns). If name is omitted, full qualified class name will apply

 

javax.servlet.annotation.@WebInitParam(

       String name,

       String value,

       String description = “”

)

 

javax.servlet.annotation.@MultiPartConfig(

       int fileSizeThreshold = 0,

long maxFileSize = -1L,

long maxRequestSize = -1L,

String location = “”

)

 

javax.servlet.annotation.@WebFilter(

       boolean asyncSupported = false,

       String description = “”,

String displayName = “”,

       String largeIcon = “”,

       String smallIcon = “”,

       String filterName = “”,

       String[] urlPatterns = {},

       String[] value = {},

       @WebInitParam[] initParams = {},

       String[] servletNames = {},

       DispatcherType[] dispatcherTypes = {DispatcherType.REQUEST}

)

Note: DispatcherType includes ASYNC, ERROR, FORWARD, INCLUDE, REQUEST

 

javax.servlet.annotation.@WebListener(

       value = “”

)

Note: value is the description of the listener

 

javax.annotation.@Resource(

       String description = “”,

       String name = “”,

       String mappedName = “”,

       Class type = Object.class,

       Resource.AuthenticationType authentication = CONTAINER,

       boolean shareable = true

)

Note: name is the JNDI name of the resource, appended to java:comp/env/; AuthenticationType includes CONTAINER, APPLICATION

 

javax.annotation.@PreDestroy, javax.annotation.@PostConstruct [used to annotate methods which run before an injection is removed or after a resource is injected.]

 

javax.ejb.@EJB(

       String description = “”,

       Class beanInterface = Object.class,

       String name = “”,

       String beanName = “”,

       String mappedName = “”,

       String lookup = “”

)

 

javax.persistence.@PersistenceUnit(

       String name = “”,

       String unitName = “”

)

Note: Expresses a dependency on an EntityManagerFactory and its associated persistence unit.

 

javax.persistence.@PersistenceContext(

       String name = “”,

       String unitName = “”

)

Note: Expresses a dependency on a container-managed EntityManager and its associated persistence context.

 

javax.servlet.annotation.@ServletSecurity(

       HttpConstraint value,

       HttpMethodConstraint[] httpMethodConstraints = {};

)

 

javax.servlet.annotation.@HttpMethodConstraint(

       String value, // required: the HTTP method name

       String[] rolesAllowed = {},

ServletSecurity.TransportGuarantee transportGuarantee = NONE | CONFIDENCIAL,

ServletSecurity.EmptyRoleSemantic emptyRoleSemantic = PERMIT | DENY;

)

 

javax.servlet.annotation.@HttpConstraint(

       String[] rolesAllowed = {},

ServletSecurity.TransportGuarantee transportGuarantee = NONE | CONFIDENCIAL,

ServletSecurity.EmptyRoleSemantic emptyRoleSemantic = PERMIT | DENY;

)