<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-299468836146488031</id><updated>2024-09-27T20:15:34.003-04:00</updated><category term="sendRedirect"/><category term="JSP lifecycle"/><category term="JSP runtime exception"/><category term="Life Cycle"/><category term="Servlet"/><category term="ServletConfig"/><category term="ServletContext"/><category term="action element"/><category term="cache control"/><category term="cookies"/><category term="declaration element"/><category term="doGet"/><category term="doPost"/><category term="errorPage"/><category term="expression"/><category term="filter"/><category term="forward"/><category term="implicit objects"/><category term="include"/><category term="isError"/><category term="request dispatcher"/><category term="scope"/><category term="scriplet"/><category term="servlet chaining"/><category term="servlet container"/><category term="servlet wrapper classes"/><category term="session tracking"/><category term="static and dynamic include"/><title type='text'>Java,J2EE Interview Questions and Answers</title><subtitle type='html'>This Blog contain the core java and JSP related all interview questions and answers.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://jksolutions-corejava.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/299468836146488031/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://jksolutions-corejava.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>jksolutions</name><uri>http://www.blogger.com/profile/01556365883008986930</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>4</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-299468836146488031.post-2346178825706598418</id><published>2011-10-02T02:28:00.001-04:00</published><updated>2011-10-02T02:28:05.772-04:00</updated><title type='text'>Struts Interview Questions and Answers(All in one)</title><content type='html'>&lt;b&gt;Q.1)What is Jakarta Struts Framework?&lt;/b&gt;&lt;br /&gt;Jakarta Struts is open source implementation of MVC (Model-View-Controller) pattern for the development of web based applications. Jakarta Struts is robust architecture and can be used for the development of application of any size. Struts framework makes it much easier to design scalable, reliable Web applications with Java.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Q.2)What is MVC ?&lt;/b&gt;&lt;br /&gt;The main aim of the MVC architecture&amp;nbsp; is to separate the business logic and application data from the presentation data to the user.&lt;br /&gt;&lt;b&gt;1).&amp;nbsp; Model: &lt;/b&gt;The model contains the core of the application&#39;s functionality. The model encapsulates the state of the application. Sometimes the only functionality it contains is state. It knows nothing about the view or controller.&lt;br /&gt;&lt;b&gt;2). View :&lt;/b&gt;The view provides the presentation of the model. It is the look of the application. The view can access the model getters, but it has no knowledge of the setters. In addition, it knows nothing about the controller. The view should be notified when changes to the model occur.&lt;br /&gt;&lt;b&gt;3). Controller:&lt;/b&gt;&amp;nbsp; Whenever the user sends a request for something then it always go through the controller. The controller is responsible for intercepting the requests from view and passes it to the model for the appropriate action. After the action has been taken on the data, the controller is responsible for directing the appropriate view to the user. In&amp;nbsp; GUIs, the views and the controllers often work very closely together.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Q.3)Why we should use the MVC design pattern?&lt;/b&gt;&lt;br /&gt;&lt;u&gt;They are resuable : &lt;/u&gt;When the problems recurs, there is no need to invent a new solution, we just have to follow the pattern and adapt it as necessary.&lt;br /&gt;&lt;u&gt;They are expressive:&lt;/u&gt; By using the MVC design pattern our application becomes more expressive.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Q.4)What is ActionServlet?&lt;/b&gt;&lt;br /&gt;A: The class org.apache.struts.action.ActionServlet is the called the ActionServlet. In the the Jakarta Struts Framework this class plays the role of controller. All the requests to the server goes through the controller. Controller is responsible for handling all the requests.&lt;br /&gt;&lt;b&gt;&lt;br /&gt;Q.5)What is role of ActionServlet?&lt;/b&gt;&lt;br /&gt;ActionServlet performs the role of Controller:&lt;br /&gt;1)Process user requests&lt;br /&gt;2)Determine what the user is trying to achieve according to the request&lt;br /&gt;3)Pull data from the model (if necessary) to be given to the appropriate view,&lt;br /&gt;4)Select the proper view to respond to the user&lt;br /&gt;5)Delegates most of this grunt work to Action classes&lt;br /&gt;6)Is responsible for initialization and clean-up of resources&lt;br /&gt;&lt;b&gt;&lt;br /&gt;Q.6)What is Action Class?&lt;/b&gt;&lt;br /&gt;The Action is part of the controller. The purpose of Action Class is to translate the HttpServletRequest to the business logic. To use the Action, we need to&amp;nbsp; Subclass and overwrite the execute()&amp;nbsp; method. The ActionServlet passes the parameterized class to Action Form using the execute() method. There should be no database interactions in the action. The action should receive the request, call business objects.&amp;nbsp; The return type of the execute method is ActionForward which is used by the Struts Framework to forward the request to the file as per the value of the returned ActionForward object.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Q.7)What is the ActionForm?&lt;/b&gt;&lt;br /&gt;ActionForm is javabean which represents the form inputs containing the request parameters from the View referencing the Action bean.&lt;br /&gt;
&lt;br /&gt;&lt;b&gt;Q.8)What is the life cycle of ActionForm?&lt;/b&gt;&lt;br /&gt;The lifecycle of ActionForm invoked by the RequestProcessor is as follows:&lt;br /&gt;1)Retrieve or Create Form Bean associated with Action&lt;br /&gt;2)&quot;Store&quot; FormBean in appropriate scope (request or session)&lt;br /&gt;3)Reset the properties of the FormBean&lt;br /&gt;4)Populate the properties of the FormBean&lt;br /&gt;5)Validate the properties of the FormBean&lt;br /&gt;6)Pass FormBean to Action&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Q.9)What is the signature of execute() method?&lt;/b&gt;&lt;br /&gt;public ActionForward execute( &lt;br /&gt;ActionMapping mapping,&lt;br /&gt;ActionForm form,HttpServletRequest request,&lt;br /&gt;
HttpServletResponse response)&lt;br /&gt;&amp;nbsp;throws Exception ;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Q.10)What is Struts Validator Framework?&lt;/b&gt;&lt;br /&gt;Struts Framework provides the functionality to validate the form data. It can be use to validate the data on the users browser as well as on the server side. Struts Framework emits the java scripts and it can be used validate the form data on the client browser. Server side validation of form can be accomplished by sub classing your From Bean with DynaValidatorForm class. &lt;br /&gt;&lt;br /&gt;
&lt;b&gt;Q.11)What design patterns are used in Struts?&lt;/b&gt;&lt;br /&gt;1)Service to Worker&lt;br /&gt;2)Dispatcher View&lt;br /&gt;3)Composite View (Struts Tiles)&lt;br /&gt;4)Front Controller&lt;br /&gt;5)View Helper&lt;br /&gt;6)Synchronizer Token&lt;br /&gt;&lt;b&gt;&lt;br /&gt;Q12)Give the Details of XML files used in Validator Framework?&lt;/b&gt;&lt;br /&gt;The Validator Framework uses two XML configuration files validator-rules.xml and validation.xml. The validator-rules.xml defines the standard validation routines, these are reusable and used in validation.xml. to define the form specific validations. The validation.xml defines the validations applied to a form bean.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Q.13)How you will display validation fail errors on jsp page?&lt;/b&gt;&lt;br /&gt;Use &amp;lt;html:errors/&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Q.14) How you will enable front-end validation based on the xml in validation.xml?&lt;/b&gt;&lt;br /&gt;The &amp;lt;html:javascript&amp;gt; tag to allow front-end validation based on the xml in validation.xml.&lt;br /&gt;For&amp;nbsp; example the code: &amp;lt;html:javascript formName=&quot;logonForm&quot; dynamicJavascript=&quot;true&quot; staticJavascript=&quot;true&quot; /&amp;gt; generates the client side java script for the form &quot;logonForm&quot; as defined in the validation.xml file. The &amp;lt;html:javascript&amp;gt; when added in the jsp file generates the client site validation script.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Q.15)What are the different kinds of actions in Struts?&lt;/b&gt;&lt;br /&gt;The different kinds of actions in Struts are:&lt;br /&gt;ForwardAction&lt;br /&gt;IncludeAction&lt;br /&gt;DispatchAction&lt;br /&gt;LookupDispatchAction&lt;br /&gt;SwitchAction&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Q.16)What is DispatchAction?&lt;/b&gt;&lt;br /&gt;The DispatchAction class is used to group related actions into one class. Using this class, you can have a method for each logical action compared than a single execute method. The DispatchAction dispatches to one of the logical actions represented by the methods. It picks a method to invoke based on an incoming request parameter. The value of the incoming parameter is the name of the method that the DispatchAction will invoke.&lt;br /&gt;&lt;b&gt;&lt;br /&gt;Q.17)What is the use of ForwardAction?&lt;/b&gt;&lt;br /&gt;ForwardAction to forward a request to another resource in your application, such as a Servlet that already does business logic processing or even another JSP page. By using this predefined action, you don’t have to write your own Action class.You just have to set up the struts-config file properly to use ForwardAction.&lt;br /&gt;&lt;b&gt;&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Q.18)What is IncludeAction?&lt;/b&gt;&lt;br /&gt;The IncludeAction class is useful when you want to integrate Struts into an application that uses Servlets. Use the IncludeAction class to include another resource in the response to the request being processed.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Q.19)What is LookupDispatchAction?&lt;/b&gt;&lt;br /&gt;The LookupDispatchAction is a subclass of DispatchAction. It does a reverse lookup on the resource bundle to get the key and then gets the method whose name is associated with the key into the Resource Bundle.&lt;br /&gt;
&lt;br /&gt;&lt;b&gt;Q.20)What is the use of LookupDispatchAction?&lt;/b&gt;&lt;br /&gt;LookupDispatchAction is useful if the method name in the Action is not driven by its name in the front end, but by the Locale independent key into the resource bundle. Since the key is always the same, the LookupDispatchAction shields your application from the side effects of I18N.&lt;br /&gt;
&lt;br /&gt;&lt;b&gt;Q.21)What is difference between LookupDispatchAction and DispatchAction?&lt;/b&gt;&lt;br /&gt;The difference between LookupDispatchAction and DispatchAction is that the actual method that gets called in LookupDispatchAction is based on a lookup of a key value instead of specifying the method name directly.&lt;br /&gt;
&lt;br /&gt;&lt;b&gt;Q.22)What is SwitchAction?&lt;/b&gt;&lt;br /&gt;The SwitchAction class provides a means to switch from a resource in one module to another resource in a different module. SwitchAction is useful only if you have multiple modules in your Struts application. The SwitchAction class can be used as is, without extending.&lt;br /&gt;&lt;b&gt;&lt;br /&gt;Q.23)What are the various Struts tag libraries?&lt;/b&gt;&lt;br /&gt;Struts is very rich framework and it provides very good and user friendly way to develop web application forms. Struts provide many tag libraries to ease the development of web applications. These tag libraries are:&lt;br /&gt;1)Bean tag library - Tags for accessing JavaBeans and their properties.&lt;br /&gt;2)HTML tag library - Tags to output standard HTML, including forms, text boxes, checkboxes, radio buttons etc..&lt;br /&gt;3)Logic tag library - Tags for generating conditional output, iteration capabilities and flow management&lt;br /&gt;4)Tiles or Template tag library - For the application using tiles&lt;br /&gt;5)Nested tag library - For using the nested beans in the application&lt;br /&gt;6)Template&lt;br /&gt;&lt;b&gt;&lt;br /&gt;Q.24)Can we have more than one struts-config.xml file for a single Struts application?&lt;/b&gt;&lt;br /&gt;Yes, we can have more than one struts-config.xml for a single Struts application. They can be configured as follows:&lt;br /&gt;&amp;lt;servlet&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;servlet-name&amp;gt;banking&amp;lt;/servlet-name&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;servlet-class&amp;gt;org.apache.struts.action.ActionServlet&lt;br /&gt;&amp;nbsp; &amp;lt;/servlet-class&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;init-param&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;param-name&amp;gt;config&amp;lt;/param-name&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;param-value&amp;gt;/WEB-INF/struts-config.xml,&lt;br /&gt;&amp;nbsp; /WEB-INF/struts-authentication.xml,&lt;br /&gt;&amp;nbsp; /WEB-INF/struts-help.xml&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;lt;/param-value&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;/init-param&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;load-on-startup&amp;gt;1&amp;lt;/load-on-startup&amp;gt;&lt;br /&gt;&amp;lt;/servlet&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Q.25)What are the core classes of the Struts Framework?&lt;/b&gt;&lt;br /&gt;Core classes of Struts Framework are ActionForm, Action, ActionMapping, ActionForward, ActionServlet etc.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Q.26)What is DynaActionForm?&lt;/b&gt;&lt;br /&gt;A specialized subclass of ActionForm that allows the creation of form beans with dynamic sets of properties (configured in configuration file), without requiring the developer to create a Java class for each type of form bean.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Q.27)How the exceptions are handled in struts?&lt;/b&gt;&lt;br /&gt;Struts handle exception in two ways:-&lt;br /&gt;Programmatic exception handling :&lt;br /&gt;Explicit try/catch blocks in any code that can throw exception. It works well when custom value (i.e., of variable) needed when error occurs. &lt;br /&gt;Declarative exception handling :You can either define &amp;lt;global-exceptions&amp;gt; handling tags in your struts-config.xml or define the exception handling tags within &amp;lt;action&amp;gt;&amp;lt;/action&amp;gt; tag. It works well when custom page needed when error occurs. This approach applies only to exceptions thrown by Actions.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Q.28)What are differences between &amp;lt;bean:message&amp;gt; and &amp;lt;bean:write&amp;gt;?&lt;/b&gt;&lt;br /&gt;&amp;lt;bean:message&amp;gt;: is used to retrive keyed values from resource bundle. It also supports the ability to include parameters that can be substituted for defined placeholders in the retrieved string.&lt;br /&gt;&amp;lt;bean:message key=&quot;prompt.customer.firstname&quot;/&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;bean:write&amp;gt;: is used to retrieve and print the value of the bean property. &amp;lt;bean:write&amp;gt; has no body.&lt;br /&gt;&amp;lt;bean:write name=&quot;customer&quot; property=&quot;firstName&quot;/&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Q.29)What are difference between ActionErrors and ActionMessage?&lt;/b&gt;&lt;br /&gt;&lt;b&gt;ActionMessage:&lt;/b&gt; A class that encapsulates messages. Messages can be either global or they are specific to a particular bean property.Each individual message is described by an ActionMessage object, which contains a message key,and up to four placeholder arguments used for parametric substitution in the resulting message.&lt;br /&gt;&lt;b&gt;ActionErrors:&lt;/b&gt; A class that encapsulates the error messages being reported by the validate() method of an ActionForm. Validation errors are either global to the entire ActionForm bean they are associated with, or they are specific to a particular bean property&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Q.30)Is struts threadsafe?Give an example?&lt;/b&gt;&lt;br /&gt;Struts is not only thread-safe but thread-dependant. The response to a request is handled by a light-weight Action object, rather than an individual servlet. Struts instantiates each Action class once, and allows other requests to be threaded through the original object. This core strategy conserves resources and provides the best possible throughput. A properly-designed application will exploit this further by routing related operations through a single Action&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Q.31)What are the disadvantages of Struts?&lt;/b&gt;&lt;br /&gt;Struts is very robust framework and is being used extensively in the industry. But there are some disadvantages of the Struts: a) High Learning Curve Struts requires lot of efforts to learn and master it. For any small project less experience developers could spend more time on learning the Struts.&lt;br /&gt;Harder to learn Struts are harder to learn, benchmark and optimize.&lt;br /&gt;&lt;b&gt;&lt;br /&gt;Q.32)How Struts relates to J2EE?&lt;/b&gt;&lt;br /&gt;Struts framework&amp;nbsp; is built on J2EE technologies (JSP, Servlet, Taglibs), but it is itself not part of the J2EE standard.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Q.33)What if &amp;lt;action&amp;gt; element has &amp;lt;forward&amp;gt; declaration with same name as global forward?&lt;/b&gt;&lt;br /&gt;In this case the global forward is not used. Instead the &amp;lt;action&amp;gt; element’s &amp;lt;forward&amp;gt; takes precendence.&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://jksolutions-corejava.blogspot.com/feeds/2346178825706598418/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://jksolutions-corejava.blogspot.com/2011/10/struts-interview-questions-and.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/299468836146488031/posts/default/2346178825706598418'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/299468836146488031/posts/default/2346178825706598418'/><link rel='alternate' type='text/html' href='http://jksolutions-corejava.blogspot.com/2011/10/struts-interview-questions-and.html' title='Struts Interview Questions and Answers(All in one)'/><author><name>jksolutions</name><uri>http://www.blogger.com/profile/01556365883008986930</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-299468836146488031.post-5380780000052367805</id><published>2011-10-01T12:23:00.000-04:00</published><updated>2012-05-25T13:51:24.588-04:00</updated><title type='text'>Java Thread</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;Introduction to Threads&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;A &lt;b&gt;thread&lt;/b&gt; is a thread of execution in a program. The Java 
 Virtual Machine allows an application to have multiple threads of 
 execution running concurrently. 
 &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;Every thread has a priority. Threads with higher priority are 
 executed in preference to threads with lower priority. Each thread 
 may or may not also be marked as a daemon. When code running in 
 some thread creates a new &lt;code&gt;Thread&lt;/code&gt; object, the new 
 thread has its priority initially set equal to the priority of the 
 creating thread, and is a daemon thread if and only if the 
 creating thread is a daemon.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;This approach of creating a thread by implementing the Runnable Interface must be used whenever the class being used to instantiate the thread object is required to extend some other class.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;style5&quot; style=&quot;color: black; font-family: Verdana,sans-serif; font-weight: bold; line-height: 1.4; text-align: left; text-decoration: underline;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;Multithreading has several advantages over Multiprocessing such as;&lt;/span&gt;&lt;/div&gt;
&lt;ul style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Threads are lightweight compared to&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt; process&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Threads share the same address space and therefore can share both data and code&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Context switching between threads is usually less expensive than between&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;processes&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Cost of thread intercommunication is relatively low that that of process intercommunication&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Threads allow different tasks to be performed concurrently.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;Methods of the Object and Thread Class.&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;Object: notify,notifyAll(),wait()&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;Thread:-sleep(),yield()&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;Thread Creation&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;style11&quot; style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;There are two ways to&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt; create &lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;thread in&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;java;&lt;/span&gt;&lt;/div&gt;
&lt;ul style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Implement the Runnable interface (java.lang.Runnable)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;By Extending the Thread class (java.lang.Thread)&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 style=&quot;color: black; font-family: Verdana,sans-serif; font-weight: normal; text-align: left;&quot;&gt;




&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;&lt;span class=&quot;style1&quot; style=&quot;text-decoration: underline;&quot;&gt;Implementing the Runnable Interface&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;center style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;&lt;/center&gt;&lt;br /&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;text5&quot; style=&quot;color: black; font-family: Verdana,sans-serif; line-height: 1.6; text-align: left; text-decoration: underline;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;The Runnable Interface Signature&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;public interface Runnable {&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;void run();&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;One way to&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;create&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;a thread in&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;is to implement the Runnable Interface and then instantiate an object of the class. We need to override the run() method into our class which is the only method that needs to be implemented. The run() method contains the logic of the thread.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;style11&quot; style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;The procedure for creating threads based on the Runnable interface is as follows:&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;1. A class implements the Runnable interface, providing the&lt;b&gt; run()&lt;/b&gt; method that will be executed by the thread. An object of this class is a Runnable object.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;2. An object of Thread class is created by passing a Runnable object as argument to the Thread constructor. The Thread object now has a Runnable object that implements the run() method.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;3. The start() method is invoked on the Thread object created in the previous step. The start() method returns immediately after a thread has been spawned.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;4. The thread ends when the run() method ends, either by normal completion or by throwing an uncaught exception.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;Below is a&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;program&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;that illustrates instantiation and running of threads using the runnable interface instead of extending the Thread class. To start the thread you need to invoke the&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;start()&lt;/b&gt;&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;method on your object.&lt;/span&gt;&lt;/div&gt;
&lt;h2 style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;



&lt;span style=&quot;font-size: small;&quot;&gt;
Extending Thread Class&lt;/span&gt;&lt;/h2&gt;
&lt;div class=&quot;style11&quot; style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;The procedure for creating threads&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;based on&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;extending the Thread is as follows:&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;1. A class extending the Thread class overrides the run() method from the Thread class to define the code &lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;executed by the thread.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;2. This subclass may call a Thread constructor explicitly in its constructors to initialize the thread, using the super() call.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;3. The start() method inherited from the Thread class is invoked on the object of the class to make the thread eligible for running.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span class=&quot;style11&quot; style=&quot;font-size: small;&quot;&gt;Below is a&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;program&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;that illustrates instantiation and running of threads by extending the Thread class instead of implementing the Runnable interface. To start the thread you need to invoke the&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;b&gt;start()&lt;/b&gt;&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;method on your object.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;When creating threads, there are two reasons why implementing the Runnable interface may be preferable to extending the Thread class:&lt;/span&gt;&lt;/div&gt;
&lt;ul style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Extending the Thread class means that the subclass cannot extend any other class, whereas a class implementing the Runnable interface has this option.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;A class might only be interested in being runnable, and therefore, inheriting the full overhead of the Thread class would be excessive.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;u&gt;&lt;b&gt;Synchronization:-&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;With respect to multithreading,Synchronization &lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;is a process of controlling the access of shared resources by the multiple threads in such a manner that only one thread can access a particular resource at a time.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;In non synchronized multithreaded application, it is possible for one thread to modify a shared object while another thread is in the process of using or updating the object’s value. Synchronization prevents such type of data corruption which may otherwisel lead to dirty reads and significant errors.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;Generally critical sections of&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;the code&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;are usually marked with synchronized keyword.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;example of using Thread Synchronization is in “The Producer/Consumer Model”.&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span class=&quot;Apple-converted-space&quot;&gt;Locks:-&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;are used to synchronize access to a shared resource. A lock can be associated with a shared resource.Threads gain access to a shared resource by first acquiring the lock associated with the object/block of code.At any given time, at most only one thread can hold the lock and thereby have access to the shared resource.A lock thus implements mutual exclusion.The object lock mechanism enforces the following rules of synchronization:&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;A thread must acquire the object lock associated with a shared resource, before it can enter the shared resource. The runtime&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;ensures that no other thread can enter a shared resource if another thread already holds the object lock associated with the shared resource. If a thread cannot immediately acquire the object lock, it is blocked, that is, it must wait for the lock to become available.When a thread exits a shared resource, the runtime&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;ensures that the object lock is also relinquished.If another thread is waiting for this object lock, it can proceed to acquire the lock in order to gain access to the shared resource.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;Classes &lt;/b&gt;also have a class-specific lock that is analogous to the object lock. Such a lock is actually a&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;lock on the&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;java.lang.Class object associated with the class. Given a class A, the reference A.class&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;denotes this unique Class object. The class lock can be used in much the same way as an object lock to implement mutual exclusion.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;style11&quot; style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;There can be 2 ways through which synchronized can be implemented in&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Java:&lt;/span&gt;&lt;/div&gt;
&lt;ul style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;synchronized methods&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;synchronized blocks&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;Synchronized statements are same as synchronized methods. A synchronized statement can only be&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;executed after a thread has acquired the lock on the object/class referenced in the synchronized statement.&lt;/span&gt;&lt;/div&gt;
&lt;h1 style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;




&lt;span class=&quot;style1&quot; style=&quot;font-size: small; text-decoration: underline;&quot;&gt;Synchronized Methods&lt;/span&gt;&lt;/h1&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;Synchronized methods are methods that are used to control access to an object. A thread only executes a synchronized method after it has acquired the lock for the method’s object or class. .If the lock is already held by another thread, the calling thread waits. A thread relinquishes the lock simply by returning from the synchronized method, allowing the next thread waiting for this lock to proceed. Synchronized methods are useful in situations where methods can manipulate the state of an object in ways that can corrupt the state if executed concurrently. This is called a race condition. It occurs when two or more threads simultaneously update the same value, and as a consequence, leave the value in an undefined or inconsistent state. While a thread is inside a synchronized method of an object, all other threads that wish to execute this synchronized method or any other synchronized method of the object will have to wait until it gets the lock. This restriction does not apply to the thread that already has the lock and is executing a synchronized method of the object. Such a method can invoke other synchronized methods of the object without being blocked. The non-synchronized methods of the object can of course be called at any time by any thread.&lt;/span&gt;&lt;/div&gt;
&lt;h1 style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;




&lt;span class=&quot;style1&quot; style=&quot;font-size: small; text-decoration: underline;&quot;&gt;Synchronized Blocks&lt;/span&gt;&lt;/h1&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;Static methods synchronize on the class lock. Acquiring and relinquishing a class lock by a thread in order to execute a static synchronized method, proceeds analogous to that of an object lock for a synchronized instance method. A thread acquires the class lock before it can proceed with the execution of any static synchronized method in the class, blocking other threads wishing to execute any such methods in the same class. This, of course, does not apply to static, non-synchronized methods, which can be invoked at any time. Synchronization of static methods in a class is independent from the synchronization of instance methods on objects of the class. A subclass decides whether the new definition of an inherited synchronized method will remain synchronized in the subclass.The synchronized block allows execution of arbitrary code to be synchronized on the lock of an arbitrary object.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;The general form of the synchronized block is as follows:&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;synchronized (&amp;lt;object reference expression&amp;gt;) {&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&amp;lt;code block&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;A compile-time error occurs if the expression produces a value of any primitive type. If execution of the block completes normally, then the lock is released. If execution of the block completes abruptly, then the lock is released.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;A thread can hold more than one lock at a time. Synchronized statements can be nested. Synchronized statements with identical expressions can be nested. The expression must evaluate to a non-null reference value, otherwise, a NullPointerException is thrown.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;The code block is usually related to the object on which the synchronization is being done. This is the case with synchronized methods, where the execution of the method is synchronized on the lock of the current object:&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;public Object method() {&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;synchronized (this) { // Synchronized block on current object&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;// method block&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;Once a thread has entered the code block after acquiring the lock on the specified object, no other thread will be able to execute the code block, or any other code requiring the same object lock, until the lock is relinquished. This happens when the execution of the code block completes normally or an uncaught exception is thrown.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;Object specification in the synchronized statement is mandatory. A class can choose to synchronize the execution of a part of a method, by using the this reference and putting the relevant part of the method in the synchronized block. The braces of the block cannot be left out, even if the code block has just one statement.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;class SmartClient {&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;BankAccount account;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;// …&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;public void updateTransaction() {&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;synchronized (account) { // (1) synchronized block&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;account.update(); // (2)&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;style11&quot; style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;In summary, a thread can hold a lock on an object&lt;/span&gt;&lt;/div&gt;
&lt;ul style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;by executing a synchronized instance method of the object&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;by executing the body of a synchronized block that synchronizes on the object&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;by executing a synchronized static method of a class&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span class=&quot;style1&quot;&gt;Thread States&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;A Java thread is always in one of several states which could be running, sleeping, dead, etc.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;A thread can be in any of the following states:&lt;/span&gt;&lt;/div&gt;
&lt;ul style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;New Thread state (Ready-to-run state)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Runnable state (Running state)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Not Runnable state&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Dead state&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;




&lt;span class=&quot;style1&quot; style=&quot;font-size: small;&quot;&gt;New Thread&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;A thread is in this state when the instantiation of a&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;Thread&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;object creates a new thread but does not&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;start it running. A thread starts life in the Ready-to-run state. You can call only the&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;start()&lt;/b&gt;&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;or&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;stop()&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;methods when the thread is in this state. Calling any method besides&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;start()&lt;/b&gt;&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;or&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;stop()&lt;/b&gt;&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;causes an&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;IllegalThreadStateException.&lt;/span&gt;&lt;/div&gt;
&lt;h2 style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;



&lt;span style=&quot;font-size: small;&quot;&gt;
Runnable&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;When the&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;start()&lt;/b&gt;&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;method is invoked on a New Thread() it gets to the runnable state or running state by calling the run() method. A Runnable thread may actually be running, or may be awaiting its turn to run.&lt;/span&gt;&lt;/div&gt;
&lt;h2 style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;



&lt;span style=&quot;font-size: small;&quot;&gt;
Not Runnable&lt;/span&gt;&lt;/h2&gt;
&lt;div class=&quot;style11&quot; style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;A thread becomes Not Runnable when one of the following four events occurs:&lt;/span&gt;&lt;/div&gt;
&lt;ul style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;When&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;sleep()&lt;/b&gt;&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;method is invoked and it sleeps for a specified amount of time&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;When&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;suspend()&lt;/b&gt;&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;method is invoked&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;When&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;the wait()&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;method is invoked and the thread waits for notification of a free resource or waits for&lt;br /&gt;the completion of another thread or waits to acquire a lock of an object.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;The thread is blocking on I/O and waits for its completion&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;Example:&lt;/b&gt;&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Thread.currentThread().sleep(1000);&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;Note:&lt;/b&gt;&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Thread.currentThread() may return an output like Thread[threadA,5,main]&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;style11&quot; style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;The output shown in bold describes&lt;/span&gt;&lt;/div&gt;
&lt;ul style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;the name of the thread,&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;the priority of the thread, and&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;the name of the group to which it belongs.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;A thread can be awakened abruptly by invoking the&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;interrupt()&lt;/b&gt;&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;method on the sleeping thread object or at the end of the period of time for sleep is over. Whether or not it will actually start running depends on its priority and the availability of the CPU.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;How a thread switches form a non runnable to a runnable state:&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;li style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;If a thread has been put to sleep, then the specified number of milliseconds must elapse (or it must be interrupted).&lt;/span&gt;&lt;/li&gt;
&lt;br /&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;li style=&quot;color: black; font-family: Verdana,sans-serif; text-align: justify;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;If a thread has been suspended, then its&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;resume()&lt;/b&gt;&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;method must be invoked&lt;/span&gt;&lt;/li&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;li style=&quot;color: black; font-family: Verdana,sans-serif; text-align: justify;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;If a thread is waiting on a condition variable, whatever object owns the variable must relinquish it by calling&lt;br /&gt;either&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;notify()&lt;/b&gt;&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;or&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;notifyAll()&lt;/b&gt;.&lt;/span&gt;&lt;/li&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;li style=&quot;color: black; font-family: Verdana,sans-serif; text-align: justify;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;If a thread is blocked on I/O, then the I/O must complete.&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: justify;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;Dead State&lt;/b&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: justify;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;A thread enters this state when the&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;run()&lt;/b&gt;&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;method has finished executing or when the&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;stop()&lt;/b&gt;&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;method is invoked. Once in this state, the thread cannot ever run again.&lt;/span&gt;

&lt;/div&gt;
&lt;h1 style=&quot;color: black; font-family: Verdana,sans-serif; text-align: justify;&quot;&gt;




&lt;span class=&quot;style1&quot; style=&quot;font-size: small; text-decoration: underline;&quot;&gt;Thread Priority&lt;/span&gt;&lt;/h1&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: justify;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;
In Java we can specify the priority of each thread&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;style11&quot; style=&quot;font-size: small;&quot;&gt;relative to other threads&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;. Those threads having higher priority get greater access to&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;style11&quot; style=&quot;font-size: small;&quot;&gt;available resources&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;then lower priority threads. A Java thread&lt;/span&gt;&lt;span class=&quot;style11&quot; style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;inherits its priority&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt; from the thread that created it. Heavy reliance on thread priorities for the behavior of a program can make the program non portable across platforms, as thread scheduling is host platform–dependent.You can&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;style11&quot; style=&quot;font-size: small;&quot;&gt;modify a thread’s priority&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;at any time after its creation using the&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;setPriority()&lt;/b&gt;&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;method and retrieve the thread priority value using&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;getPriority()&lt;/b&gt;&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;method.&lt;br /&gt;
The following static final integer constants are defined in the&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;Thread&lt;/b&gt;&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;class:&lt;/span&gt;
&lt;/div&gt;
&lt;ul style=&quot;color: black; font-family: Verdana,sans-serif; text-align: justify;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;MIN_PRIORITY (0) Lowest Priority&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;NORM_PRIORITY (5) Default Priority&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;MAX_PRIORITY (10) Highest Priority&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: justify;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;
The priority of an individual thread can be set to&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;style11&quot; style=&quot;font-size: small;&quot;&gt;any integer value between and including&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;the above defined constants.When two or more threads are ready to be executed and system resource becomes available to execute a thread, the runtime system (the thread scheduler)&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;style11&quot; style=&quot;font-size: small;&quot;&gt;chooses&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;the&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;i&gt;Runnable&lt;/i&gt;&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;thread with the&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;style11&quot; style=&quot;font-size: small;&quot;&gt;highest priority&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;for execution.“If two threads of the&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;style11&quot; style=&quot;font-size: small;&quot;&gt;same priority&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;are waiting for the CPU, the thread scheduler chooses one of them to run in a &amp;gt;&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;round-robin fashion&lt;/b&gt;&lt;i&gt;.&lt;/i&gt;&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;The chosen thread will run until one of the following conditions is true:&lt;/span&gt;
&lt;/div&gt;
&lt;ul style=&quot;color: black; font-family: Verdana,sans-serif; text-align: justify;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;style11&quot; style=&quot;font-size: small;&quot;&gt;higher priority&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;thread becomes&lt;i&gt;&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;Runnable&lt;/i&gt;. &lt;b&gt;(Pre-emptive scheduling)&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;it&lt;i&gt;&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;yields&lt;/i&gt;, or its&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;run()&lt;/b&gt;&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;method exits&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;on systems that support&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;i&gt;time-slicing&lt;/i&gt;, its time allotment has&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;style11&quot; style=&quot;font-size: small;&quot;&gt;expired&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 class=&quot;style1&quot; style=&quot;color: black; font-family: Verdana,sans-serif; text-align: justify; text-decoration: underline;&quot;&gt;



&lt;span style=&quot;font-size: small;&quot;&gt;
Thread Scheduler&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: justify;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;
Schedulers in JVM implementations usually employ one of the two following strategies:&lt;/span&gt;
&lt;/div&gt;
&lt;div class=&quot;style5&quot; style=&quot;color: black; font-family: Verdana,sans-serif; font-weight: bold; line-height: 1.4; text-align: justify; text-decoration: underline;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;Preemptive scheduling&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: justify;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;
If a thread with a higher priority than all other Runnable threads becomes Runnable, the scheduler will preempt the running thread (is moved to the runnable state) and choose the new higher priority thread for execution.&lt;/span&gt;
&lt;/div&gt;
&lt;div class=&quot;style5&quot; style=&quot;color: black; font-family: Verdana,sans-serif; font-weight: bold; line-height: 1.4; text-align: justify; text-decoration: underline;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;Time-Slicing or Round-Robin scheduling&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: justify;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;
A running thread is allowed to execute for a fixed length of time (a time slot it’s assigned to), after which it moves to the Ready-to-run state (runnable) to await its turn to run again.&lt;br /&gt;
A thread scheduler is implementation and platform-dependent; therefore, how threads will be scheduled is unpredictable across different platforms.&lt;/span&gt;
&lt;/div&gt;
&lt;h2 style=&quot;color: black; font-family: Verdana,sans-serif; text-align: justify;&quot;&gt;



&lt;span style=&quot;font-size: small;&quot;&gt;
Yielding&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: justify;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;
A call to the static method yield(), defined in the Thread class, will cause the current thread in the Running state to move to the Runnable state, thus relinquishing the CPU. The thread is then at the mercy of the thread scheduler as to when it will run again. If there are no threads waiting in the Ready-to-run state, this thread continues execution. If there are other threads in the Ready-to-run state, their priorities determine which thread gets to execute. The&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;yield()&lt;/b&gt;&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;method gives other threads of the same priority a chance to run. If there are no equal priority threads in the “Runnable” state, then the yield is ignored.&lt;/span&gt;
&lt;/div&gt;
&lt;h2 style=&quot;color: black; font-family: Verdana,sans-serif; text-align: justify;&quot;&gt;



&lt;span style=&quot;font-size: small;&quot;&gt;
Sleeping and Waking Up&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: justify;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;
The thread class contains a static method named sleep() that causes the currently running thread to pause its execution and transit to the Sleeping state. The method does not relinquish any lock that the thread might have. The thread will sleep for at least the time specified in its argument, before entering the runnable state where it takes its turn to run again. If a thread is interrupted while sleeping, it will throw an InterruptedException when it awakes and gets to execute. The Thread class has several overloaded versions of the&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;sleep()&lt;/b&gt;&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;method.&lt;/span&gt;
&lt;/div&gt;
&lt;h2 style=&quot;color: black; font-family: Verdana,sans-serif; text-align: justify;&quot;&gt;



&lt;span style=&quot;font-size: small;&quot;&gt;
Waiting and Notifying&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;
Waiting and notifying provide means of thread inter-communication that synchronizes on the same object. The threads execute wait() and notify() (or notifyAll()) methods on the shared object for this purpose. The notifyAll(), notify() and wait() are methods of the Object class. These methods can be invoked only from within a synchronized context (synchronized method or synchronized block), otherwise, the call will result in an &lt;b&gt;IllegalMonitorStateException&lt;/b&gt;. The notifyAll() method wakes up all the threads waiting on the resource. In this situation, the awakened threads compete for the resource. One thread gets the resource and the others go back to waiting.&lt;/span&gt;
&lt;/div&gt;
&lt;div class=&quot;style11&quot; style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;wait() method signatures&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;
final void wait(long timeout) throws InterruptedException&lt;br /&gt;final void wait(long timeout, int nanos) throws InterruptedException&lt;br /&gt;final void wait() throws InterruptedException .&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;wait() call can specify the time the thread should wait before being timed out. An another thread can invoke an interrupt() method on a waiting thread resulting in an InterruptedException. This is a checked exception and hence the code with the wait() method must be enclosed within a try catch block.&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;style11&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;
&lt;span class=&quot;style11&quot; style=&quot;font-size: small;&quot;&gt;notify() method signatures&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
final void notify()&lt;br /&gt;final void notifyAll()&lt;br /&gt;
A thread usually calls the wait() method on the object whose lock it holds because a condition for its continued execution was not met. The thread leaves the Running state and transits to the Waiting-for-notification state. There it waits for this condition to occur. The thread relinquishes ownership of the object lock. The releasing of the lock of the shared object by the thread allows other threads to run and execute synchronized code on the same object after acquiring its lock.&lt;br /&gt;The wait() method causes the current thread to wait until another thread notifies it of a condition change.&lt;/span&gt;&lt;span class=&quot;style1&quot; style=&quot;font-size: small; text-decoration: underline;&quot;&gt;Joining&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;
A thread invokes the join() method on another thread in order to wait for the other thread to complete its execution.&lt;br /&gt;Consider a thread t1 invokes the method join() on a thread t2. The join() call has no effect if thread t2 has already completed. If thread t2 is still alive, then thread t1 transits to the Blocked-for-join-completion state.&lt;/span&gt;
&lt;/div&gt;
&lt;h2 style=&quot;color: black; font-family: Verdana,sans-serif; text-align: justify;&quot;&gt;



&lt;span style=&quot;font-size: small;&quot;&gt;
Deadlock&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: justify;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;
There are situations when programs become deadlocked when each thread is waiting on a resource that cannot become available. The simplest form of deadlock is when two threads are each waiting on a resource that is locked by the other thread. Since each thread is waiting for the other thread to relinquish a lock, they both remain waiting forever in the Blocked-for-lock-acquisition state. The threads are said to be deadlocked.&lt;br /&gt;&lt;b&gt;Note:&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;
&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;The following methods namely&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;join, sleep&lt;/b&gt;&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;and&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;wait&lt;/b&gt;&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;name the InterruptedException in its throws clause and can have a timeout argument as a parameter. The following methods namely&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;wait, notify&lt;/b&gt;&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;and&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;notifyAll&lt;/b&gt;should only be called by a thread that holds the lock of the instance on which the method is invoked. The Thread.start method causes a new thread to get ready to run at the discretion of the thread scheduler. The Runnable interface declares the run method. The Thread class implements the Runnable interface. Some implementations of the Thread.yield method will not yield to a thread of lower priority. A program will terminate only when all user threads stop running. A thread inherits its daemon status from the thread that created it&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif; text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jksolutions-corejava.blogspot.com/feeds/5380780000052367805/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://jksolutions-corejava.blogspot.com/2011/10/java-thread.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/299468836146488031/posts/default/5380780000052367805'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/299468836146488031/posts/default/5380780000052367805'/><link rel='alternate' type='text/html' href='http://jksolutions-corejava.blogspot.com/2011/10/java-thread.html' title='Java Thread'/><author><name>jksolutions</name><uri>http://www.blogger.com/profile/01556365883008986930</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-299468836146488031.post-1300103879453795167</id><published>2011-10-01T02:57:00.002-04:00</published><updated>2011-10-01T02:57:47.424-04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="cookies"/><category scheme="http://www.blogger.com/atom/ns#" term="doGet"/><category scheme="http://www.blogger.com/atom/ns#" term="doPost"/><category scheme="http://www.blogger.com/atom/ns#" term="filter"/><category scheme="http://www.blogger.com/atom/ns#" term="forward"/><category scheme="http://www.blogger.com/atom/ns#" term="include"/><category scheme="http://www.blogger.com/atom/ns#" term="Life Cycle"/><category scheme="http://www.blogger.com/atom/ns#" term="request dispatcher"/><category scheme="http://www.blogger.com/atom/ns#" term="sendRedirect"/><category scheme="http://www.blogger.com/atom/ns#" term="Servlet"/><category scheme="http://www.blogger.com/atom/ns#" term="servlet chaining"/><category scheme="http://www.blogger.com/atom/ns#" term="servlet container"/><category scheme="http://www.blogger.com/atom/ns#" term="servlet wrapper classes"/><category scheme="http://www.blogger.com/atom/ns#" term="ServletConfig"/><category scheme="http://www.blogger.com/atom/ns#" term="ServletContext"/><category scheme="http://www.blogger.com/atom/ns#" term="session tracking"/><title type='text'>Servlet Interview Question and Answers</title><content type='html'>&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;Q.1)What is the difference between CGI and Servlet?&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;CGI:-&lt;/b&gt;Traditional CGI creates a heavy weight process to handle each http request. N number of copies of the same traditional CGI programs is copied into memory to serve N number of requests. &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;Servlet:-&lt;/b&gt;Its a lightweight Java thread to handle each http request. Single copy of a type of servlet but N number of threads (thread sizes can be configured in an application server).&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;A Servlet is a Java class that runs within a web container in an application server, servicing multiple client requests concurrently forwarded through the server and the web container. The web browser establishes a socket connection to the host server in the URL , and sends the HTTP request. Servlets can forward requests to other servers and servlets and can also be used to balance load among several servers. &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;Q.2)Which protocol is used to communicate between a browser and a servlet?&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;A browser and a servlet communicate using the HTTP protocol (a stateless request/response based protocol).&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;Q.3) What are the two objects a servlet receives when it accepts a call from its client?&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;A “ServletRequest”, which encapsulates client request from the client and the “ServletResponse”, which encapsulates the communication from the servlet back to the client.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;Q.4) What can you do in your Servlet/JSP code to tell browser not to cache the pages?&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;response.setHeader(“Cache-Control”,“no-cache”); //document should never be cached. HTTP 1.1&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;response.setHeader(“Pragma”, “no-cache”); //HTTP 1.0&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;response.setDateHeader(“Expires”, 0);&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;Q.5) What is the difference between request parameters and request attributes? &lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;Request parameters:&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;a)Parameters are form data that are sent in the request from the HTML page. These parameters are generally form fields in an HTML form.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;b)You can get them but cannot set them. &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;Request attributes:&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;a)Once a servlet gets a request, it can add additional attributes, then forward the request off to other servlets or JSPs for processing. Servlets and JSPs can communicate with each other by setting and getting attributes.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;b)You can both set the attribute and get the attribute. You can also get and set the attributes in session and application scopes. &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Q.6) What are the different scopes or places where a servlet can save data for its processing?&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;request&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;session&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;application&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Q.7)How does a Servlet differ from an Applet?&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Applet:&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;Applets execute on a browser&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;Applets have a graphical user interface&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Servlet: &lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;Servlets execute within a web container in an Application Server.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;Servlets do not have a graphical user interface.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Q.8)HTTP is a stateless protocol, so, how do you maintain state? How do you store user data between requests? &lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;HTTP Sessions are the recommended approach. A session identifies the requests that originate from the same browser during the period of conversation. All the servlets can share the same session. The JSESSIONID is generated by the server and can be passed to client through cookies, URL re-writing (if cookies are turned off) or built-in SSL mechanism. Care should be taken to minimize size of objects stored in session and objects stored in session should be serializable.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;b style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Verdana,sans-serif; font-size: small;&quot;&gt;&lt;span class=&quot;que&quot;&gt; 
&lt;span class=&quot;queIndex&quot;&gt;Q.9)&lt;/span&gt;What are the types of Session Tracking ?&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;
 &lt;/span&gt;&lt;/b&gt;&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;Sessions need to work with all web 
browsers and take into account the users security preferences. Therefore
 there are a variety of ways to send and receive the identifier:
&lt;/span&gt;&lt;/div&gt;
&lt;ul style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;strong&gt;URL rewriting : &lt;/strong&gt; URL rewriting is a method of 
session tracking in which some extra data (session ID) is appended at 
the end of each URL. This extra data identifies the session. The server 
can associate this session identifier with the data it has stored about 
that session. This method is used with browsers that do not support 
cookies or where the user has disabled the cookies. &lt;/span&gt;&lt;/li&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;strong&gt;Hidden Form Fields :&lt;/strong&gt; Similar to URL rewriting. The 
server embeds new hidden fields in every dynamically generated form page
 for the client. When the client submits the form to the server the 
hidden fields identify the client.&lt;/span&gt;&lt;/li&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;strong&gt;Cookies : &lt;/strong&gt;Cookie is a small amount of information 
sent by a servlet to a Web browser. Saved by the browser, and later sent
 back to the server in subsequent requests. A cookie has a name, a 
single value, and optional attributes. A cookie&#39;s value can uniquely 
identify a client.&lt;/span&gt;&lt;/li&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;strong&gt;Secure Socket Layer (SSL) Sessions :&lt;/strong&gt; Web browsers 
that support Secure Socket Layer communication can use SSL&#39;s support via
 HTTPS for generating a unique session key as part of the encrypted 
conversation.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Q.10)What is the difference between using getSession(true) and getSession(false) methods?&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;getSession(true): &lt;/b&gt;This method will check whether there is already a session exists for the user. If a session exists, it returns that session object. If a session does not already exist then it creates a new session for the user.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;getSession(false):&lt;/b&gt; This method will check whether there is already a session exists for the user. If a session exists, it returns that session object. If a session does not already exist then it returns null.retrieve stored state information for the supplied JSESSIONID.Servlet Sessions can be timed out (configured in web.xml) or manually invalidated.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Q.11)How can you set a cookie and delete a cookie from within a Servlet?&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;u&gt;&lt;span style=&quot;font-size: small;&quot;&gt;To add a cookie&lt;/span&gt;&lt;/u&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;Cookie myCookie = new Cookie(“aName”, “aValue”);&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;response.addCookie(myCookie);&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;u&gt;&lt;span style=&quot;font-size: small;&quot;&gt;To delete a cookie&lt;/span&gt;&lt;/u&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;myCookie.setValue(“aName”, null);&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;myCookie.setMax(0);&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;myCookie.setPath(“/”);&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;response.addCookie(myCookie);&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Q.12) Explain the life cycle methods of a servlet?&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;These three method going to be call in servlet life cycle:-&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;1)public void init(ServletConfig config) throws ServletException&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;2)public void service(ServletRequest req,ServletResponse res) throws&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; ServletException,IOException&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;3)public void destroy()&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;color: navy; font-family: Verdana,sans-serif; font-size: small;&quot;&gt;&lt;/span&gt;&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;que&quot;&gt;&lt;span class=&quot;queIndex&quot;&gt;Q.13)&lt;/span&gt;What are the phases of the servlet life cycle?&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp; &amp;nbsp; The life cycle of a servlet consists of the following phases:&lt;/span&gt;&lt;/div&gt;
&lt;ul style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;strong&gt;Servlet class loading&lt;/strong&gt; : For each servlet defined 
in the deployment descriptor of the Web application, the servlet 
container locates and loads a class of the type of the servlet. This can
 happen when the servlet engine itself is started, or later when a 
client request is actually delegated to the servlet.&lt;/span&gt;&lt;/li&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;strong&gt;Servlet instantiation&lt;/strong&gt; : After loading, it instantiates one or more object instances of the servlet class to service the client requests.&lt;/span&gt;&lt;/li&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;strong&gt;Initialization (call the init method)&lt;/strong&gt; : After 
instantiation, the container initializes a servlet before it is ready to
 handle client requests. The container initializes the servlet by 
invoking its init() method, passing an object implementing the 
ServletConfig interface. In the init() method, the servlet can read 
configuration parameters from the deployment descriptor or perform any 
other one-time activities, so the init() method is invoked once and only
 once by the servlet container. &lt;/span&gt;&lt;/li&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;strong&gt;Request handling (call the service method)&lt;/strong&gt; : 
After the servlet is initialized, the container may keep it ready for 
handling client requests. When client requests arrive, they are 
delegated to the servlet through the service() method, passing the 
request and response objects as parameters. In the case of HTTP 
requests, the request and response objects are implementations of 
HttpServletRequest and HttpServletResponse respectively. In the 
HttpServlet class, the service() method invokes a different handler 
method for each type of HTTP request, doGet() method for GET requests, 
doPost() method for POST requests, and so on.&lt;/span&gt;&lt;/li&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;strong&gt;Removal from service (call the destroy method)&lt;/strong&gt; : A
 servlet container may decide to remove a servlet from service for 
various reasons, such as to conserve memory resources. To do this, the 
servlet container calls the destroy() method on the servlet. Once the 
destroy() method has been called, the servlet may not service any more 
client requests. Now the servlet instance is eligible for garbage 
collection&lt;/span&gt;&lt;/li&gt;
&lt;div align=&quot;justify&quot; class=&quot;content&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;The life cycle of a servlet is controlled by the container in which the servlet has been deployed.&lt;/span&gt;&lt;/div&gt;
&lt;/ul&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Q.14)Explain the directory structure of a Web application?&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;The directory structure of a Web application consists of two parts:&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;A public resource directory(document root):&lt;/b&gt;The document root is where JSP pages,client-side classes and archives, and static Web resources are stored.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;A private directory called WEB-INF:&lt;/b&gt;which contains following files and directories:&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;u&gt;web.xml:&lt;/u&gt;Web application deployment descriptor. application server specific deployment descriptor e.g.jboss-web.xml etc.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;u&gt;*.tld:&lt;/u&gt;Tag library descriptor files.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;u&gt;classes:&lt;/u&gt;A directory that contains serverside classes like servlets,utility classes,JavaBeans etc.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;u&gt;lib:&lt;/u&gt;A directory where JAR(archive files of tag libraries,utility libraries used by the server side classes)&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;files are stored.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Q.15)What is the difference between doGet () and doPost () or GET and POST?&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;GET or doGet()&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;a)The request parameters are transmitted as a query string appended to the request. All the parameters get appended to the URL in the address bar. Allows browser bookmarks but not appropriate for transmitting private or sensitive information.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;b)GET was originally intended for static resource retrieval.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;c)GET is not appropriate when large amounts of input data are being transferred. Limited to 1024 characters&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;POST or doPost()&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;a)The request parameters are passed with the body of the request.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;b)POST was intended for form submits where the state of the model and database are expected to change.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;c)Since it sends information through a socket back to the server and it won’t show up in the URL address bar, it can send much more information to the server. Unlike doGet(), it is not restricted to sending only textual data. It can also send binary data such as serialized Java objects. &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Q.16)What are the ServletContext and ServletConfig objects?&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;ServletConfig&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;The ServletConfig parameters are for a particular Servlet. The parameters are specified in the web.xml . It is created after a servlet is instantiated and it is used to pass initialization information to the servlet. &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;ServletContext :&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;The ServletContext parameters are specified for the entire Web application. The parameters are specified in the web.xml. Servlet context is common to all Servlets. So all Servlets share information through ServletContext. &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Q.17) What is the difference between HttpServlet and GenericServlet?&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;Both these classes are abstract but:&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;GenericServlet:&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;a)A GenericServlet has a service() method to handle requests. &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;b)Protocol independent. GenericServlet is for servlets that might not use HTTP (for example FTP service).&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;HttpServlet:&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;a)The HttpServlet extends GenericServlet and adds support for HTTP protocol based methods like doGet(), doPost(), doHead() etc.HttpServlet also has methods like doHead(), doPut(), doOptions(), doDelete(), and doTrace().&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;b)Protocol dependent (i.e. HTTP).&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Q.18) How do you make a Servlet thread safe?&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;The shared resources should be appropriately synchronized or should only use variables in a read-only manner. There are situations where synchronizing will not give you the expected results&amp;nbsp; and to achieve the expected results you should store your values in a user session or store them as a hidden field values. Having large chunks of code in synchronized blocks in your service or doPost() methods can adversely affect performance and makes the code more complex.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;Alternatively it is possible to have a single threaded model of a servlet by implementing the marker or null interface javax.servlet.SingleThreadedModel.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Q.19)How do you get your servlet to stop timing out on a really long database query? &lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;Client-pull or client-refresh: You can use the &amp;lt;META&amp;gt; tag for polling the server. This tag tells the client it must refresh the page after a number of seconds.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&amp;lt;META http-equiv=”Refresh” content=”10; url=”newPage.html” /&amp;gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Q.20)What is lazy loading of a Servlet?&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;By default the container does not initialize the servlets as soon as it starts up. It initializes a servlet when it receives a request for the first time for that servlet. This is called lazy loading.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Q.21)What is pre-initialization of a Servlet?&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;The servlet deployment descriptor (web.xml) defines the 
&amp;lt;load-on-startup&amp;gt; element, which can be configured to make the 
servlet container load and initialize the servlet as soon as it starts 
up. The process of loading a servlet before any request comes in is 
called pre-loading or pre-initializing a servlet. We can also specify 
the order in which the servlets are initialized.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&amp;lt;load-on-startup&amp;gt;2&amp;lt;/load-on-startup&amp;gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Q.22)What is a filter, and how does it work?&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;A filter dynamically intercepts requests and responses to transform or use the information contained in the requests or responses but typically do not themselves create responses. Filters can also be used to transform the response from the Servlet or JSP before sending it back to client. Filters improve reusability by placing recurring tasks in the filter as a reusable unit.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Q.23)What do you understand by servlet
mapping?&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Servlet mapping defines an
association between a URL pattern and a servlet. You can use one servlet to
process a number of url pattern (request pattern). For example in case of Struts
*.do url patterns are processed by Struts Controller Servlet.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;que&quot;&gt; 
&lt;span class=&quot;queIndex&quot;&gt;Q.24)&lt;/span&gt;Why do we need a constructor in a servlet if we use the init method?&lt;/span&gt;&lt;/span&gt;
&lt;/b&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;Even though there is an init method 
in a servlet which gets called to initialize it, a constructor is still 
required to instantiate the servlet. Even though you as the developer 
would never need to explicitly call the servlet&#39;s constructor, it is 
still being used by the container (the container still uses the 
constructor to create an instance of the servlet). Just like a normal 
POJO (plain old java object) that might have an init method, it is no 
use calling the init method if you haven&#39;t constructed an object to call
 it on yet.&lt;/span&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;que&quot;&gt; 
&lt;span class=&quot;queIndex&quot;&gt;Q.25)&lt;/span&gt;Can servlet have a constructor ?&lt;/span&gt;&lt;/span&gt;
&lt;/b&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;One can definitely have constructor 
in servlet.Even you can use the constrctor in servlet for initialization
 purpose,but this type of approch is not so common. You can perform 
common operations with the constructor as you normally do.The only thing
 is that you cannot call that constructor explicitly by the new keyword 
as we normally do.In the case of servlet, servlet container is 
responsible for instantiating the servlet, so the constructor is also 
called by servlet container only.&lt;/span&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;que&quot;&gt;&lt;span class=&quot;queIndex&quot;&gt;&lt;/span&gt;Q.26)Should I override the service() method?&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;

&lt;/div&gt;
&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;We never override the service method, since the HTTP Servlets have already taken care of it &lt;code&gt;&lt;/code&gt;.
 The default service function invokes the doXXX() method corresponding 
to the method of the HTTP request.For example, if the HTTP request 
method is GET, doGet() method is called by default. A servlet should 
override the doXXX() method for the HTTP methods that servlet supports. 
Because HTTP service method check the request method and calls the 
appropriate handler method, it is not necessary to override the service 
method itself. Only override the appropriate doXXX() method.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;color: black; font-family: Verdana,sans-serif; font-size: small;&quot;&gt;&lt;br /&gt;&lt;b&gt;&lt;span class=&quot;que&quot;&gt; 
&lt;span class=&quot;queIndex&quot;&gt;Q.27)&lt;/span&gt;Should I override the service() method?&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;b style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;
    
 
  

&lt;/span&gt;&lt;/b&gt;

&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;We never override the service method, since the HTTP Servlets have already taken care of it &lt;code&gt;&lt;/code&gt;.
 The default service function invokes the doXXX() method corresponding 
to the method of the HTTP request.For example, if the HTTP request 
method is GET, doGet() method is called by default. A servlet should 
override the doXXX() method for the HTTP methods that servlet supports. 
Because HTTP service method check the request method and calls the 
appropriate handler method, it is not necessary to override the service 
method itself. Only override the appropriate doXXX() method. &lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: black; font-family: Verdana,sans-serif; font-size: small;&quot;&gt;&lt;span class=&quot;que&quot;&gt;&lt;span class=&quot;queIndex&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black; font-family: Verdana,sans-serif; font-size: small;&quot;&gt;&lt;br /&gt;&lt;b&gt;&lt;span class=&quot;que&quot;&gt;&lt;span class=&quot;queIndex&quot;&gt;Q.28)&lt;/span&gt;What is a servlet context object?&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;b style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;

&lt;/span&gt;&lt;/b&gt;
&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;A servlet context object contains the
 information about the Web application of which the servlet is a part. 
It also provides access to the resources common to all the servlets in 
the application. Each Web application in a container has a single 
servlet context associated with it.&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;color: black; font-family: Verdana,sans-serif; font-size: small;&quot;&gt;&lt;br /&gt;&lt;b&gt;&lt;span class=&quot;que&quot;&gt;&lt;span class=&quot;queIndex&quot;&gt;Q.29)&lt;/span&gt;What are the differences between the ServletConfig interface and the ServletContext interface?&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;span style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;

&lt;/span&gt;
&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;

&lt;/div&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;5&quot; cellspacing=&quot;5&quot; class=&quot;mytable&quot;&gt;
  &lt;tbody&gt;
&lt;tr&gt;
    &lt;th width=&quot;50%&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;ServletConfig&lt;/span&gt;&lt;/th&gt;
     &lt;th width=&quot;50%&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;ServletContext&lt;/span&gt;&lt;/th&gt;
  &lt;/tr&gt;
&lt;tr class=&quot;odd&quot;&gt;
    &lt;td&gt;&lt;span style=&quot;font-size: small;&quot;&gt;The ServletConfig interface is implemented by  the servlet 
container in order to pass configuration information to a servlet.  The 
server passes an object that implements the ServletConfig interface to 
the  servlet&#39;s init() method. &lt;/span&gt;&lt;/td&gt;
    &lt;td&gt;&lt;span style=&quot;font-size: small;&quot;&gt;A ServletContext defines a set of methods that a  servlet uses to communicate with its servlet container.&lt;/span&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr class=&quot;even&quot;&gt;
    &lt;td&gt;&lt;span style=&quot;font-size: small;&quot;&gt;There is one ServletConfig parameter per  servlet. &lt;/span&gt;&lt;/td&gt;
    &lt;td&gt;&lt;span style=&quot;font-size: small;&quot;&gt;There is one ServletContext for the entire  webapp and all the servlets in a webapp share it.&lt;/span&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr class=&quot;odd&quot;&gt;
    &lt;td&gt;&lt;span style=&quot;font-size: small;&quot;&gt;The param-value pairs for ServletConfig object  are specified in
 the &amp;lt;init-param&amp;gt; within the &amp;lt;servlet&amp;gt; tags in the  web.xml 
file&lt;/span&gt;&lt;/td&gt;
    &lt;td&gt;&lt;span style=&quot;font-size: small;&quot;&gt;The param-value pairs for ServletContext object  are specified in the &amp;lt;context-param&amp;gt; tags in the web.xml file.&lt;/span&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;span style=&quot;color: black; font-family: Verdana,sans-serif; font-size: small;&quot;&gt;&lt;br /&gt;&lt;b&gt;&lt;span class=&quot;que&quot;&gt;&lt;span class=&quot;queIndex&quot;&gt;Q.30)&lt;/span&gt;What&#39;s the difference between forward() and sendRedirect() methods?&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;b style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;

&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: black; font-family: Verdana,sans-serif; font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;/span&gt;&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;

&lt;/div&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;5&quot; cellspacing=&quot;5&quot; class=&quot;mytable&quot;&gt;
  &lt;tbody&gt;
&lt;tr&gt;
    &lt;th width=&quot;50%&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;forward()&lt;/span&gt;&lt;/th&gt;
     &lt;th width=&quot;50%&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;sendRedirect()&lt;/span&gt;&lt;/th&gt;
  &lt;/tr&gt;
&lt;tr class=&quot;odd&quot;&gt;
    &lt;td&gt;&lt;span style=&quot;font-size: small;&quot;&gt;A forward is performed internally by the servlet.&lt;/span&gt;&lt;/td&gt;
    &lt;td&gt;&lt;span style=&quot;font-size: small;&quot;&gt;A redirect is a two step process, where the web  application 
instructs the browser to fetch a second URL, which differs from the  
original.&lt;/span&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr class=&quot;even&quot;&gt;
    &lt;td&gt;&lt;span style=&quot;font-size: small;&quot;&gt;The &amp;nbsp;browser is  completely unaware that it has taken place, so its original URL remains intact.&lt;/span&gt;&lt;/td&gt;
    &lt;td&gt;&lt;span style=&quot;font-size: small;&quot;&gt;The browser, in this case, is doing the work and knows  that it&#39;s making a new request.&lt;/span&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr class=&quot;odd&quot;&gt;
    &lt;td&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Any browser reload of the resulting page will simple  repeat the original request, with the original URL&lt;/span&gt;&lt;/td&gt;
    &lt;td&gt;&lt;span style=&quot;font-size: small;&quot;&gt;A browser reloads of the second URL ,will not repeat  the original request, but will rather fetch the second URL.&lt;/span&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr class=&quot;even&quot;&gt;
     &lt;td&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Both resources must be part of the same context (Some  
containers make provisions for cross-context communication but this 
tends not  to be very portable)&lt;/span&gt;&lt;/td&gt;
     &lt;td&gt;&lt;span style=&quot;font-size: small;&quot;&gt;This method can be used to redirect users to resources  that are not part of the current context, or even in the same domain.&lt;/span&gt;&lt;/td&gt;
   &lt;/tr&gt;
&lt;tr class=&quot;odd&quot;&gt;
     &lt;td&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Since both resources are part of same context, the original  request context is retained&lt;/span&gt;&lt;/td&gt;
     &lt;td&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Because this involves a new request, the previous  request 
scope objects, with all of its parameters and attributes are no longer  
available after a redirect.&lt;br /&gt;
      (Variables  will need to be passed by via the session object).&lt;/span&gt;&lt;/td&gt;
   &lt;/tr&gt;
&lt;tr class=&quot;even&quot;&gt;
     &lt;td&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Forward is marginally faster than redirect.&lt;/span&gt;&lt;/td&gt;
     &lt;td&gt;&lt;span style=&quot;font-size: small;&quot;&gt;redirect is marginally slower than a forward, since it  requires two browser requests, not one.&lt;/span&gt;&lt;/td&gt;
   &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;que&quot;&gt;&lt;span class=&quot;queIndex&quot;&gt;Q.31)&lt;/span&gt;What is the difference between the include() and forward() methods?&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;
&lt;/div&gt;
&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;/div&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;5&quot; cellspacing=&quot;5&quot; class=&quot;mytable&quot;&gt;
  &lt;tbody&gt;
&lt;tr&gt;
    &lt;th width=&quot;50%&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;include()&lt;/span&gt;&lt;/th&gt;
     &lt;th width=&quot;50%&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;forward()&lt;/span&gt;&lt;/th&gt;
  &lt;/tr&gt;
&lt;tr class=&quot;odd&quot;&gt;
    &lt;td&gt;&lt;span style=&quot;font-size: small;&quot;&gt;The &lt;code&gt;RequestDispatcher include()&lt;/code&gt; method inserts the 
the contents of the specified  resource directly in the flow of the 
servlet response, as if it were part of  the calling servlet.&lt;/span&gt;&lt;/td&gt;
    &lt;td&gt;&lt;span style=&quot;font-size: small;&quot;&gt;The &lt;code&gt;RequestDispatcher forward()&lt;/code&gt; method is used to show a different resource in place  of the servlet that was originally called. &lt;/span&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr class=&quot;even&quot;&gt;
    &lt;td&gt;&lt;span style=&quot;font-size: small;&quot;&gt;If you include a servlet or JSP document, the  included resource
 must not attempt to change the response status code or HTTP  headers, 
any such request will be ignored. &lt;/span&gt;&lt;/td&gt;
    &lt;td&gt;&lt;span style=&quot;font-size: small;&quot;&gt;The forwarded resource may be another servlet,  JSP or static 
HTML document, but the response is issued under the same URL that  was 
originally requested. In other words, it is not the same as a 
redirection.&lt;/span&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr class=&quot;odd&quot;&gt;
    &lt;td&gt;&lt;span style=&quot;font-size: small;&quot;&gt;The &lt;code&gt;include()&lt;/code&gt; method is often used to include common  &quot;boilerplate&quot; text or template markup that may be included by many  servlets. &lt;/span&gt;&lt;/td&gt;
    &lt;td&gt;&lt;span style=&quot;font-size: small;&quot;&gt;The &lt;code&gt;forward()&lt;/code&gt; method is often used where a servlet 
is taking a  controller role; processing some input and deciding the 
outcome by returning a  particular response page. &lt;/span&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;

&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;que&quot;&gt;&lt;span class=&quot;queIndex&quot;&gt;Q.32)&lt;/span&gt;What&#39;s  the use of the servlet wrapper classes??&lt;/span&gt;&lt;/span&gt;&lt;/b&gt; &lt;/div&gt;
&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;The &lt;code&gt;HttpServletRequestWrapper&lt;/code&gt; and &lt;code&gt;HttpServletResponseWrapper&lt;/code&gt;
 classes are designed to make it easy for developers  to create custom 
implementations of the servlet request and response types. The  classes 
are constructed with the standard &lt;code&gt;HttpServletRequest&lt;/code&gt;  and &lt;code&gt;HttpServletResponse&lt;/code&gt; instances respectively and their default behaviour is  to pass all method calls directly to the underlying objects.&lt;/span&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;que&quot;&gt; 
&lt;span class=&quot;queIndex&quot;&gt;Q.33)&lt;/span&gt;What is Session Tracking?&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;
&lt;/div&gt;
&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;Session tracking is a mechanism that 
servlets use to maintain state about a series of requests from the same 
user (that is, requests originating from the same browser) across some 
period of time. &lt;/span&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;b style=&quot;color: black;&quot;&gt;&lt;span style=&quot;color: black; font-family: Verdana,sans-serif; font-size: small;&quot;&gt;&lt;span class=&quot;que&quot;&gt; 
&lt;span class=&quot;queIndex&quot;&gt;Q.34)&lt;/span&gt;How can an existing session be invalidated?&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;
&lt;/span&gt;&lt;/b&gt;&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;An existing session can be invalidated in the following two ways:&lt;/span&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;u&gt;Setting timeout in the deployment descriptor:&lt;/u&gt; This can be done by specifying timeout between the &lt;code&gt;&amp;lt;session-timeout&amp;gt;&lt;/code&gt;tags as follows:&lt;/span&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt; &amp;lt;session-config&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;mid&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;&lt;strong&gt;session-timeout&amp;gt;10&amp;lt;/session-timeout&amp;gt;&lt;/strong&gt;
&amp;lt;/session-config&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class=&quot;mid&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;This will set the time for session timeout to be ten minutes.&lt;/span&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;u&gt;Setting timeout programmatically: &lt;/u&gt;This will set  the timeout for a
 specific session. The syntax for setting the timeout  programmatically 
is as follows:&lt;/span&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;public void setMaxInactiveInterval(int interval)&lt;/span&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;The seMaxInactiveInterval() method sets the maximum time in seconds before a session becomes invalid.&lt;/span&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;que&quot;&gt;Q.35)What is Servlet Chaining?&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;
    &lt;/div&gt;
&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;Servlet Chaining is a method 
where the output of one  servlet is piped into a second servlet. The 
output of the second servlet could  be piped into a third servlet, and 
so on. The last servlet in the chain returns  the output to the Web 
browser.&lt;/span&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;que&quot;&gt;&lt;span class=&quot;queIndex&quot;&gt;Q.36)&lt;/span&gt;What are the functions of the Servlet container?&lt;/span&gt;
    &lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; class=&quot;content&quot; style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;The functions of the Servlet container are as follows:&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;color: black; font-family: Verdana,sans-serif; font-size: small;&quot;&gt;
    &lt;/span&gt;&lt;ul style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;strong&gt;Lifecycle management&lt;/strong&gt; : It       manages the 
life and death of a servlet, such as class loading,       instantiation,
 initialization, service, and making servlet instances       eligible 
for garbage collection. &lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;strong&gt;Communication support&lt;/strong&gt; : It       handles the communication between the servlet and the Web server. &lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;strong&gt;Multithreading support&lt;/strong&gt; : It       
automatically creates a new thread for every servlet request received.  
     When the Servlet service() method completes, the thread dies. &lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;strong&gt;Declarative security&lt;/strong&gt; : It       manages the security inside the XML deployment descriptor file. &lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;strong&gt;JSP support&lt;/strong&gt; : The container       is responsible for converting JSPs to servlets and for maintaining them. &lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;span style=&quot;color: black; font-family: Verdana,sans-serif; font-size: small;&quot;&gt;
  
  
  &lt;/span&gt; &lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://jksolutions-corejava.blogspot.com/feeds/1300103879453795167/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://jksolutions-corejava.blogspot.com/2011/10/servlet-interview-question-and-answers.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/299468836146488031/posts/default/1300103879453795167'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/299468836146488031/posts/default/1300103879453795167'/><link rel='alternate' type='text/html' href='http://jksolutions-corejava.blogspot.com/2011/10/servlet-interview-question-and-answers.html' title='Servlet Interview Question and Answers'/><author><name>jksolutions</name><uri>http://www.blogger.com/profile/01556365883008986930</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-299468836146488031.post-6154944937409983311</id><published>2011-09-29T23:11:00.000-04:00</published><updated>2011-09-29T23:11:44.687-04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="action element"/><category scheme="http://www.blogger.com/atom/ns#" term="cache control"/><category scheme="http://www.blogger.com/atom/ns#" term="declaration element"/><category scheme="http://www.blogger.com/atom/ns#" term="errorPage"/><category scheme="http://www.blogger.com/atom/ns#" term="expression"/><category scheme="http://www.blogger.com/atom/ns#" term="implicit objects"/><category scheme="http://www.blogger.com/atom/ns#" term="isError"/><category scheme="http://www.blogger.com/atom/ns#" term="JSP lifecycle"/><category scheme="http://www.blogger.com/atom/ns#" term="JSP runtime exception"/><category scheme="http://www.blogger.com/atom/ns#" term="scope"/><category scheme="http://www.blogger.com/atom/ns#" term="scriplet"/><category scheme="http://www.blogger.com/atom/ns#" term="sendRedirect"/><category scheme="http://www.blogger.com/atom/ns#" term="static and dynamic include"/><title type='text'>JSP All in one(JSP interview Questions and ans)</title><content type='html'>&lt;b&gt;Q.1)What is JSP?&lt;/b&gt;&lt;br /&gt;
Java Server Pages (JSP) is a platform independent presentation layer 
technology.JSPs are normal HTML 
pages with Java code pieces embedded in them. A JSP compiler is used in the background to generate a 
Servlet from the JSP page.&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Q.2)&amp;nbsp; Explain the life cycle methods of a JSP?&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;A)&lt;/b&gt;&lt;b&gt;Pre-translated:&lt;/b&gt;Before the JSP file has been translated and compiled into the Servlet. &lt;b&gt;B)Translated:&lt;/b&gt;The JSP file has been translated and compiled as a Servlet.&lt;br /&gt;
&lt;b&gt;C)&lt;/b&gt;&lt;b&gt;Initialized:&lt;/b&gt;Prior to handling there quests in the service method the container calls the jsp Init() to initialize the Servlet. Called only once per Servlet instance.&lt;br /&gt;
&lt;b&gt;D)&lt;/b&gt;&lt;b&gt;Servicing:&lt;/b&gt; Services the client requests. Container calls the _jspService() method for each request. &lt;b&gt;E)Outofservice:&lt;/b&gt;The Servlet instance is out of service.The container calls the jspDestroy() method.&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Q.3)What are the life-cycle methods of JSP? &lt;/b&gt;&lt;br /&gt;
Life-cycle methods of the JSP are:&lt;br /&gt;
&lt;b&gt;a) jspInit(): &lt;/b&gt;The container calls the jspInit() to initialize the servlet instance. It is called before any other method, and is called only once for a servlet instance. &lt;br /&gt;
&lt;b&gt;b)_jspService():&lt;/b&gt; The container calls the _jspservice() for each request and it passes the request and the response objects. _jspService() method cann&#39;t be overridden.&lt;br /&gt;
&lt;b&gt;c) jspDestroy(): &lt;/b&gt;The container calls this when its instance is about to destroyed. &lt;br /&gt;
&lt;br /&gt;
The jspInit() and jspDestroy() methods can be overridden within a JSP page. &lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Q.4)What are the main elements of JSP?&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
There are two types of data in a JSP page.&lt;br /&gt;
&lt;b&gt; Static part&lt;/b&gt;(i.e.HTML,CSSetc),which gets copied directly to the response by the JSP Engine.&lt;br /&gt;
&lt;b&gt;Dynamic part&lt;/b&gt;,which contains anything that can be translated and compiled by the JSPEngine. &lt;br /&gt;
&lt;br /&gt;
There are three types of dynamic elements.&lt;br /&gt;
A)Scripting Elements:&lt;br /&gt;
B)Action Elements:&lt;br /&gt;
C)Directive Elements:&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;A)Scripting Elements:&lt;/b&gt; A JSP element that provides embedded Java statements. There are three types of scripting elements.&lt;br /&gt;
&lt;br /&gt;
&lt;u&gt;1)Declaration Element: &lt;/u&gt;is the embedded Java declaration statement, which gets inserted at the Servlet class level.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;%! int a =10 ;%&amp;gt; &lt;br /&gt;
&lt;u&gt;2)Expression Element:&lt;/u&gt; is the embedded Java expression, which gets evaluated by the service method.&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;%= new date()%&amp;gt; &lt;br /&gt;
&lt;u&gt;3)Scriptlet Element: &lt;/u&gt;are the embedded Java statements, which get executed as part of the service method. &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;%&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; String username = null;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; username = request.getParameter(&quot;userName&quot;); &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;B)Action Elements: &lt;/b&gt;A JSP element that provides information for execution phase.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;jsp:useBean id=&quot;object_name&quot; class=&quot;class_name&quot;/&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;jsp:include page=&quot;scripts/login.jsp&quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;C)Directive Elements: &lt;/b&gt;A JSP element that provides global information for the translation phase. There are three types of directive elements. &lt;br /&gt;
&lt;br /&gt;
1)page : &amp;lt;%@ page import=”java.util.Date” %&amp;gt;&lt;br /&gt;
&lt;br /&gt;
2)include:&amp;nbsp; &amp;lt;%@ include file=”myJSP” %&amp;gt;&lt;br /&gt;
3)taglib:&amp;nbsp; &amp;lt;%@ taglib uri=”tagliburi” prefix=”myTag”%&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Q.5) Can you declare a method within your JSP page?&lt;/b&gt;&lt;br /&gt;
You can declare methods within your JSP pages as declarations, and your methods can be invoked from within your other methods you declare, expression elements or scriptlets. These declared methods do not have direct access to the JSP implicit objects like session, request,&lt;br /&gt;
response etc but you can pass them to your methods you declare as parameters. &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;%!&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public String myJspMethod(&lt;b&gt;HttpSession session&lt;/b&gt;) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; String str = (String)session.getAttribute(&quot;anyAttributeName&quot;);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return str.substring(0,10);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; %&amp;gt; &lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Q.6)How will you perform a browser redirection from a JSP page?&lt;/b&gt;&lt;br /&gt;
&amp;lt;% response.sendRedirect(“http://www.someAbsoluteAddess.com”); %&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or you can alter the location HTTP header attribute as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%&lt;br /&gt;
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);&lt;br /&gt;
response.setHeader(“Location”, “/someNewPath/index.html”);&lt;br /&gt;
%&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Q.7)How do you prevent the HTML output of your JSP page being cached?&lt;/b&gt;&lt;br /&gt;
&amp;lt;%&lt;br /&gt;
response.setHeader(“Cache-Control”, “no=store”); //HTTP 1.1&lt;br /&gt;
response.setHeader(&quot;Pragma\&quot;,&quot;no-cache&quot;); //HTTP 1.0&lt;br /&gt;
response.setDateHeader(“Expires”, 0);&lt;br /&gt;
%&amp;gt; &lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Q.8)How do you forward a request to another resource (e.g. another Servlet) from within your JSP?&lt;/b&gt;&lt;br /&gt;
&amp;lt;jsp:forward page=”/newPage.jsp” /&amp;gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Q.9)How does JSP handle run-time exceptions?&lt;/b&gt;&lt;br /&gt;
You can use the attribute “errorPage” of the “page” directive to have your uncaught&amp;nbsp; RuntimeExceptions automatically forwarded to an error processing page.&lt;br /&gt;
&amp;lt;%@ page errorPage=&quot;error.jsp&quot; %&amp;gt; &lt;br /&gt;
The above code redirects the browser client to the error.jsp page. Within your error.jsp page, you need to indicate that it is an error processing page with the “isErrorPage” attribute of the “page” directive as shown below.“exception” is an implicit object accessible only within error pages (i.e. pages with directive &amp;lt;%@ page&lt;br /&gt;
isErrorPage=”true” %&amp;gt;&lt;br /&gt;
&amp;lt;%@ page isErrorPage=”true” %&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;%= exception.gerMessage() %&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Q.10)How will you specify a global error page as opposed to using “errorPage” and “isErrorPage” attributes?&lt;/b&gt;&lt;br /&gt;
You could specify your error page in the web.xml deployment descriptor as shown below:&lt;br /&gt;
By error type:- &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;error-page&amp;gt; &amp;lt;exception-type&amp;gt;java.lang.Throwable&amp;lt;/exception-type&amp;gt;&amp;nbsp;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;location&amp;gt;/error.jsp&amp;lt;/location&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/error-page&amp;gt;&lt;br /&gt;
By error code:-&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;error-page&amp;gt; &amp;lt;error-code&amp;gt;404&amp;lt;/error-code&amp;gt;&amp;nbsp; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;location&amp;gt;/error404.html&amp;lt;/location&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/error-page&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Q.11)Q. How can you prevent the automatic creation of a session in a JSP page?&lt;/b&gt;&lt;br /&gt;
Sessions consume resources and if it is not necessary, it should not be created. By default, a JSP page will automatically create a session for the request if one does not exist. You can prevent the creation of useless sessions with the attribute “session” of the page directive.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;%@ page session=”false” %&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Q.12)What are the different scope values ?&lt;/b&gt;&lt;br /&gt;
1)page&lt;br /&gt;
2)request&lt;br /&gt;
3)session&lt;br /&gt;
4)application&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Q.13)What are the differences between static and a dynamic include?&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Static include&lt;/b&gt; &amp;lt;%@ include %&amp;gt; :-During the translation or compilation phase all the included JSP pages are compiled into a single Servlet.No run time performance overhead. &lt;br /&gt;
&lt;b&gt;Dynamic include&lt;/b&gt; &amp;lt;jsp:include .....&amp;gt;:-The dynamically included JSP is compiled into a separate Servlet. It is a separate resource, which gets to process the request, and the content generated by this resource is included in the JSP response.Has run time performance overhead.&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Q.14)What are implicit objects and list them?&lt;/b&gt;&lt;br /&gt;
request&lt;br /&gt;
response&lt;br /&gt;
pageContext&lt;br /&gt;
session&lt;br /&gt;
application&lt;br /&gt;
out&lt;br /&gt;
config&lt;br /&gt;
page&lt;br /&gt;
exception &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Q.15)Explain hidden and output comments?&lt;/b&gt;&lt;br /&gt;
An &lt;b&gt;output comment&lt;/b&gt; is a comment that is sent to the client where it is viewable in the browser’s source.&lt;br /&gt;
&amp;lt;!-- This is a comment which is sent to the client --&amp;gt;&lt;br /&gt;
A &lt;b&gt;hidden comment&lt;/b&gt; documents a JSP page but does not get sent to the client. The JSP engine ignores a hidden comment, and does not process any code within hidden comment tags.&lt;br /&gt;
&amp;lt;%-- This comment will not be visible to the client --%&amp;gt; &lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Q.16)Is JSP variable declaration thread safe?&lt;/b&gt;&lt;br /&gt;
No. The declaration of variables in JSP is not thread-safe, because the declared variables end up in the generated Servlet as an instance variable, not within the body of the _jspService() method.&lt;br /&gt;
&lt;br /&gt;
The following declaration is not thread safe: because these declarations end up in the generated servlet as instance variables.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;%! int a = 5 %&amp;gt; &lt;br /&gt;
The following declaration is thread safe: because the variables declared inside the scriplets end up in the generated servlet within the body of the _jspService() method as local variables.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;% int a = 5 %&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Q.17)What is the difference between custom JSP tags and JavaBeans?&lt;/b&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;Custom Tags &lt;/b&gt;:-&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; 1 )Can manipulate JSP content&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; 2)Custom tags can simplify the complex operations much better than the bean can. But require a&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; bit more work to set up.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; 3)Used only in JSPs in a relatively self-contained manner.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;JavaBeans:-&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp;&lt;/b&gt;&amp;nbsp;&amp;nbsp; 1)Can’t manipulate JSP content.&lt;br /&gt;
&amp;nbsp; &amp;nbsp; 2)Easier to set up.&lt;br /&gt;
&amp;nbsp; &amp;nbsp; 3)Can be used in both Servlets and JSPs. You can define a bean in one Servlet and use them in&amp;nbsp;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; another Servlet or a JSP page. &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Q.18)How will you avoid scriptlet code in JSP?&lt;/b&gt;&lt;br /&gt;
Use JavaBeans or custom tags instead.&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Q:19)Difference between forward and sendRedirect?&lt;/b&gt;&lt;br /&gt;
When you invoke a forward request, the request is sent to another resource on the server, without the client being informed that a different resource is going to process the request. This process occurs completly with in the web container. When a sendRedirtect method is invoked, it causes the web container to return to the browser indicating that a new URL should be requested. Because the browser issues a completly new request any object that are stored as request attributes before the redirect occurs will be lost. This extra round trip a redirect is slower than forward. &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Q.20) Generally you would be invoking a JSP page from a Servlet. Why would you want to invoke a Servlet from a JSP?&lt;/b&gt;&lt;br /&gt;
JSP technology is intended to simplify the programming of dynamic textual content. If you want to output any binary data (e.g. pdfs, gifs etc) then JSP pages are poor choice for the following reasons and should use Servlets instead:&lt;br /&gt;
1)There are no methods for writing raw bytes in the JspWriter object.&lt;br /&gt;
2)During execution, the JSP engine preserves whitespace. Whitespace is sometimes unwanted (a .gif file).&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Q.21)How can I enable session tracking for JSP pages if the browser has disabled cookies?&lt;/b&gt;&lt;br /&gt;
using URL rewriting&lt;br /&gt;
hidden form field &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Q.22) What is the difference between &amp;lt;jsp:forward page = ... &amp;gt; and  response.sendRedirect(url)?&lt;/b&gt;&lt;br /&gt;
The &amp;lt;jsp:forward&amp;gt; element forwards the request object containing the client request information from one JSP file to another file. The target file can be an HTML file, another JSP file, or a servlet, as long as it is in the same application context as the forwarding JSP file.&amp;nbsp; sendRedirect sends HTTP temporary redirect response to the browser, and browser creates a new request to go the redirected page. The&amp;nbsp; response.sendRedirect kills the session variables. &lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Q.23) Identify the advantages of JSP over Servlet.  &lt;/b&gt;&lt;br /&gt;
a) Embedding of Java code in HTML pages&lt;br /&gt;
b) Platform independence&lt;br /&gt;
c) Creation of database-driven Web applications&lt;br /&gt;
d) Server-side programming capabilities&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Q.24)How can I set a cookie and delete a cookie from within a JSP page?&lt;/b&gt;&lt;br /&gt;
 A cookie, mycookie, can be deleted using the following scriptlet: &lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;lt;%&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //creating a cookie&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; Cookie mycookie = new Cookie(&quot;aName&quot;,&quot;aValue&quot;);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; response.addCookie(mycookie);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //delete a cookie&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Cookie killMyCookie = new Cookie(&quot;mycookie&quot;, null);&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; killMyCookie.setMaxAge(0);&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; killMyCookie.setPath(&quot;/&quot;);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; response.addCookie(killMyCookie);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; %&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Q.25): Can I generate and manipulate JSP pages using XML tools?&lt;/b&gt;&lt;br /&gt;
The JSP 1.1 specification describes a mapping between JSP pages and XML documents.The mapping enables the creation and manipulation of JSP pages using XML tools. &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Q.26)Is it possible to pass in any Object other than a String to a taglib?&lt;/b&gt;&lt;br /&gt;&amp;nbsp;Yes, just use an expression. The value of the expression will not be converted to a string in this case.&lt;br /&gt;&amp;nbsp;Take the following example&lt;br /&gt;&amp;nbsp;&amp;lt;example:tag date=&quot;&amp;lt;%= new java.util.Date()%&amp;gt;&quot; /&amp;gt;&lt;br /&gt;&amp;nbsp;In the above example, the corresponding tag handler will have the setDate method called with a&amp;nbsp;&amp;nbsp;&lt;br /&gt;
&amp;nbsp;java.util.Date object passed in as a parameter.&lt;br /&gt;&amp;nbsp;This is perfectly valid in your JSP assuming the tag&#39;s value can be a runtime expression. The TLD&lt;br /&gt;
&amp;nbsp;for this taglibrary would have to include the following snippet.&amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;lt;attribute&amp;gt; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;name&amp;gt;date&amp;lt;/name&amp;gt; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;rtexprvalue&amp;gt;true&amp;lt;/rtexprvalue&amp;gt; &lt;br /&gt;&amp;nbsp; &amp;lt;/attribute&amp;gt;&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Q.27)Can you have your JSP-generated servlet subclass your own servlet instead of the default HttpServlet?&lt;/b&gt;&lt;br /&gt;
Your JSP generated servlet can extend your own servlet with the directive:&quot;extend&quot;.Your own superclass servlet has to fulfill the contract with the JSP engine by :- &lt;br /&gt;
1. Implementing the HttpJspPage interface for HTTP protocol or JspPage interface. If you do not then you will have to make sure that all your super-class servlet methods are declared as final.&lt;br /&gt;
2.&amp;nbsp;&amp;nbsp;&amp;nbsp; Implementing your super-class servlet methods as follows:&lt;br /&gt;
The service() method has to invoke the _jspService() method. The init() method has to invoke the jspInit() method. The destroy() method has invoke jspDestroy() method.&lt;br /&gt;
If the above conditions are not met, then a translation error may be thrown by your JSP engine. &lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://jksolutions-corejava.blogspot.com/feeds/6154944937409983311/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://jksolutions-corejava.blogspot.com/2011/09/jsp-all-in-onejsp-interview-questions.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/299468836146488031/posts/default/6154944937409983311'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/299468836146488031/posts/default/6154944937409983311'/><link rel='alternate' type='text/html' href='http://jksolutions-corejava.blogspot.com/2011/09/jsp-all-in-onejsp-interview-questions.html' title='JSP All in one(JSP interview Questions and ans)'/><author><name>jksolutions</name><uri>http://www.blogger.com/profile/01556365883008986930</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>