12) Explain Servlet life cycle & importance of context object. (May-13,Jan-13,Jun-12,Nov-11) Servlet Life Cycle: Servlets follow a three-phase life cycle, namely initialization, service, and destruction. Of the three phases, the initialization and destruction phases are performed only once while the service phase is carried out many times. The first phase of the servlet life cycle is initialization. It represents the creation and initialization of the resources the servlet may needed. All servlet must implement the javax.servlet.servlet interface, which defines the init() method that corresponds to the initialization phase of a servlet life cycle. As soon as a servlet is loaded in a container, the init() method is invoked before servicing any request. The second phase of the servlet life cycle is service phase. This phase of the servlet life cycle represents all the interactions carried out along with the requests that are handled by the servlet until it is destroyed. The service phase of the servlet life cycle is corresponds to the service() method of Servlet interface. The service() method is invoked once for every request. Then, its sole responsibility is to generate the response to that request. The service(0 method takes two objects as parameters, javax.servlet.servletrequest and javax.servlet.servletresponse. The destruction phase is the third and final phase of the servlet life cycle. This phase represents the termination of the servlet execution and its removal from the container. Destruction phase is corresponding to the destroy() method of the Servlet interface. To efficiently manage application resources, a servlet should properly use all the three methods of its life cycle. ServletContext: ServletContext object is used to get configuration information from Deployment Descriptor(web.xml) which will be available to any Servlet or JSPs that are part of the web application. Arjun V. Bala Page 20
There is one context per "web application" per Java Virtual Machine. The ServletContext object is contained within the ServletConfig object. That is, the ServletContext can be accessed using the ServletConfig object within a servlet. You can specify param-value pairs for ServletContext object in <context-param> tags in web.xml file. Example: <context-param> <param-name>facultyname</param-name> <param-value>arjunbala</param-value> </context-param> Diffrence between ServletContext & ServletConfig. Servlet Config Servlet Context Servlet config object represent single servlet It represent whole web application running on particular JVM and common for all the servlet Its like local parameter associated with Its like global parameter associated with whole particular servlet application It s a name value pair defined inside the servlet ServletContext has application wide scope so section of web.xml file so it has servlet wide define outside of servlet tag in web.xml file. scope getservletconfig() method is used to get the getservletcontext() method is used to get the config object context object. for example shopping cart of a user is a specific to particular user so here we can use servlet config To get the MIME type of a file or application session related information is stored using servlet context object. 13) Explain event handling in Java Servlet with example.(jan-13,jun-12) An event refers to a set of actions that may occur while an application is running. It could also be defined as a set of actions, such as clicking a button or pressing a key, performed by a user. The following list shows some of the events that may occur during the life cycle of a servlet: Initializing servlets. Adding, removing or replacing attributes in ServletContext. Creating, activating, passivating, or invalidating a Session. Adding, removing, or replacing attributes in a servlet session. Destroying servlets. Types of Servlet Events on the basis of their level of occurrence. Request level events o Refers to the events related to the client s information to a servlet. o There are two event listeners 1. ServletRequestListner 2. ServletRequestAttributeListner Servlet Context level events o Refer to the application level events o There are two event listeners 1. ServletContextListner 2. ServletContextAttributeListner Arjun V. Bala Page 21
Servlet Session level events o Refers to the events that are used to maintain the session of a client. o There are three event listeners 1. HttpSessionListner 2. HttpSessionAttributeListner 3. HttpSessionActivationListner Example: import javax.servlet.servletcontext; import javax.servlet.servletcontextevent; import javax.servlet.servletcontextlistener; ContextListner.java public class ContextListener implements ServletContextListener private ServletContext cont=null; public void contextinitialized(servletcontextevent evt) this.cont = evt.getservletcontext(); System.out.println("Context intialized...!!!!!"); JOptionPane.showMessageDialog(null,"Context intialized...!!!!!"); public void contextdestroyed(servletcontextevent evt) System.out.println("Context destroyed...!!!!!"); JOptionPane.showMessageDialog(null,"Context destroyed...!!!!!"); this.cont=null; web.xml <web-app> <listener> <listener-class>contextlistener</listener-class> </listener> <servlet> <servlet-name>demoservlet</servlet-name> <servlet-class>demoservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>demoservlet</servlet-name> <url-pattern>/demoservlet</url-pattern> </servlet-mapping> </web-app> Arjun V. Bala Page 22
14) Enlist and explain the need of filters with a program. (May-13,Jan-13,Jun-12) Need of Filters: Security verification Filters can be used for the security verification as a part of authentication process. Session validation Filters can be used for validating session for the application. Loggin operations Filters are required to keep the log of the operations taken place in the application. Internationalization It is used to implement the internationalization of the application. Triggering resource access events It is also used in the event handling procedure to trigger the events occurring for the resources. Image conversion & scaling maps Filters can be used for the image processing like conversing images in to large or small scale or scaling the map according to the users requirements. Data compression Filters needed to compress the data at the output so that the ultimate output will be of small in size. Encryption It is used to encrypt/decrypt the data, Filters are working at the end of the application so such operation can be useful for information security. Tokenization It can be used to tokenize the work, so that any one can use resources can be used with out worrying of dirty read/dirty write problem. Caching and XSL transformations of XML responses Filters are used to cache the data as well as transformation of XSL & XML. Debugging It can be used for debugging the application from the log file it has generated. Example: import java.io.ioexception; import java.io.printwriter; import javax.servlet.*; MyFilter.java public class MyFilter implements Filter public void init(filterconfig arg0) throws ServletException public void dofilter(servletrequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException PrintWriter out=resp.getwriter(); out.print("filter is invoked before"); chain.dofilter(req, resp);//sends request to next resource Arjun V. Bala Page 23
out.print("filter is invoked after"); public void destroy() web.xml <web-app> <servlet> <servlet-name>helloservlet</servlet-name> <servlet-class>helloservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>helloservlet</servlet-name> <url-pattern>/servlet1</url-pattern> </servlet-mapping> <filter> <filter-name>f1</filter-name> <filter-class>myfilter</filter-class> </filter> <filter-mapping> <filter-name>f1</filter-name> <url-pattern>/servlet1</url-pattern> </filter-mapping> </web-app> Arjun V. Bala Page 24