SlideShare a Scribd company logo
Javax.serlet package
ServletConfig Vs ServletContext  ServletConfig   Used for deploy-time information to be given to the servlet . One ServletConfig per servlet.  Used to access the ServletContext . Parameters configured in the DD . ServletContext   One ServletContext per webapp. Used to access the webapp parameters (configured in DD).  Can store information for the rest of the webapp servlets (attributes).  Used to get server info, container info, API version info...
HttpServletRequest Object  Methods inherited from ServletRequest interface :  getAttribute(String)  getContentLength()  getInputStream()  getLocalPort() << port the server passes the request to (one per thread)  getRemotePort() << client port  getServerPort() << port the server is listening to  getParameter(String)  getParameterValues(String)  getParameterNames()  Methods inherited from HttpServletRequest inteface :  getCookies()  getHeader(String):String  getHeaders(String):String[]  getIntHeader(String):int & getDateHeader(String):Date  getHeaderNames():Enumeration  getQueryString()  getSession()
HttpServletRequest
HttpServletResponse Used to send data back to the client. Most common methods are setContentType() and getWriter() (NOT getPrintWriter())  getWriter() allows to make character I/O to the response stream  Response can be used to set headers, add cookies or send errors  The method used to get an outputStream is getOutputStream()  The ServletOutputStream writing method is : write(byte[])  setContentType() allows the browser to know how to handle the data. (text/html, application/pdf, image/jpeg, ...)  setHeader() << overwrites the existing value or creates it.  addHeader() << adds an additional value or creates it.  To redirect a request, use sendRedirect(url:String). The redirection is made by the client.  sendRedirect() cannot be called if the response has already been committed (flushed).  sendRedirect(String) : the url string may start with a /, to force URL to be relative to the root of this web container, or without, to make the url string relative to the current request path.
HttpServletResponse Methods inherited from ServletResponse interface :  setContentType()  getOutputStream()  getWriter()  setContentLength()  Methods inherited from HttpServletResponse interface :  addCookie()  addHeader()  encodeURL()  encodeRedirectURL()  sendError()  setStatus()  sendRedirect()
Initialization Parameters Servlet Init parameters  Defined in the DD, in the servlet element :  <init-param>  <param-name>...</param-name>  <param-value>...</param-value>  </init-param>  Accessible in the servlet via the ServletConfig :getServletConfig() : inherited from Servlet interface.  getServletConfig().getInitParameter(name:String)  NOTE : init parameters are read ONCE and passed ONCE to the servlet, when it’s initialized, in the ServletConfig parameter of the init() method.  NOTE : init parameters are for one servlet only, so they’re defined in an <init-param> block, nested in the <servlet> block.
Initialization Parameter Context init parameters  Context init parameters are just like servlet init parameters, except that they are available to the whole web app, not just one servlet.  They are defined in the DD, in a <context-param> block :  <context-param>  <param-name>...</param-name>  <param-value>...</param-value>  </context-param>  They are accessible via the ServletContext object, that one can get from the getServletContext() method, inherited from the ServletConfig interface.  getServletConfig().getServletContext().getInitParameter(name:String)  NOTE : context init parameters are for the whole web app, so they are NOT nested in the <servlet> element. The <context-param> block is not nested in anything except the <web-app> root element.  NOTE : servlet and context parameters are accessed via the same method name : getInitParameter(String)  Servlet init parameters are accessed via the ServletConfig  Context init parameters are accessed via the ServletContext (accessed via ServletConfig)
Initialization Parameter NOTE : there is ONE ServletConfig per SERVLET  And there is ONE ServletContext per WEB APP  NOTE : By default, assume that “init parameter” stands for “servlet init parameter”  Servlet and context init parameters must be seen as DEPLOY-TIME constants.  There are getter methods to retrieve them, but there is NO setter

More Related Content

Servlet Part 2

  • 2. ServletConfig Vs ServletContext ServletConfig Used for deploy-time information to be given to the servlet . One ServletConfig per servlet. Used to access the ServletContext . Parameters configured in the DD . ServletContext One ServletContext per webapp. Used to access the webapp parameters (configured in DD). Can store information for the rest of the webapp servlets (attributes). Used to get server info, container info, API version info...
  • 3. HttpServletRequest Object Methods inherited from ServletRequest interface : getAttribute(String) getContentLength() getInputStream() getLocalPort() << port the server passes the request to (one per thread) getRemotePort() << client port getServerPort() << port the server is listening to getParameter(String) getParameterValues(String) getParameterNames() Methods inherited from HttpServletRequest inteface : getCookies() getHeader(String):String getHeaders(String):String[] getIntHeader(String):int & getDateHeader(String):Date getHeaderNames():Enumeration getQueryString() getSession()
  • 5. HttpServletResponse Used to send data back to the client. Most common methods are setContentType() and getWriter() (NOT getPrintWriter()) getWriter() allows to make character I/O to the response stream Response can be used to set headers, add cookies or send errors The method used to get an outputStream is getOutputStream() The ServletOutputStream writing method is : write(byte[]) setContentType() allows the browser to know how to handle the data. (text/html, application/pdf, image/jpeg, ...) setHeader() << overwrites the existing value or creates it. addHeader() << adds an additional value or creates it. To redirect a request, use sendRedirect(url:String). The redirection is made by the client. sendRedirect() cannot be called if the response has already been committed (flushed). sendRedirect(String) : the url string may start with a /, to force URL to be relative to the root of this web container, or without, to make the url string relative to the current request path.
  • 6. HttpServletResponse Methods inherited from ServletResponse interface : setContentType() getOutputStream() getWriter() setContentLength() Methods inherited from HttpServletResponse interface : addCookie() addHeader() encodeURL() encodeRedirectURL() sendError() setStatus() sendRedirect()
  • 7. Initialization Parameters Servlet Init parameters Defined in the DD, in the servlet element : <init-param> <param-name>...</param-name> <param-value>...</param-value> </init-param> Accessible in the servlet via the ServletConfig :getServletConfig() : inherited from Servlet interface. getServletConfig().getInitParameter(name:String) NOTE : init parameters are read ONCE and passed ONCE to the servlet, when it’s initialized, in the ServletConfig parameter of the init() method. NOTE : init parameters are for one servlet only, so they’re defined in an <init-param> block, nested in the <servlet> block.
  • 8. Initialization Parameter Context init parameters Context init parameters are just like servlet init parameters, except that they are available to the whole web app, not just one servlet. They are defined in the DD, in a <context-param> block : <context-param> <param-name>...</param-name> <param-value>...</param-value> </context-param> They are accessible via the ServletContext object, that one can get from the getServletContext() method, inherited from the ServletConfig interface. getServletConfig().getServletContext().getInitParameter(name:String) NOTE : context init parameters are for the whole web app, so they are NOT nested in the <servlet> element. The <context-param> block is not nested in anything except the <web-app> root element. NOTE : servlet and context parameters are accessed via the same method name : getInitParameter(String) Servlet init parameters are accessed via the ServletConfig Context init parameters are accessed via the ServletContext (accessed via ServletConfig)
  • 9. Initialization Parameter NOTE : there is ONE ServletConfig per SERVLET And there is ONE ServletContext per WEB APP NOTE : By default, assume that “init parameter” stands for “servlet init parameter” Servlet and context init parameters must be seen as DEPLOY-TIME constants. There are getter methods to retrieve them, but there is NO setter