1 / 14

Knowledge Byte In this section, you will learn about:

Knowledge Byte In this section, you will learn about: Fields of the HttpServletResponse interface to represent status codes Calling public methods of other servlets. Field. Status Code. Description. SC_HTTP_VERSION_NOT_SUPPORT-ED. 505. Server does not support the HTTP protocol version.

parsley
Télécharger la présentation

Knowledge Byte In this section, you will learn about:

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Knowledge Byte • In this section, you will learn about: • Fields of the HttpServletResponse interface to represent status codes • Calling public methods of other servlets Collaborate

  2. Field Status Code Description SC_HTTP_VERSION_NOT_SUPPORT-ED 505 Server does not support the HTTP protocol version. SC_FORBIDDEN 403 Server receives request but refuses to process it. SC_NO_CONTENT 204 Server does not have any new information to return for the request. • Fields of HttpServletResponse to Represent Status Codes • The following table lists some fields of the HttpServletResponse interface that represent the status codes: Collaborate

  3. Field Status Code Description SC_PARTIAL_CONTENT 206 Server has successfully processed a partial GET request. SC_NOT_MODIFIED 304 Server contains the resource that is not modified. SC_PAYMENT_REQUIRED 402 Reserved for future use. • Fields of HttpServletResponse to Represent Status Codes (Contd.) • The fields of the HttpServletResponse interface that represent the status codes (Contd.): Collaborate

  4. Calling Public Methods of Other Servlets • A servlet can call the public methods of other servlets, provided both the servlets are running within the same application. • To call the public method of other servlet, you need to: • Know the name of the servlet whose public method you want to call. • Retrieve the context in which the servlet is running, using the getServletContext() method of the ServletContext interface. • Call the getServlet(Stringname) method on the context object by passing the name of the servlet as an argument. Collaborate Lesson 2C / Slide 4 of 14

  5. Calling Public Methods of Other Servlets (Contd.) • You can use the following code snippet to call the public method of another servlet: • public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ • ServletContext sc = getServletContext(); • SimpleServlet simple_ref = (SimpleServlet)sc.getServlet("simple"); • /* "simple" is the alias name of the SimpleServlet servlet • * that you specify in the alias name field while deploying * the SimpleServlet servlet. */ • out.println (simple_ref.getDetails()); • /* getDetails() is an arbitrary method of SimpleServlet servlet */ • } Collaborate Lesson 2C / Slide 5 of 14

  6. From the Expert’s Desk • In this section, you will learn about: • Best practices on performing servlet session management. • Best practices on performing clean shutdown of servlets. • Tips on handling session object effectively. • Tips on deleting cookies explicitly from client browser. • Tips on disabling caching of servlet contents in client browser. • FAQs on servlets. Collaborate

  7. Best Practices • Performing Servlet Session Management • While developing real life applications, you will often need to incorporate session management in your application. • The following techniques enables you to perform session management: • Use Servlet Session API for managing end user sessions. • Use the unique session-id generated by the server for each session created. • Set time after which, if a user remains inactive the session objects get destroyed. Collaborate

  8. Best Practices (Contd.) • Performing Clean Shutdown of Servlets • While processing client requests, the servlet instances can use various shared resources, such as database connection, which needs to be released before the removal of the servlet instance. • You must ensure that all the currently running service threads complete the servicing of client requests before the server removes the servlet instance from memory. • You can define the shuttingDown() method in your servlet to make the server notify all the long running service threads that it is time to shutdown. • You can then define the isShuttingDown() method, which is called by the servlet after a fixed interval, to check if the flag value is set to true. If the value of the flag variable is true, then all the currently running service threads will close the processing of client requests and would release all the shared resources being held. Collaborate

  9. Tips • Handling Session Object Effectively • You can use the sessionCreated() and sessionDestroyed() methods of HttpSessionListener interface to handle session object effectively. • Whenever an end user logs into a Web site, a session object is created. • After the creation of a session object, the HttpSessionEvent event is generated and the sessionCreated() method is called. • When the session is destroyed, the sessionDestroyed() method is called. Collaborate Lesson 2C / Slide 9 of 14

  10. Tips (Contd.) • Deleting Cookies Explicitly from Client Browser • The Cookie class of the servlet API provides the setMaxAge() method to set the life span of a cookie. • The setMaxAge() method takes an integer value to specify the time in seconds for which the browser needs to retain the cookie. • Set the age of the cookie to zero seconds by passing 0 to the setMaxAge() method. Collaborate Lesson 2C / Slide 10 of 14

  11. Tips (Contd.) • Disabling Caching of Servlet Contents in Client Browser • You can programmatically control the caching of servlet contents in a client browser. • The Expires response header allows you to specify the time after which the browser stops using contents from its cache. • You can set the Expires header using the setDateHeader() method in your servlet, with a date previous to the current date, to indicate that the browser should not cache your servlet content. Collaborate Lesson 2C / Slide 11 of 14

  12. FAQs • What are the servlet attributes and methods that are inherently thread-safe? • Local variables and Request attributes are inherently thread-safe. A new instance of the variable is created for each call to method. • Since request attributes are also local variables, therefore they are also thread-safe. • The servlet attributes that need to be guarded for thread safety are instance variable, class variable, session attributes, and context attributes. • Many threads can have access to instance, class, session, and context variables and can manipulate them. • Instance variables can be thread-safe, if you implement the SingleThreadModel interface. Collaborate Lesson 2C / Slide 12 of 14

  13. Challenge • The_________ header allows you to specify the time for which the client browser can serve cache contents of your servlet. • You can use the _____________ method to obtain the idle time of a client from the last time it accessed the application. • You can use the _____________ element in the web.xml file to pass initialization parameters to a servlet. • You can use the__________method to obtain a RequestDispatcher object in your servlet. • A filter class needs to implement the __________ interface. Collaborate Lesson 2C / Slide 13 of 14

  14. Solutions to Challenge • Expires • publiclonggetLastAccessedTime() • <init-param> • getRequestDispatcher() • Filter Collaborate Lesson 2C / Slide 14 of 14

More Related