Home | E-mail 

JSP Exam Notes for the Sun Certified Web Component Developer Exam

1. The Servlet Model

1.1. Identify corresponding method in HttpServlet class for each of the following HTTP methods:

1.1.1. GET: public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
1.1.2. POST: public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
1.1.3. PUT: public void doPut(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException

1.2. GET, POST and HEAD

1.2.1. Identify triggers that might cause a browser to use:
1.2.1.1. GET: (a) typing url directly into a browser, (b) clicking on a hyperlink, (c) submitting html form with 'method=get' or no method attribute
1.2.1.2. POST: (a) submitting html form with 'method=post'
1.2.1.3. HEAD: (a) may be used by a browser to check modification time for purposes of caching
1.2.2. Identify benefits or functionality of:
1.2.2.1. GET:
1.2.2.1.1. designed for getting information (e.g. document, chart, results of query)
1.2.2.1.2. can include a query string (some servers limit this to about 240 characters) for sending information to server
1.2.2.1.3. requested page can be bookmarked
1.2.2.2. POST:
1.2.2.2.1. designed for posting information (e.g. credit card #, info to be stored in a db)
1.2.2.2.2. passes all its data (of unlimited length) to server directly over the socket connection as part of its http request body
1.2.2.2.3. posts cannot be bookmarked or, in some cases, even reloaded
1.2.2.2.4. hides sensitive information from server log by including it in the message body instead of the url query string
1.2.2.3. HEAD:
1.2.2.3.1. sent by a client when it wants to see only the headers of the response, to determine the document's size, modification time, or general availability.
1.2.2.3.2. The service() method treats HEAD requests specially. It calls doGet with a modified response object, which suppresses any output but retains headers.

1.3. For each of the following operations, identify the interface and method name that should be used:

1.3.1. Retrieve HTML form parameters from the request:
1.3.1.1. Enumeration ServletRequest.getParameterNames() - returns empty enum if no parameters
1.3.1.2. String ServletRequest.getParameter(String name) - returns null if does not exist
1.3.1.3. String[] ServletRequest.getParameterValues(String name) - returns null if does not exist
1.3.2. Retrieve a servlet initialization parameter:
1.3.2.1. Enumeration ServletConfig.getInitParameterNames() - returns empty enum if no init parameters
1.3.2.2. String ServletConfig.getInitParameter(String name) - returns null if does not exist
1.3.3. Retrieve HTTP request header information:
1.3.3.1. Enumeration HttpServletRequest.getHeaderNames() - returns empty enum if no headers
1.3.3.2. String HttpServletRequest.getHeader(String name) - returns null if does not exist
1.3.3.3. Enumeration HttpServletRequest.getHeaders(String name) - returns empty enum if no headers
1.3.3.4. long getDateHeader(String name) - returns -1 if does not exist
1.3.3.5. int getIntHeader(String name) - returns -1 if does not exist
1.3.4. Set an HTTP response header; set the content type of the response
1.3.4.1. void HttpServletResponse.setHeader(String name, String value) - if header already exists, overwrites its value
1.3.4.2. void HttpServletResponse.setIntHeader(String name, int value)
1.3.4.3. void HttpServletResponse.setDateHeader(String name, long date)
1.3.4.4. void HttpServletResponse.addHeader(String name, String value) - if header already exists, adds an additional value
1.3.4.5. void HttpServletResponse.addIntHeader(String name, int value)
1.3.4.6. void HttpServletResponse.addDateHeader(String name, long date)
1.3.4.7. void HttpServletResponse.setContentType(String type) - if calling getWriter(), then setContentType should be called first

1.3.5. Acquire a text stream for the response
1.3.5.1. PrintWriter ServletResponse.getWriter() throws IOException - character encoding may be set by calling setContentType, which must be called before calling getWriter()
1.3.6. Acquire a binary stream for the response
1.3.6.1. ServletOutputStream ServletResponse.getOutputStream() throws IOException
1.3.7. Redirect an HTTP request to another URL
1.3.7.1. void HttpServletResponse.sendRedirect(String location) throws IllegalStateException IOException
1.3.7.2. sets status to SC_MOVED_TEMPORARILY, sets the Location header, and performs an implicit reset on the response buffer before generating the redirect page; headers set before sendRedirect() remain set
1.3.7.3. must be called before response body is committed, else throws IllegalStateException
1.3.7.4. the path may be relative or absolute
1.3.7.5. to support clients without redirect capability, method writes a short response body that contains a hyperlink to the new location; so do not write your own msg body

1.4. Identify the interface and method to access values and resources and to set object attributes within the following three Web scopes:

1.4.1. Request (Interfaces: ServletRequest and HttpServletRequest)
1.4.1.1. Enumeration ServletRequest.getAttributeNames() - returns empty enumeration if no attributes
1.4.1.2. Object ServletRequest.getAttribute(String name) - returns null if does not exist
1.4.1.3. void setAttribute(String name, Object obj) - most often used in conjunction with RequestDispatcher; attrib names should follow same convention as pkg names
1.4.1.4. void removeAttribute(String name)
1.4.1.5. String ServletRequest.getCharacterEncoding() - returns encoding used in request body, or null if not specified
1.4.1.6. int ServletRequest.getContentLength() - returns length of request body or -1 if unknown
1.4.1.7. String ServletRequest.getContentType() - returns mime type of request body or null if unknown
1.4.1.8. String ServletRequest.getProtocol() - returns protocol/version, e.g. HTTP/1.1
1.4.1.9. String ServletRequest.getScheme() - scheme used to make this request, e.g. ftp, http, https
1.4.1.10. String ServletRequest.getServerName()
1.4.1.11. int ServletRequest.getServerPort()
1.4.1.12. String HttpServletRequest.getAuthType() - e.g. BASIC, SSL, or null if not protected
1.4.1.13. String HttpServletRequest.getContextPath() - e.g. "/myservlet"
1.4.1.14. String HttpServletRequest.getMethod() - e.g. GET, POST, HEAD, PUT
1.4.1.15. String HttpServletRequest.getPathInfo() - returns extra path info (string following servlet path but preceding query string); null if does not exist
1.4.1.16. String HttpServletRequest.getPathTranslated() - translates extra path info to a real path on the server
1.4.1.17. String HttpServletRequest.getQueryString() - returns query string; null if does not exist
1.4.1.18. String HttpServletRequest.getRemoteUser() - returns null if user not authenticated
1.4.1.19. Principal HttpServletRequest.getUserPrincipal() - returns null if user not authenticated
1.4.1.20. String HttpServletRequest.getRequestURI() - e.g. if request is "POST /some/path.html HTTP/1.1", then returns "/some/path.html"
1.4.1.21. String HttpServletRequest.getServletPath() - returns servlet path and name, but no extra path info
1.4.1.22. HttpSession HttpServletRequest.getSession(boolean create)
1.4.1.23. HttpSession HttpServletRequest.getSession() - calls getSession(true)
1.4.2. Session (Interface: HttpSession)
1.4.2.1. Enumeration HttpSession.getAttributeNames() - returns empty enumeration if no attributes; IllegalStateException if session invalidated
1.4.2.2. Object HttpSession.getAttribute(String name) - returns null if no such object
1.4.2.3. void HttpSession.setAttribute(java.lang.String name, java.lang.Object value)
1.4.2.4. void HttpSession.removeAttribute(java.lang.String name)
1.4.2.5. String HttpSession.getId() - returns unique session identifier assigned by servlet container
1.4.2.6. long HttpSession.getLastAccessedTime() - time when client last sent a request associated with this session
1.4.2.7. int HttpSession.getMaxInactiveInterval() - returns number of seconds this session remains open between client requests; -1 if session should never expire
1.4.2.8. void HttpSession.setMaxInactiveInterval(int interval)
1.4.3. Context (Interface: ServletContext)
1.4.3.1. Enumeration getAttributeNames() - Returns an Enumeration containing the attribute names available within this servlet context.
1.4.3.2. Object getAttribute(String name) - Returns the servlet container attribute with the given name, or null if there is no attribute by that name.
1.4.3.3. void setAttribute(String name, java.lang.Object object) - Binds an object to a given attribute name in this servlet context.
1.4.3.4. void removeAttribute(String name) - Removes the attribute with the given name from the servlet context.
1.4.3.5. ServletContext getContext(String uripath) - Returns a ServletContext object that corresponds to a specified URL on the server.
1.4.3.6. String getInitParameter(String name) - Returns a String containing the value of the named context-wide initialization parameter, or null if does not exist.
1.4.3.7. Enumeration getInitParameterNames() - Returns names of the context's initialization parameters as Enumeration of String objects
1.4.3.8. int getMajorVersion() - Returns the major version of the Java Servlet API that this servlet container supports.
1.4.3.9. int getMinorVersion() - Returns the minor version of the Servlet API that this servlet container supports.
1.4.3.10. String getMimeType(String file) - Returns the MIME type of the specified file, or null if the MIME type is not known.
1.4.3.11. RequestDispatcher getNamedDispatcher(String name) - Returns a RequestDispatcher object that acts as a wrapper for the named servlet.
1.4.3.12. RequestDispatcher getRequestDispatcher(String path) - Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path.
1.4.3.13. String getRealPath(String path) - Returns a String containing the real path for a given virtual path.
1.4.3.14. java.net.URL getResource(String path) - Returns a URL to the resource that is mapped to a specified path.
1.4.3.15. InputStream getResourceAsStream(String path) - Returns the resource located at the named path as an InputStream object.
1.4.3.16. String getServerInfo() - Returns the name and version of the servlet container on which the servlet is running.

1.5. For each of the following life-cycle method, identify its purpose and how and when it is invoked:

1.5.1. public void init() throws ServletException:
1.5.1.1. called after server constructs the servlet instance and before the server handles any requests
1.5.1.2. depending on the server and web app configuration, init() may be called at any of these times: (a) when server starts, (b) when the servlet is first requested, just before the service() method is invoked, (c) at the request of the server administrator
1.5.1.3. if servlet specifies <load-on-startup/> in its web.xml file, then upon server startup, the server will create an instance of the servlet and call its init() method.
1.5.1.4. typically used to perform servlet initialization, e.g. loading objects used by servlet to handle requests, reading in servlet init parameters, starting a background thread.
1.5.1.5. servlet cannot be placed into service if init method throws ServletException or does not return within a server-defined time period
1.5.1.6. init() can only be called once per servlet instance; so would still need to synchronize access to class variables
1.5.2. public void service() throws ServletException, IOException:
1.5.2.1. called by the servlet container to allow the servlet to respond to a request.
1.5.2.2. this method is only called after the servlet's init() method has completed successfully.
1.5.2.3. servlets typically run inside multithreaded servlet containers that can handle multiple requests concurrently. developers must be aware to synchronize access to any shared resources such as files and network connections, as well as the servlet's class and instance variables.
1.5.3. public void destroy():
1.5.3.1. called after the servlet has been taken out of service and all pending requests to the servlet have been completed or timed out
1.5.3.2. gives the servlet an opportunity to clean up any resources that are being held (for example, memory, file handles, threads) and make sure that any persistent state is synchronized with the servlet's current state in memory
1.5.3.3. calling super.destroy() causes GenericServlet.destroy() to write a note to the log that the servlet is being destroyed
1.5.3.4. destroy()called once per servlet instance; destroy() not called if server crashes, so should save state (if needed) periodically after servicing requests
1.5.4. Note: servlet reloading
1.5.4.1. most servers automatically reload a servlet after its class file (under servlet dir, e.g. WEB-INF/classes) changes. when a server dispatches a request to a servlet, it first checks whether the servlet's class file has changed on disk. If it has, then the server creates a new custom class loader, and reloads the entire web application context.
1.5.4.2. class reloading is not based on support class changes or on changes in classes found in the server's classpath, which are loaded by the core, primordial class loader.

1.6. Use a RequestDispatcher to include or forward to a Web resource

1.6.1. include:
1.6.1.1. public void include(ServletRequest request, ServletResponse response) throws ServletException, IOException
1.6.1.2. Includes the content of a resource (servlet, JSP page, HTML file) in the response. In essence, this method enables programmatic server-side includes.
1.6.1.3. The ServletRequest object has its path elements (e.g. attributes request_uri, context_path, and servlet_path) and parameters remain unchanged from the caller's. If the included element requires information on its own path elements, it's available in request attributes (javax.servlet.include.request_uri, context_path etc)
1.6.1.4. The included servlet cannot change the response status code or set headers; any attempt to make a change is ignored.
1.6.1.5. The request and response parameters must be the same objects as were passed to the calling servlet's service method.
1.6.1.6. The included resource must use the same output mechanism (e.g. PrintWriter or ServletOutputStream) as the caller's
1.6.1.7. Information can be passed to target using attached query string or using request attributes set with setAttribute() method.
1.6.1.8. include can be called at any time, even after the flush is called on the response.
1.6.2. forward:
1.6.2.1. public void forward(ServletRequest request, ServletResponse response) throws ServletException, IOException
1.6.2.2. Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server. This method allows one servlet to do preliminary processing of a request and another resource to generate the response. The forwarding servlet generates no output, but may set headers.
1.6.2.3. The ServletRequest object has its path attributes adjusted to match the path of the target resource. Any new request parameters are added to the original.
1.6.2.4. forward() should be called before the response has been committed to the client (before response body output has been flushed). If the response already has been committed, this method throws an IllegalStateException. Uncommitted output in the response buffer is automatically cleared before the forward.
1.6.2.5. The request and response parameters must be the same objects as were passed to the calling servlet's service method.
1.6.2.6. Information can be passed to target using attached query string or using request attributes set with setAttribute() method.
1.6.2.7. forwarding to an html page containing relative url's included (e.g. <IMG> tags) is a bad idea, because forward() does not notify client about the directory from which the page is served, hence the links may be broken. Instead, use sendRedirect().
1.6.3. Note: to get a request dispatcher object:
1.6.3.1. public javax.servlet.RequestDispatcher ServletRequest.getRequestDispatcher(String path) - path may be relative, and cannot extend outside current servlet context
1.6.3.2. public javax.servlet.RequestDispatcher ServletRequest.getNamedDispatcher(String name) - name is the registered name in web.xml file
1.6.3.3. ServletContext also supports these methods, but its getRequestDispatcher method accepts only absolute paths, and not relative paths. Use the request's methods, ServletContext method may be depracted in future.

 

Java SCJP 6 Exam Download
Java SCJP 6 Exam Details

Previous                                                   Contents                                                         Next 

 

Privacy Policy Legal Contact webmaster Web design by anandsoft