<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>rajsekhar</title>
	<atom:link href="http://rajsekhar.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://rajsekhar.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Tue, 17 Jul 2007 08:50:45 +0000</lastBuildDate>
	<language></language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='rajsekhar.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>rajsekhar</title>
		<link>http://rajsekhar.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://rajsekhar.wordpress.com/osd.xml" title="rajsekhar" />
	<atom:link rel='hub' href='http://rajsekhar.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Hello world!</title>
		<link>http://rajsekhar.wordpress.com/2007/07/17/hello-world/</link>
		<comments>http://rajsekhar.wordpress.com/2007/07/17/hello-world/#comments</comments>
		<pubDate>Tue, 17 Jul 2007 06:48:50 +0000</pubDate>
		<dc:creator>rajsekhar</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Explain DTD A document type definition (DTD) describes the permissible tags of an XML document. The DTD serves as a data template. It defines entities, elements, attributes, and notations, as well as the relationships between these. For example, the DTD can state that a memo element consists of To, From, Subject, and Message elements. You need [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rajsekhar.wordpress.com&amp;blog=1379224&amp;post=1&amp;subd=rajsekhar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h3><span><a href="http://sharat.wordpress.com/2007/05/18/explain-dtd/"><font face="Times New Roman">Explain DTD</font></a></span></h3>
<p><span><font face="Times New Roman">A document type definition (DTD) describes the permissible tags of an XML document. </font></span><span><font face="Times New Roman">The DTD serves as a data template. It defines entities, elements, attributes, and notations, as well as the relationships between these. </font></span><span><font face="Times New Roman">For example, the DTD can state that a memo element consists of To, From, Subject, and Message elements. </font></span><span><font face="Times New Roman">You need to use a DTD if you want the XML processor to validate your XML documents. </font></span><span><font face="Times New Roman">DTDs can help you ensure that your XML documents are well-formed. </font></span><span><font face="Times New Roman">You need to indicate to the XML processor that a DTD should be used. This is done by adding a document type declaration before the document element. </font></span><span><font face="Times New Roman">The DTD can be internal or external to the document. If the DTD is external, you will need to specify its location or URL.</font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2007/05/18/is-an-xml-schema-an-alternative-to-dtd/"><font face="Times New Roman">Is an XML schema an alternative to DTD</font></a></span></h3>
<p><span><font face="Times New Roman">Schemas are becoming more popular and DTDs less so. A schema is an XML-based syntax for describing how the XML document is marked up or how it looks – very similar to a DTD, but a DTD has a lot of drawbacks.<br />
A DTD doesn’t use anything like XML syntax to describe the definition. You can’t do data typing in a DTD, and it’s not extensible. </font></span><span><font face="Times New Roman">An XML schema allows you to specify elements as an integer, a float, a Boolean, or whatever, and has more extensibility to it. Plus, it’s in XML format. Your XML schema, the definition for your XML, is also in XML format.<br />
Microsoft is pushing the XML schema – so you’re likely to see schemas more and more. That’s all we use. In fact, the new BizTalk Server from Microsoft actually uses schemas.</font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2007/05/18/explain-xml-namespaces/"><font face="Times New Roman">Explain XML Namespaces</font></a></span></h3>
<p><span><font face="Times New Roman">An XML namespace is a collection of element names or attribute names to be defined within an XML document.<br />
Let’s say you have an invoice XML document and an order XML document and you want to put them together. You usually have certain names that overlap each other – like the date. You might have an invoice date and an order date – both called “date”. </font></span><span><font face="Times New Roman">With a namespace, you have an invoice date and an order date, and you can reference them as such – “invoice:date” and then “order:date.” That way you can still use the same two tag names and you won’t get confused about which is which. That’s really what an XML namespace is supposed to do for you.</font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2007/05/18/what-are-the-advantages-of-xml/"><font face="Times New Roman">What are the advantages of XML</font></a></span></h3>
<p><span><font face="Times New Roman">XML is free form which means that you can configure it any way you want. It’s also very easy to read – you don’t have to read cryptic code to figure it out.<br />
It was designed specifically for internet protocols, and this makes it simple to transmit an XML document across, for example, a HTTP protocol.<br />
Strong data typing is available for xml, and it is also compatible with the SGML standard. </font></span><span><font face="Times New Roman">XML is Application independent – you can transfer XML data from a C language program to a Visual Basic program. Or you could, for example, have XML data going from one server out across the internet and being picked up by another application running on a Unix box. XML is also platform independent – it works equally well on Windows, UNIX, or for example, a CICS on a mainframe.<br />
XML is also language independent so it doesn’t matter what type of programming language you’re using – C, Visual Basic, ASP using JavaScript. It doesn’t matter – all have mechanisms to read an XML document. </font></span><span><font face="Times New Roman">XML is Unicode-based which makes it very flexible and very good for operating across languages like English, Spanish, or French. You can describe all these types of languages since it’s in Unicode.<br />
A huge benefit of XML is that it’s license free –<br />
it doesn’t cost anything to use XML.</font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2007/05/18/differences-between-dtd-and-schema/"><font face="Times New Roman">Differences between DTD and Schema</font></a></span></h3>
<table border="1" cellPadding="0" cellSpacing="0" style="border-collapse:collapse;border:medium none;">
<thead>
<td style="background-color:transparent;border:#ece9d8;padding:0;"><strong><font size="3"><font face="Times New Roman"> </font></font></strong></td>
<td style="background-color:transparent;border:#ece9d8;padding:0;"><strong><font size="3"><font face="Times New Roman">XML Schema </font></font></strong></td>
<td style="background-color:transparent;border:#ece9d8;padding:0;"><strong><font size="3"><font face="Times New Roman">DTD </font></font></strong></td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Markup validation </font></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Any global element can be root. No ambiguous content support. </font></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Can specify only the root element in the instance document. No ambiguous content support. </font></p>
</td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Namespace support </font></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Yes. Declarations only where multiple namespaces are used. </font></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">No. </font></p>
</td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Code reuse </font></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Can reuse definitions using named types. </font></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Poorly supported. Can use parameter entities. </font></p>
</td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Datatype Validation </font></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Provides flexible set of datatypes. Provides multi-field key cross references. No co-occurrence constraints. </font></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">No real datatype support. </font></p>
</td>
</tr>
</table>
<p><span><font face="Times New Roman"> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2007/05/18/what-are-the-disadvantages-of-dtd/"><font face="Times New Roman">What are the disadvantages of DTD</font></a></span></h3>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">They are not written in XML syntax, which means you have to learn a new syntax in order to write them </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">there is no support for namespaces </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">there are no constraints imposed on the kind of character data allowed, so datatyping is not possible </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">there is minimal support for code modularity and none for inheritance </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">large DTDs are hard to read and maintain </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">there are no default values for elements and attribute defaults must be specified when they are declared </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">its attribute value models and ID attribute mechanism are simplistic </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">there is limited ability to control whitespace </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">there is limited documentation support, as you cannot use the structured documentation features available for schema notation </font></span></li>
</ul>
<p><strong><span style="font-size:10pt;font-family:Verdana;"> </span></strong><strong><span style="font-size:10pt;font-family:Verdana;"> </span></strong><font size="2"><font face="Verdana"> </font></font><font size="2"><font face="Verdana"> </font></font><font size="2"><font face="Verdana"> </font></font></p>
<h3><span><a href="http://sharat.wordpress.com/2007/05/16/exception-drill/"><font face="Times New Roman">Exception Drill</font></a></span></h3>
<p><strong><span style="color:red;"><font face="Times New Roman">1. What is an Exception ? </font></span></strong><span><br />
<font face="Times New Roman">An exception is an abnormal condition that arises in a code sequence at run time. In other words, an exception is a run-time error. </font></span><strong><span style="color:red;"><font face="Times New Roman">2. What is a Java Exception ? </font></span></strong><span><br />
<font face="Times New Roman">A Java exception is an object that describes an exceptional condition i.e., an error condition that has occurred in a piece of code. When this type of condition arises, an object representing that exception is created and thrown in the method that caused the error by the Java Runtime. That method may choose to handle the exception itself, or pass it on. Either way, at some point, the exception is caught and processed. </font></span><strong><span style="color:red;"><font face="Times New Roman">3.Where does Exception stand in the Java tree hierarchy ? </font></span></strong><strong><span><br />
</span></strong><span><font face="Times New Roman">         java.lang.Object<br />
         java.lang.Throwable<br />
         java.lang.Exception<br />
         java.lang.Error </font></span><strong><span style="color:red;"><font face="Times New Roman">4.What are checked exceptions ?</font></span></strong><span><br />
<font face="Times New Roman">Checked exception are those which the Java compiler forces you to catch. e.g. IOException are checked Exceptions. </font></span><strong><span style="color:red;"><font face="Times New Roman">5.What are runtime exceptions ?</font></span></strong><span><br />
<font face="Times New Roman">Runtime exceptions are those exceptions that are thrown at runtime because of either wrong input data or because of wrong business logic etc. These are not checked by the compiler at compile time. </font></span><strong><span style="color:red;"><font face="Times New Roman">6.What is the difference between error and an exception ?</font></span></strong><span><br />
<font face="Times New Roman">An error is an irrecoverable condition occurring at runtime. Such as OutOfMemory error. These JVM errors and you can not repair them at runtime. While exceptions are conditions that occur because of bad input etc. e.g. FileNotFoundException will be thrown if the specified file does not exist. Or a NullPointerException will take place if you try using a null reference. In most of the cases it is possible to recover from an exception (probably by giving user a feedback for entering proper values etc.). </font></span><strong><span style="color:red;"><font face="Times New Roman">7.How to create custom exceptions ?</font></span></strong><span><br />
<font face="Times New Roman">Your class should extend class Exception, or some more specific type thereof. </font></span><strong><span style="color:red;"><font face="Times New Roman">8.If I want an object of my class to be thrown as an exception object, what should I do ?</font></span></strong><span><br />
<font face="Times New Roman">The class should extend from Exception class. Or you can extend your class from some more precise exception type also. </font></span><strong><span style="color:red;"><font face="Times New Roman">9.If my class already extends from some other class what should I do if I want an instance of my class to be thrown as an exception object ?</font></span></strong><span><br />
<font face="Times New Roman">One can not do anytihng in this scenarion. Because Java does not allow multiple inheritance and does not provide any exception interface as well. </font></span><strong><span style="color:red;"><font face="Times New Roman">10.How does an exception permeate through the code ?</font></span></strong><span><br />
<font face="Times New Roman">An unhandled exception moves up the method stack in search of a matching When an exception is thrown from a code which is wrapped in a try block followed by one or more catch blocks, a search is made for matching catch block. If a matching type is found then that block will be invoked. If a matching type is not found then the exception moves up the method stack and reaches the caller method. Same procedure is repeated if the caller method is included in a try catch block. This process continues until a catch block handling the appropriate type of exception is found. If it does not find such a block then finally the program terminates. </font></span><strong><span style="color:red;"><font face="Times New Roman">11.What are the different ways to handle exceptions ?</font></span></strong><span><br />
<font face="Times New Roman">There are two ways to handle exceptions,<br />
1. By wrapping the desired code in a try block followed by a catch block to catch the exceptions. and<br />
2. List the desired exceptions in the throws clause of the method and let the caller of the method hadle those exceptions. </font></span><strong><span style="color:red;"><font face="Times New Roman">12.What is the basic difference between the 2 approaches to exception handling.</font></span></strong><span><br />
<font face="Times New Roman">1 try catch block and<br />
2 specifying the candidate exceptions in the throws clause ?<br />
In the first approach as a programmer of the method, you urself are dealing with the exception. This is fine if you are in a best position to decide should be done in case of an exception. Whereas if it is not the responsibility of the method to deal with it’s own exceptions, then do not use this approach. In this case use the second approach. In the second approach we are forcing the caller of the method to catch the exceptions, that the method is likely to throw. This is often the approach library creators use. They list the exception in the throws clause and we must catch them. You will find the same approach throughout the java libraries we use. </font></span><strong><span style="color:red;"><font face="Times New Roman">13.Is it compulsory to use the finally block  ? </font></span></strong><span><br />
<font face="Times New Roman">It is always a good practice to use the finally block. The reason for using the finally block is, any unreleased resources can be released and the memory can be freed. For example while closing a connection object an exception has occurred. In finally block we can close that object. Coming to the question, you can omit the finally block when there is a catch block associated with that try block. A try block should have at least a catch or a finally block. </font></span><strong><span style="color:red;"><font face="Times New Roman">14.How are try, catch and finally block organized  ? </font></span></strong><span><br />
<font face="Times New Roman">A try block should associate with at least a catch or a finally block. The sequence of try, catch and finally matters a lot. If you modify the order of these then the code won’t compile. Adding to this there can be multiple catch blocks associated with a try block. The final concept is there should be a single try, multiple catch blocks and a single finally block in a try-catch-finally block. </font></span><strong><span style="color:red;"><font face="Times New Roman">15.What is a throw in an Exception block ? </font></span></strong><span><br />
<font face="Times New Roman">“throw” is used to manually throw an exception (object) of type Throwable class or a subclass of Throwable. Simple types, such as int or char, as well as non-Throwable classes, such as String and Object, cannot be used as exceptions. The flow of execution stops immediately after the throw statement; any subsequent statements are not executed. </font></span><span><font face="Times New Roman">throw ThrowableInstance; ThrowableInstance must be an object of type Throwable or a subclass of Throwable.<br />
throw new NullPointerException(”thrownException”); </font></span><strong><span style="color:red;"><font face="Times New Roman">16.What is the use of throws keyword ? </font></span></strong><span><br />
<font face="Times New Roman">If a method is capable of causing an exception that it does not handle, it must specify this behavior so that callers of the method can guard themselves against that exception. You do this by including a throws clause in the method’s declaration. A throws clause lists the types of exceptions that a method might throw. </font></span><span><font face="Times New Roman">type method-name(parameter-list) throws exception-list {<br />
// body of method<br />
} </font></span><span><font face="Times New Roman">Here, exception-list is a comma-separated list of the exceptions that a method can throw. </font></span><span><font face="Times New Roman">static void throwOne() throws IllegalAccessException {<br />
System.out.println(”Inside throwOne.”); </font></span><strong><span style="color:red;"><font face="Times New Roman">17.Is it necessary that each try block must be followed by a catch block ?</font></span></strong><span><br />
<font face="Times New Roman">It is not necessary that each try block must be followed by a catch block. It should be followed by either a catch block OR a finally block. And whatever exceptions are likely to be thrown should be declared in the throws clause of the method. </font></span><strong><span style="color:red;"><font face="Times New Roman">18.If I write return at the end of the try block, will the finally block still execute ?</font></span></strong><span><br />
<font face="Times New Roman">Yes even if you write return as the last statement in the try block and no exception occurs, the finally block will execute. The finally block will execute and then the control return. </font></span><strong><span style="color:red;"><font face="Times New Roman">19.If I write System.exit (0); at the end of the try block, will the finally block still execute ?</font></span></strong><span><br />
<font face="Times New Roman">No in this case the finally block will not execute because when you say System.exit (0); the control immediately goes out of the program, and thus finally never executes. </font></span><strong><span style="color:red;"><font face="Times New Roman">20.What are Checked and UnChecked Exception ?</font></span></strong><span><br />
<font face="Times New Roman">A checked exception is some subclass of Exception (or Exception itself), excluding class RuntimeException and its subclasses.<br />
Making an exception checked forces client programmers to deal with the possibility that the exception will be thrown. eg, IOException thrown by java.io.FileInputStream’s read() method·<br />
Unchecked exceptions are RuntimeException and any of its subclasses. Class Error and its subclasses also are unchecked. With an unchecked exception, however, the compiler doesn’t force client programmers either to catch the<br />
exception or declare it in a throws clause. In fact, client programmers may not even know that the exception could be thrown. eg, StringIndexOutOfBoundsException thrown by String’s charAt() method· Checked exceptions must be caught at compile time. Runtime exceptions do not need to be. Errors often cannot be. </font></span><strong><span style="color:red;"><font face="Times New Roman">21.Give me some examples of Checked Exceptions and Unchecked Exceptions ? </font></span></strong><span><br />
<font face="Times New Roman">Unchecked Exceptions. <br />
         ArithmeticException <br />
         ArrayIndexOutOfBoundsException<br />
         ClassCastException<br />
         IndexOutOfBoundsException<br />
         IllegalStateException<br />
         NullPointerException<br />
         SecurityException  </font></span><span><font face="Times New Roman">Checked Exception<br />
        ClassNotFoundException<br />
        CloneNotSupportedException <br />
        IllegalAccessException<br />
        InstantiationException<br />
        InterruptedException<br />
        NoSuchFieldException<br />
        NoSuchMethodException </font></span><strong><span style="color:red;"><font face="Times New Roman">22. What are Chained Exceptions ? </font></span></strong><span><br />
<font face="Times New Roman">The chained exception feature allows you to associate another exception with an exception. This second exception describes the cause of the first exception. Lets take a simple example. You are trying to read a number from the disk and using it to divide a number. Think the method throws an ArithmeticException because of an attempt to divide by zero (number we got). However, the problem was that an I/O error occurred, which caused the divisor to be set improperly (set to zero). Although the method must certainly throw an ArithmeticException, since that is the error that occurred, you might also want to let the calling code know that the underlying cause was an I/O error. This is the place where chained exceptions come in to picture. </font></span><span><font face="Times New Roman">Throwable getCause( )<br />
Throwable initCause(Throwable causeExc)</font></span><font size="2"><font face="Verdana"> </font></font><font size="2"><font face="Verdana"> </font></font><font size="2"><font face="Verdana"> </font></font><font size="2"><font face="Verdana"> </font></font><font size="2"><font face="Verdana"> </font></font><font size="2"><font face="Verdana"> </font></font></p>
<h3><span><a href="http://sharat.wordpress.com/2007/05/15/collections-drill-ii/"><font face="Times New Roman">Collections Drill - II</font></a></span></h3>
<p><strong><span style="color:red;"><font face="Times New Roman">1.What is meant by compatible equals() and hashCode() methods ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">In order for the Java Collections to work properly (and everything else in Java), the equals() and hashCode() methods must be compatible. Here, compatible means that if equals() reports that two instances are the same, then the hashCode() of both instances must be the same value. </font></span><strong><span style="color:red;"><font face="Times New Roman">2.Since Properties extends Hashtable, can I use the Hashtable methods to add elements to a Properties list ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">Technically speaking you can. However, you have to make sure you only add key-value pairs where both are strings. If you add something other than a String, the listing, loading, and saving methods won’t work as expected.<br />
Like the Stack/Vector subclass relationship, Properties/Hashtable should be a has-a relationship, not an is-a/subclass relationship. </font></span><strong><span style="color:red;"><font face="Times New Roman">3.When I wrap a collection to be read-only or synchronized, why can’t I call any of the collection methods via reflection without getting an IllegalAccessException ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">When you wrap a collection through the static methods of the Collections class, this creates an instance of a package-private (default access) class. Because you don’t have access to these classes, you can’t call their methods via reflection (though you can call their methods directly through the appropriate interface). </font></span><strong><span style="color:red;"><font face="Times New Roman">4.What is a weak reference and what are they used for ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">Normally the Java garbage collector plays safe. It will only free up the memory used by an object when that object can no longer be accessed by the program. Once an object become impossible to reach it is eligible for collection, and eventually its memory will be reclaimed.<br />
This eliminates one of the most common programming errors in some other languages (like C++), where code accidentally tries to access an object that has been freed. Unfortunately it can lead to another problem, where you leave open a potential access route to an object that you don’t need any more. Memory fills up, and the program slows down or reports an “Out of Memory” error. </font></span><span><font face="Times New Roman">To avoid this, you can be very careful to close off access paths to an object once you have finished using it. Java 2 introduces another alternative, the weak reference. Weak references provide access to an object without preventing it from being freed. When you use a weak reference you have to accept that the object referred to may have disappeared, which results in the reference being automatically set to null. On the other hand, the weak reference will not hold the object in memory once it is inaccessible via normal references (or via “soft” references &#8211; see below). Weak references are not appropriate in all circumstances, but sometimes they can make code easier to write and understand. </font></span><span><font face="Times New Roman">The most common use of weak references is indirect &#8211; they are used internally by the WeakHashMap class. Like HashMap, WeakHashMap associates key objects with values. However, once the key object becomes inaccessible via stronger references it becomes eligible for garbage collection. When it is freed, the map entry magically disappears. The assumption here is that if you are not using the key anywhere other than in the map you will have no need to look it up, so it should be freed. </font></span><span><font face="Times New Roman">Other specialist references are soft references (which inhibit collection until memory runs short), and phantom references (used for cleanup when objects are freed). </font></span><font face="Times New Roman"><strong><span style="color:red;">5.What is the minimum number of key-value pairs for which it makes sense to use a HashMap, as opposed to using a pair of arrays (one for keys, the other for values) with brute-force key searches ? </span></strong><span></span></font><span><font face="Times New Roman">Many people often need maps for very small numbers (2-5) of key-value pairs. When does it make sense to forgo the convenience of the HashMap to avoid the associated overhead?<br />
Well, is there really that much of a performance loss using a HashMap? There is no synchronization penalty (unless you impose your own). You can tune the sizing by adjusting the initial size and load factor. Plus, do you really want to be responsible for “rolling your own” code to handle the dynamic resizing of the key and value arrays, inserting/removing data from these arrays, optimizing the searching algorithm, etc. Yuck! </font></span><span><font face="Times New Roman">In general, the performance hit associated with using a general purpose Map (such as the HashMap) is far outweighed by the benefits of using a simple interface backed by a tested algorithm. </font></span><span><font face="Times New Roman">The only reason I could see wanting to use arrays is to guaruntee the type of your key/values to add type checking and avoid casting. Still, if this is a critical aspect of your application, you can wrap your HashMap in another object to provide type-safety, and the casting overhead should be minimal. </font></span><span><font face="Times New Roman">Another alternative to creating a custom solution is to explore other collection classes, such as ObjectSpaces’s JGL Libraries. There may be something there that would suit your needs. </font></span><span><font face="Times New Roman">So, to answer your question, I would say that the fewer the key-value pairs you have, the more reason you have to use a HashMap. Since the fewer the keys, the faster the search, why not use it for 2-5 key-value pairs. I would think that only when you get to many pairs (tens of thousands) and there is a performance problem you should consider an alternative. Basically, exhaust your search of tried-and-true collections before you try a custom solution. Let other people create these collections so you can focus on your application. </font></span><strong><span style="color:red;"><font face="Times New Roman">6.How does ArrayList increase its capacity ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">Unlike Vector where you can specify a capacity increment, ArrayList doesn’t support this. Instead, ArrayList will increase capacity by about a half when it runs out of space. The refernece implementation uses the forumla:<br />
newCapacity = (oldCapacity * 3)/2 + 1<br />
though, this isn’t part of the class definition so others can implement it differently. </font></span><strong><span style="color:red;"><font face="Times New Roman">7.What is the default initial size for a Hashtable / HashMap ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">This depends on what version of Java you are using. For JDK 1.2, the size was 101. For JDK 1.3, the size changed to 11. </font></span><strong><span style="color:red;"><font face="Times New Roman">8.How do you create a multi-dimensional List ? </font></span></strong><span><br />
<font face="Times New Roman">Since the elements of a List are objects, in order to make the List multi-dimensional, each object in the outer list needs to be a List, too. For instance, … </font></span><span><font face="Times New Roman">List list = new ArrayList(10);<br />
for (int i=0; i&lt;10; i++) {<br />
list.set(i, new LinkedList());<br />
} </font></span><span><font face="Times New Roman">Then just fill up each inner list with items. </font></span><strong><span style="color:red;"><font face="Times New Roman">9.In a TreeMap, can I use a sorting algorithm other than the natural sorting for the keys ? </font></span></strong><span><br />
<font face="Times New Roman">You can pass a Comparator to the TreeMap constructor to use a sorting order other than the natural order. </font></span><strong><span style="color:red;"><font face="Times New Roman">10.What are the differences between HashMap and Hashtable ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">Both provide key-value access to data. The Hashtable is one of the original collection classes in Java. HashMap is part of the new Collections Framework, added with Java 2, v1.2.<br />
The key difference between the two is that access to the Hashtable is synchronized on the table while access to the HashMap isn’t. You can add it, but it isn’t there by default. </font></span><span><font face="Times New Roman">Another difference is that iterator in the HashMap is fail-safe while the enumerator for the Hashtable isn’t. If you change the map while iterating, you’ll know. </font></span><span><font face="Times New Roman">And, a third difference is that HashMap permits null values in it, while Hashtable doesn’t. </font></span><span><font face="Times New Roman">For new code, I would tend to always use HashMap. </font></span><strong><span style="color:red;"><font face="Times New Roman">11.What are the differences between Vector and ArrayList? Which is best to use ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">Vector and ArrayList are very similar. Both of them represent a ‘growable array’, where you access to the elements in it through an index. </font></span><span><font face="Times New Roman">ArrayList it’s part of the Java Collection Framework, and has been added with version 1.2, while Vector it’s an object that is present since the first version of the JDK. Vector, anyway, has been retrofitted to implement the List interface. </font></span><span><font face="Times New Roman">The main difference is that Vector it’s a synchronized object, while ArrayList it’s not. </font></span><span><font face="Times New Roman">While the iterator that are returned by both classes are fail-fast (they cleanly thrown a ConcurrentModificationException when the orignal object has been modified), the Enumeration returned by Vector are not. </font></span><span><font face="Times New Roman">Unless you have strong reason to use a Vector, the suggestion is to use the ArrayList. </font></span><strong><span style="color:red;"><font face="Times New Roman">12.How should I implement object comparisons in a flexible manner? For example, I have a Person class and sometimes I will compare based on name and sometimes I will compare based on age. </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">Instead of having the Person class implement the Comparable interface, you could delegate the comparing to another class. Perhaps you could have a PersonComparator interface that you could implement for the various types of comparisons. For example:<br />
public interface Person {<br />
public String getName();<br />
public int getAge();<br />
} </font></span><span><font face="Times New Roman">public interface PersonComparator {<br />
public int compare(Person p1, Person p2);<br />
} </font></span><span><font face="Times New Roman">public class AgeComparator implements PersonComparator {<br />
public int compare(Person p1, Person p2) {<br />
if (p1.getAge() == p2.getAge()) return 0;<br />
return p1.getAge() &gt; p2.getAge() ? 1 : -1;<br />
}<br />
} </font></span><span><font face="Times New Roman">public class NameComparator implements PersonComparator {<br />
public int compare(Person p1, Person p2) {<br />
return p1.getName().compareTo(p2.getName());<br />
}<br />
} </font></span><span><font face="Times New Roman">This is a very simple example of the Strategy Pattern. This allows your comparisons and your object to change independent of one another. </font></span><strong><span style="color:red;"><font face="Times New Roman">13.Does the equals() method of an array do element-level checking ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">If you have two arrays in memory with the same elements, and ask first.equals(second), this does not do an element-by-element comparison. Instead, it behaves just like Object’s equals() method, essentially asking if the variables point to the same place in memory:<br />
int a[] = {1, 2, 3};<br />
int b[] = {1, 2, 3};<br />
// This prints false<br />
System.out.println(a.equals(b)); </font></span><span><font face="Times New Roman">To check for equality of two arrays, use Arrays.equals().<br />
// This prints true<br />
System.out.println(Arrays.equals(a,b)); </font></span><strong><span style="color:red;"><font face="Times New Roman">14.How can I retrieve the items in my HashSet / HashMap in the order they were added ?</font></span></strong><span><br />
<font face="Times New Roman">Prior to Java 1.4, you had to manage a separate insertion order list yourself. Starting with Java 1.4, you can use the new LinkedHashMap / LinkedHashSet classes. The iterators you get back from them return the items in insertion order. </font></span><strong><span style="color:red;"><font face="Times New Roman">15.How do you sort an ArrayList (or any list) of user-defined objects ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">Create an implementation of the java.lang.Comparable interface that knows how to order your objects and pass it to java.util.Collections.sort(List, Comparator). </font></span><strong><span style="color:red;"><font face="Times New Roman">16.How can you get the hash code for an instance of a class if the class overrode hashCode() ? </font></span></strong><span><br />
<font face="Times New Roman">The System class method identityHashCode() allows you to get this information:<br />
int code = System.identityHashCode(anObject); </font></span><strong><span style="color:red;"><font face="Times New Roman">17.How can I easily shift the elements in a List / Vector such that all the elements rotate n elements ?</font></span></strong><span><br />
<font face="Times New Roman">The Java 1.4 API adds a rotate() method to the Collections class: rotate(List list, int distance) that will shift the elements for you. </font></span><strong><span style="color:red;"><font face="Times New Roman">18.What’s the most optimum way of swapping two elements in a List ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">The 1.4 version of Collections has a swap() method to do this for you. However, for earlier version of Java, you can swap two elements w/o an intermediate variable with:<br />
list.set(index1, list.set(index2, list.get(index1)));<br />
This works because the set() method returns the original element. </font></span><strong><span style="color:red;"><font face="Times New Roman">19.What’s the purpose of the IdentityHashMap ? </font></span></strong><span><br />
<font face="Times New Roman">The IdentityHashMap uses == for equality checking instead of equals(). This can be used for both performance reasons, if you know that two different elements will never be equals and for preventing spoofing, where an object tries to imitate another. </font></span><strong><span style="color:red;"><font face="Times New Roman">20.How do I convert an old-style Enumeration to something in the Collections Framework ? </font></span></strong><span><br />
<font face="Times New Roman">Prior to Java 1.4, any conversion had to be manually done. With the introduction of 1.4, you can call Collections.list(enumeration) to automatically convert the Enumeration to an ArrayList. </font></span><strong><span style="color:red;"><font face="Times New Roman">21.How do I retrieve the values of a Hashtable/HashMap in sorted order ? </font></span></strong><span><br />
<font face="Times New Roman">Basically, you can’t directly do this. What you can do is get the Collection of values() back from the map and create a sorted collection, or maintain two maps, one in each direction, and keep the second map sorted by being a TreeMap. Which you use depends on the frequency you must sort the elements. </font></span><strong><span style="color:red;"><font face="Times New Roman">22.How can I add a Collection to another Collection ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">The java.util.Collection interface includes an addAll(Collection c) method to add one collection to another. </font></span><strong><span style="color:red;"><font face="Times New Roman">23.How can I use two iterators to go through a collection ?</font></span></strong><span><br />
<font face="Times New Roman">Just get a separate iterator for each loop:<br />
Collection l = …;<br />
for(Iterator i = l.iterator(); …) {<br />
for(Iterator j = l.iterator();…) {<br />
}<br />
} </font></span><strong><span style="color:red;"><font face="Times New Roman">24.How do I traverse a map backwards ?</font></span></strong><span><br />
<font face="Times New Roman">Just keep getting the last key and the head map before it:<br />
if (!map.isEmpty()) {<br />
Object last = map.lastKey();<br />
boolean first = true;<br />
do {<br />
if (!first) {<br />
System.out.print(”, “);<br />
}<br />
System.out.print(last);<br />
last=map.headMap(last).lastKey();<br />
first=false;<br />
} while (last != map.firstKey());<br />
System.out.println();<br />
} </font></span><strong><span style="color:red;"><font face="Times New Roman">25.How do I traverse a sorted set backwards ?</font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">Just keep getting the last element and the head set before it:<br />
if (!set.isEmpty()) {<br />
Object last = set.last();<br />
boolean first = true;<br />
do {<br />
if (!first) {<br />
System.out.print(”, “);<br />
}<br />
System.out.print(last);<br />
last=set.headSet(last).last();<br />
first=false;<br />
} while (last != set.first());<br />
System.out.println();<br />
} </font></span><strong><span style="color:red;"><font face="Times New Roman">26.How can I go through an Iterator mulitple times ?</font></span></strong><span><br />
<font face="Times New Roman">There is no direct support for this. You’ll need to create your own caching mechanism. For instance, as you go through the Iterator the first time, add the elements to a LinkedList. Then, you can just get an Iterator from the LinkedList for the second pass through. </font></span><strong><span style="color:red;"><font face="Times New Roman">27.What’s new to the Collections Framework in Java 1.4 ?</font></span></strong><span><br />
<font face="Times New Roman">There are three new implementations:<br />
          LinkedHashSet<br />
          LinkedHashMap<br />
          IdentityHashMap<br />
         One marker interface: <br />
         RandomAccess<br />
        And six new utility methods for the Collections class:<br />
        rotate(List list, int distance)<br />
        replaceAll(List list, Object oldVal, Object newVal)<br />
        indexOfSubList(List source, List target)<br />
        lastIndexOfSubList(List source, List target) <br />
       swap(List list, int i, int j) <br />
       list(Enumeration e) </font></span><strong><span style="color:red;"><font face="Times New Roman">28.How can I add an array of objects to a collection ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">First you need to convert the array to a Collection. This can be done with Arrays.asList(objectArray). Once you have the array as a List, you can add it to another Collection with theCollection.addAll(theList). </font></span><strong><span style="color:red;"><font face="Times New Roman">29.Is Vector’s clone method thread-safe ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">Sure it is, since it is a Vector which is thread-safe. </font></span><strong><span style="color:red;"><font face="Times New Roman">30.How do I load property settings with the Properties class ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">java.util.Properties objects can load values from a file using the method load(InputStream).<br />
Here is the code you need: </font></span><span><font face="Times New Roman">Properties props = new Properties();<br />
props.load(new FileInputStream(”propertyfile.properties”));<br />
String value = props.getProperty(”propertyname”); </font></span><span><font face="Times New Roman">//Just a trick: in a web archive (war) you can get the InputStream inside the war archive using<br />
ClassLoader cl = this.getClass().getClassLoader();<br />
InputStream is = cl.getResourceAsStream(”it/package/application.properties”); </font></span><span><font face="Times New Roman">This is better than using a FileInputStream, because you are loading the file within the archive as it was a resource. You should use this.getClass().getClassLoader() to use the same ClassLoader as the one used the servlet container to load your JSP/Servlet. This code is snipped from a JSP page inside Tomcat. </font></span><strong><span style="color:red;"><font face="Times New Roman">31.How do I save properties settings with the Properties class ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">Try this:<br />
Properties prop = new Properties();<br />
FileOutputStream output = new FileOutputStream(”Test.properties”);<br />
prop.store(output,”my testproperties”);<br />
output.flush();<br />
output.close(); </font></span><span><font face="Times New Roman">You’ll need to catch an IOException. </font></span><strong><span style="color:red;"><font face="Times New Roman">32.What happens if two threads perform a get of one hashmap at the same time ? </font></span></strong><span><br />
<font face="Times New Roman">Synchronization needs to be done only when there is a chance of changing the data from different threads simultaneously. In your case, it is simply going to be a read, the synchronization is not required. If you need to remove or modify the values in the hashmap, then you [may] need to synchronize that. </font></span><span><font face="Times New Roman">For synchronizing a HashMap, you can use Collections.synchronizedMap(&lt;your hashmap reference&gt;) which will return a synchronized map for you, which is thread-safe. </font></span><span><font face="Times New Roman">Remember, synchronization will cause a performance problem. So, it needs to be used carefully, when really required. </font></span><strong><span style="color:red;"><font face="Times New Roman">33.How can I convert a Collection to an Array then back to a Collection ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">The Collection interface provides a mechanism to turn a Collection into an Array using the methods &lt;T&gt; T[] toArray(T[] a) or Object[] toArray(). The first method will return a Array containing all the elements of the Collection with the array being that of the type provided in the method call. The second method just returns an array being of an Object[] type. </font></span><span><font face="Times New Roman">The Arrays class provides the opposite. A way to turn an array into a List using the List&lt;T&gt; asList(Array[] a) method. The List returned is of a fixed length with any attempts to add an element throwing an UnsupportedOperationException. </font></span><span><font face="Times New Roman">import java.util.*; </font></span><span><font face="Times New Roman">public class G{<br />
public static void main(String[] args){<br />
List&lt;String&gt; sun = new ArrayList&lt;String&gt;();<br />
sun.add(”Feel”);<br />
sun.add(”the”);<br />
sun.add(”power”);<br />
sun.add(”of”);<br />
sun.add(”the”);<br />
sun.add(”Sun”);<br />
String[] s1 = sun.toArray(new String[0]); //Collection to array<br />
for(int i = 0; i &lt; s1.length; ++i){<br />
String contents = s1[i];<br />
System.out.print(contents);<br />
}<br />
System.out.println();<br />
List&lt;String&gt; sun2 = Arrays.asList(s1); //Array back to Collection<br />
for(String s2: sun2){<br />
String s3 = s2; System.out.print(s3);<br />
}<br />
//sun2.add(new String(”Hello”)); // throws UnsupportedOperationException<br />
}<br />
} </font></span><strong><span style="color:red;"><font face="Times New Roman">34.How can I create a Collection based on another Collection ?</font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">Every concrete implementation provides a constructor, which takes a Collection as an argument. Care must be taken when creating a Collection based another Collection, this is because depending on the target concrete implementation being created, specific rules regarding duplicates may be be enforced. Such as creating a Set based on a List. </font></span><span><font face="Times New Roman">The following is a short list of the constructors provided by some of the concrete Classes of the JCF (Java Collections Framework), which enable the creation of a Collection based an another implementation. </font></span><span><font face="Times New Roman">ArrayList(Collection&lt;? extends E&gt; c)<br />
LinkedList(Collection&lt;? extends E&gt; c)<br />
Vector(Collection&lt;? extends E&gt; c)<br />
TreeSet(Collection&lt;? extends E&gt; c) </font></span><span><font face="Times New Roman">Creating a Collection based on another Collection is quite easy. The hard part is knowing which Collection to use based on performance and other issues. </font></span><span><font face="Times New Roman">For example the ArrayList created will contain the same elements in the same order as the first ArrayList created. </font></span><span><font face="Times New Roman">List&lt;String&gt; slist = new ArrayList&lt;String&gt;();<br />
slist.add(”g”);<br />
slist.add(”a”);<br />
slist.add(”d”);<br />
slist.add(”a”);<br />
slist.add(”f”);<br />
slist.add(”e”);<br />
slist.add(”c”);<br />
slist.add(”b”);<br />
for(String s : slist){<br />
System.out.print(s + “\t”);<br />
}<br />
System.out.println();<br />
List&lt;String&gt; s2list = new ArrayList&lt;String&gt;(slist);<br />
for(String s : s2list){<br />
System.out.print(s + “\t”);<br />
} </font></span><span><font face="Times New Roman">When creating a Set based on an existing List the Set will be void of duplicates.<br />
List&lt;String&gt; slist = new ArrayList&lt;String&gt;();<br />
slist.add(”g”);<br />
slist.add(”a”);<br />
slist.add(”d”);<br />
slist.add(”a”);<br />
slist.add(”f”);<br />
slist.add(”e”);<br />
slist.add(”c”);<br />
slist.add(”b”);<br />
for(String s : slist){<br />
System.out.print(s + “\t”);<br />
}<br />
System.out.println();<br />
Set&lt;String&gt; s2set = new TreeSet&lt;String&gt;(slist);<br />
for(String s : s2set){<br />
System.out.print(s + “\t”);<br />
} </font></span><strong><span style="color:red;"><font face="Times New Roman">35.How can I define my own Comparable type so that it can be naturally sorted within a List ? </font></span></strong><span><br />
<font face="Times New Roman">When taking a peek at the Java docs you will notice certain classes implement an interface named Comparable. Take a look at some of the subclasses of Number such as Byte, Integer, Long, Float or some of the classes like String and Date. What the Comparable interface provides is a way for a class to be sorted by it’s natural ordering. So what do we mean by natural ordering? Depending on the type wishing to be sorted the natural ordering can be different things. If we are sorting Strings the ordering is lexicographic or alphabetic if we are sorting Dates the ordering is chronological if we are sorting Integers the ordering is numerical. </font></span><span><font face="Times New Roman">Comparable only contains one method that needs to be implemented by the class wishing to be sorted naturally. Remember if you try and sort a list that contains elements that do not implement the Comparable interface then Collections.sort() will throw an exception specifically a ClassCastException. </font></span><span><font face="Times New Roman">public interface Comparable&lt;T&gt;{<br />
public int compareTo(T o);<br />
} </font></span><span><font face="Times New Roman">The following is a short example on how to implement the Comparable interface and use the compareTo(T o) method.<br />
import java.util.*; </font></span><span><font face="Times New Roman">public final class Alpha implements Comparable&lt;Alpha&gt;{<br />
public static void main(String[] args){<br />
List&lt;Alpha&gt; alpha = new ArrayList&lt;Alpha&gt;();<br />
alpha.add(new Alpha(”z”));<br />
alpha.add(new Alpha(”g”));<br />
alpha.add(new Alpha(”k”));<br />
alpha.add(new Alpha(”q”));<br />
alpha.add(new Alpha(”a”));<br />
alpha.add(new Alpha(”b”));<br />
alpha.add(new Alpha(”o”));<br />
alpha.add(new Alpha(”v”));<br />
alpha.add(new Alpha(”c”));<br />
Collections.sort(alpha);<br />
System.out.println(alpha);<br />
}<br />
private String letter;<br />
public Alpha(String letter){<br />
if(letter == null){throw new NullPointerException();}<br />
this.letter = letter;<br />
}<br />
public String toString(){return letter;}<br />
public boolean equals(Alpha a){<br />
if(!(a instanceof Alpha))<br />
return false;<br />
return letter.equals(a.letter);<br />
}<br />
public int compareTo(Alpha a){<br />
int i = letter.compareTo(a.letter);<br />
return i;<br />
}<br />
} </font></span><span><font face="Times New Roman">More complex examples might included sorting on multiple fields. Most things that you would have to sort probably have more then one part like a name for instance (First:Middle:Last) or maybe you have to sort in (Brand:Model) order.<br />
import java.util.*; </font></span><span><font face="Times New Roman">public final class Car implements Comparable&lt;Car&gt;{<br />
public static void main(String[] args){<br />
Car[] cararry = {new Car(”Toyota”,”Celica”), new Car(”Honda”,”Civic”),<br />
new Car(”Ford”,”Mustang”), new Car(”Lexus”,”ES”), new Car(”Acura”,”Integra”),<br />
new Car(”Honda”,”Accord”), new Car(”Acura”,”RL”), new Car(”Toyota”,”Avalon”)<br />
};<br />
List&lt;Car&gt; car = Arrays.asList(cararry);<br />
Collections.sort(car);<br />
System.out.println(car);<br />
}<br />
private String brand;<br />
private String model;<br />
public Car(String brand, String model){<br />
if(brand == null || model == null){throw new NullPointerException();}<br />
this.brand = brand;<br />
this.model = model;<br />
}<br />
public String toString(){return brand + ” ” + model;}<br />
public boolean equals(Car car){<br />
if(!(car instanceof Car))<br />
return false;<br />
boolean samebrand = brand.equals(car.brand);<br />
return samebrand != true ? samebrand: model.equals(car.model);<br />
}<br />
public int compareTo(Car car){<br />
int i = brand.compareTo(car.brand);<br />
return(i != 0 ? i : model.compareTo(car.model));<br />
}<br />
} </font></span><strong><span style="color:red;"><font face="Times New Roman">36.What are Generics and how can I use them ? </font></span></strong><span><br />
<font face="Times New Roman">One of the biggest additions since the creation of the Collections framework is Generics.This long awaited update to the type system is a welcomed feature, which C++ developers have had in their toolbox for years using the STL. A generic type is defined using one or more type variables with it’s contained methods using that type variable as a place holder to mark a parameter or return type. For instance the interface List has now been defined as. </font></span><span><font face="Times New Roman">public interface List&lt;E&gt;{<br />
public add(E e);<br />
Iterator&lt;E&gt; iterator();<br />
}<br />
public interface Iterator&lt;E&gt;{<br />
E next();<br />
boolean hasNext();<br />
} </font></span><span><font face="Times New Roman">Type Safe Collections </font></span><span><font face="Times New Roman">So you might ask. What are Generics and why should I use them? Generics are a way to restrict a data structure to hold only a specific type thus providing compile time type checking. One of the added bonuses is that it is no longer necessary to cast a returned Object to a specific type because the compiler is aware of what type is being held by the Collection and what type is to be returned. </font></span><span><font face="Times New Roman">Set s = new SortedSet();<br />
s.add(new String(”Java”));<br />
String j = (String) s.get(0); // cast required; </font></span><span><font face="Times New Roman">// java 5<br />
Set&lt;String&gt; s = new SortedSet&lt;String&gt;();<br />
s.addElement(new String(”String Type”));<br />
String s = s.get(0); // no cast required! </font></span><span><font face="Times New Roman">Having a type safe system not only obviates the need to cast to a specific type but shields the programmer against miss-casting or casting to an unrelated type at runtime. </font></span><span><font face="Times New Roman">String s = “Java, write once run anywhere”<br />
List l = new ArrayList();<br />
l.add(new String(”Java”));<br />
Integer i = (Integer)l.get(0); // Runtime exception! ClassCastException! </font></span><span><font face="Times New Roman">// Using Generics the compiler catches<br />
String s = “Java. Write once run anywhere!”<br />
List&lt;String&gt; l = new ArrayList&lt;String&gt;();<br />
l.add(new String(”Java”));<br />
Integer i = l.get(0); </font></span><span><font face="Times New Roman">Generics and Subtyping </font></span><span><font face="Times New Roman">Generics do not share some of the things that are commonly held true in the Java language and one of them is subtyping. One would assume the following perfectly legal since it is true that Object is a supertype of String. But the following is in fact illegal and will cause a compile time error. </font></span><span><font face="Times New Roman">List&lt;Object&gt; l = new ArrayList&lt;String&gt;(); </font></span><span><font face="Times New Roman">As a rule. If X is a superclass or superinterface of Y and E is a generic type declaration then it not the case that E&lt;X&gt; is a supertype of E&lt;Y&gt;. So if E&lt;Object&gt; is not a super type of E&lt;String&gt; then what is the super type of E&lt;String&gt;? Read on and find out! </font></span><span><font face="Times New Roman">Wild Cards </font></span><span><font face="Times New Roman">Wild Cards represent what is called “the unknown type”. Essentially they can be thought of as the supertype of any Collection. Wildcards allow some flexibility in certain situations where it is needed that a type be abstracted out. For instance what if we define the following method, printSet(Collection&lt;Object&gt; x). We just saw in the previous example that E&lt;Object&gt; is not the super type of E&lt;String&gt; or any other type for that matter. In this case we can change the printSet’s parameter to Collection&lt;?&gt;. This allows us to pass in E&lt;X&gt; where X is any type. </font></span><span><font face="Times New Roman">public void printElements(Collection&lt;?&gt; c){<br />
for(Object o: c){<br />
System.out.println(o);<br />
}<br />
} </font></span><span><font face="Times New Roman">When working with wildcards it is always legal to read and assign the contents to an Object type </font></span><span><font face="Times New Roman">List&lt;String&gt; l = new ArrayList&lt;String&gt;();<br />
l.add(new String(”Java”));<br />
Object o = getElement(l, 0); // legal </font></span><span><font face="Times New Roman">public Object getElement(List&lt;?&gt; l, int index){<br />
Object o = null;<br />
try{<br />
o = l.get(0);<br />
}catch(IndexOutOfBoundsException e){<br />
//…….<br />
}<br />
return o;<br />
} </font></span><span><font face="Times New Roman">assigning values is another matter. The add() method takes an argument of type E which is the type that the Collection is to hold. Any type wishing to be added to the Collection would have to be of the same type. Since&lt;?&gt; represents an unknown type it is impossible to determine what the type parameter of the collection represents. </font></span><span><font face="Times New Roman">Bounded Wildcards </font></span><span><font face="Times New Roman">A Bounded Wildcard allows as type to be constrained. Bounded Wildcards are useful when there is some type of partial knowledge about the type arguments. While it is still illegal to try and add an element to a unknown Collection with a bounded type they come in handy in other situations. One use is to be able to pass not only types into a method but sub-types also. In doing this we are able to implement polymorphic behavior. </font></span><span><font face="Times New Roman">import java.util.*; </font></span><span><font face="Times New Roman">class Printer{<br />
public void print(String s){<br />
for(int i = 0; i &lt; s.length(); i++){<br />
System.out.print(s.charAt(i));<br />
}<br />
}<br />
}<br />
class ReversePrinter extends Printer{<br />
public void print(String s){<br />
for(int i = s.length() &#8211; 1 ; i &gt;= 0; i–){<br />
System.out.print(s.charAt(i));<br />
}<br />
}<br />
}<br />
public class G{<br />
public static void main(String[] args){<br />
String s = “Nothing like a good cup of Java in the morning!”<br />
List&lt;Printer&gt; l = new ArrayList&lt;Printer&gt;();<br />
l.add(new Printer());<br />
printElements(l,s);<br />
List&lt;ReversePrinter&gt; rl = new ArrayList&lt;ReversePrinter&gt;();<br />
rl.add(new ReversePrinter());<br />
printElements(rl,s);<br />
}<br />
public static void printElements(List&lt;? extends Printer&gt; l, String s){<br />
Printer printer = l.get(0);<br />
printer.print(s);<br />
System.out.println();<br />
}<br />
} </font></span><strong><span style="color:red;"><font face="Times New Roman">37.How can I shuffle the elements of a Collection ?</font></span></strong><span><br />
<font face="Times New Roman">The Collections class which can be found within the java.util namespace provides two methods which suffle the elements of a Collection. </font></span><span><font face="Times New Roman">static void shuffle(List&lt;?&gt; list)<br />
static void shuffle(List&lt;?&gt; list, Random rnd) </font></span><span><font face="Times New Roman">The first method shuffles the elements according to a default source of randomness, with the second using a specified source of randomness.<br />
import java.util.*; </font></span><span><font face="Times New Roman">public class ShuffleTest{<br />
public static void main(String[] args){<br />
List&lt;String&gt; sl = new ArrayList&lt;String&gt;();<br />
sl.add(”One”);<br />
sl.add(”Two”);<br />
sl.add(”Three”);<br />
sl.add(”Four”);<br />
sl.add(”Five”);<br />
sl.add(”Six”);<br />
for(String s: sl){<br />
System.out.println(s);<br />
}<br />
System.out.println();<br />
Collections.shuffle(sl);<br />
for(String s: sl){<br />
System.out.println(s);<br />
}<br />
}<br />
} </font></span><strong><span style="color:red;"><font face="Times New Roman">38.How can i tell if two Collections contain the same elements or have no elements in common ? </font></span></strong><span><br />
<font face="Times New Roman">Two methods are needed in this case. </font></span><span><font face="Times New Roman">boolean containsAll(Collection&lt;?&gt; c)<br />
boolean disjoint(Collection&lt;?&gt;c1 Collection&lt;?&gt;c2) </font></span><span><font face="Times New Roman">Since containsAll(Collection&lt;?&gt; c) is defined within the Collection interface all concrete implementations will be able to use this method. disjoint(Collection&lt;?&gt;c1 Collection&lt;?&gt;c2) is defined within the Collections class. </font></span><span><font face="Times New Roman">Using both of these methods is pretty straightforward. containsAll(Collection&lt;?&gt; c) is an instance method so naturally it must be invoked on an object. disjoint(Collection&lt;?&gt;c1 Collection&lt;?&gt;c2) is defined as Static within the Collections class so all that is needed is to invoke it using the class name ie Collections.disjoint(Collection&lt;?&gt;c1 Collection&lt;?&gt;c2) </font></span><strong><span style="color:red;"><font face="Times New Roman">39.How can I traverse a List backwards ? </font></span></strong><span><br />
<font face="Times New Roman">In order to traverse a List backwards a ListIterator must be used. The List interface provides a method, which returns a ListIterator. </font></span><span><font face="Times New Roman">ListIterator&lt;E&gt; listIterator() </font></span><span><font face="Times New Roman">Using a returned ListIterator, concrete implementations of List can be traverse backwards using the following methods. </font></span><span><font face="Times New Roman">boolean hasPrevious()<br />
E previous() </font></span><span><font face="Times New Roman">Since ListIterator extends the Iterator interface forward direction is still possible via Iterators methods.<br />
boolean hasNext()<br />
E next() </font></span><span><font face="Times New Roman">import java.util.List;<br />
import java.util.ArrayList;<br />
import java.util.ListIterator; </font></span><span><font face="Times New Roman">public class {<br />
public static void main(String[] args){<br />
List&lt;String&gt; slist = new ArrayList&lt;String&gt;();<br />
slist.add(”1″);<br />
slist.add(”2″);<br />
slist.add(”3″);<br />
slist.add(”4″);<br />
ListIterator i = slist.listIterator();<br />
while(i.hasNext()){<br />
System.out.print(i.next());<br />
}<br />
System.out.println();<br />
while(i.hasPrevious()){<br />
System.out.print(i.previous());<br />
}<br />
}<br />
} </font></span><strong><span style="color:red;"><font face="Times New Roman">40.How can I get a sorted list of keys that are contained within a Map ? </font></span></strong><strong><span><br />
</span></strong><span><font face="Times New Roman">The Map interface defines a method named keySet() which concrete classes such as HashMap and TreeMap implement. Depending on the implementation on which keySet() is invoked the returned Set might not contain it’s elements (keys) in sorted order. For instance the HashMap class makes no guarantees as to the order of the elements contained within. Whereas the TreeMap class does guarantee element ordering since it implements the SortedMap interface. </font></span><span><font face="Times New Roman">/* TreeMap used. Keys stored in ascending order */ </font></span><span><font face="Times New Roman">Map&lt;String,String&gt; book = new TreeMap&lt;String,String&gt;();<br />
book.put(new String(”Java”),new String(”A trademark used for a programming language designed to develop applications, especially ones for the Internet, that can operate on different platforms.”));<br />
book.put(new String(”C#”),new String(”An object-oriented language devised and promoted by Microsoft, intended to replace Java, which it strongly resembles.”));<br />
book.put(new String(”Python”),new String(”A simple, high-level interpreted language by Guido van Rossum”));<br />
book.put(new String(”LISP”),new String(”A programming language designed to process data consisting of lists. It is widely used in artificial intelligence research.”));<br />
Set words = book.keySet();<br />
for(Iterator i = words.iterator();i.hasNext();){<br />
System.out.print(i.next() + “\t”);<br />
} </font></span><strong><span style="color:red;"><font face="Times New Roman">41.How can I create a read only Collection ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">Unmodifiable Collections can be easily created using various static methods which the Collections class provides. Any attempts to modify the returned Collection, whether direct or via its iterator, result in an UnsupportedOperationException. </font></span><span><font face="Times New Roman">Collection&lt;T&gt; unmodifiableCollection(Collection&lt;? extends T&gt; c)<br />
List&lt;T&gt; unmodifiableList(List&lt;? extends T&gt; list)<br />
Set&lt;T&gt; unmodifiableSet(Set&lt;? extends T&gt; s)<br />
SortedSet&lt;T&gt; unmodifiableSortedSet(SortedSet&lt;T&gt; s) </font></span><span><font face="Times New Roman">import java.util.*; </font></span><span><font face="Times New Roman">public class Unmod{<br />
public static void main(String[] args){<br />
List&lt;String&gt; strlist = new ArrayList&lt;String&gt;();<br />
strlist.add(”C”);<br />
strlist.add(”B”);<br />
strlist.add(”A”);<br />
Collection&lt;String&gt; unmodstrlist = Unmod.makeUnmodifiable(strlist);<br />
// unmodstrlist.add(”G”); throws UnsupportedOperationException<br />
Set&lt;String&gt; strset = new TreeSet&lt;String&gt;();<br />
strset.add(”C”);<br />
strset.add(”B”);<br />
strset.add(”A”);<br />
Collection&lt;String&gt; unmodstrset = Unmod.makeUnmodifiable(strset);<br />
// unmodstrset.add(”G”); throws UnsupportedOperationException<br />
}<br />
public static Collection&lt;String&gt; makeUnmodifiable(Collection&lt;String&gt; c){<br />
return(Collections.unmodifiableCollection(c));<br />
}<br />
} </font></span><strong><span style="color:red;"><font face="Times New Roman">42.Is there a way determine how many times an Object occurs within a Collection ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">The Collections class provides a method which returns the number of times an Object appears within a given Collection. </font></span><span><font face="Times New Roman">public static int frequency(Collection&lt;?&gt; c, Object o) </font></span><span><font face="Times New Roman">If a null Collection is passed in then a NullPointerException is thrown.<br />
import java.util.*; </font></span><span><font face="Times New Roman">public class Freq {<br />
public static void main(String[] args){<br />
List&lt;Integer&gt; password = new ArrayList&lt;Integer&gt;();<br />
password.add(new Integer(4));<br />
password.add(new Integer(6));<br />
password.add(new Integer(8));<br />
password.add(new Integer(4));<br />
password.add(new Integer(9));<br />
Integer passwordelement = new Integer(4);<br />
System.out.println(passwordelement + ” appears “<br />
+ getFrequency(password,passwordelement) + ” times within password”);<br />
}<br />
private static int getFrequency(Collection c, Object o){<br />
return(Collections.frequency(c,o));<br />
}<br />
} </font></span><strong><span style="color:red;"><font face="Times New Roman">43.What is the easiest way to obtain a Map Entry ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">The easiest way to obtain a Map Entry or (key-value pair) is by invoking the following method provided by the Map interface. </font></span><span><font face="Times New Roman">Set&lt;Map.Entry&lt;K,V&gt;&gt; entrySet(); </font></span><span><font face="Times New Roman">The entrySet() method returns a Set which is populated with Map.Entry objects. The only way to obtain a reference to a Map.Entry is by using an Iterator on the returned Set view. Once a reference to a Map.Entry is obtained the follow methods can be invoked which return the key and value corresponding to the entry. </font></span><span><font face="Times New Roman">K getKey()<br />
V getValue() </font></span><span><font face="Times New Roman">import java.util.Map;<br />
import java.util.Set;<br />
import java.util.TreeMap;<br />
import java.util.Iterator; </font></span><span><font face="Times New Roman">public class Emps{<br />
public static void main(String[] args){<br />
Map&lt;String,String&gt; empmap = new TreeMap&lt;String,String&gt;();<br />
empmap.put(”956544″,”Bob Jones”);<br />
empmap.put(”132485″,”Phil Harris”);<br />
empmap.put(”102161″,”Kamal Uganda”);<br />
empmap.put(”226545″,”Bill Russel”);<br />
empmap.put(”116423″,”Dorris Smith”);<br />
Set s = empmap.entrySet();<br />
for(Iterator i = s.iterator();i.hasNext();){<br />
Map.Entry me = (Map.Entry)i.next();<br />
System.out.println(me.getKey() + ” : ” + me.getValue());<br />
}<br />
}<br />
} </font></span><strong><span style="color:red;"><font face="Times New Roman">44.How can I find the maximum element contained within a Collection ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">Finding the maximum element within a Collection is easy. The following method can be used which can be found within the Collections class. </font></span><span><font face="Times New Roman">public static &lt;T extends Object &amp; Comparable&lt;? super T&gt;&gt; T max(Collection&lt;? extends T&gt; coll) </font></span><span><font face="Times New Roman">This method returns the maximum element of the given Collection according to the natural ordering of it’s elements. This means that all elements must implement the Comparable interface. With the following code below the implementation of the Comparable interface is already taken care of since the class Byte already implements this interface.<br />
import java.util.Set;<br />
import java.util.ArrayList;<br />
import java.util.Collection;<br />
import java.util.Collections; </font></span><span><font face="Times New Roman">public class Max{<br />
public static void main(String[] args){<br />
Collection&lt;Byte&gt; numl = new ArrayList&lt;Byte&gt;();<br />
numl.add(new Byte(”2″));<br />
numl.add(new Byte(”6″));<br />
numl.add(new Byte(”3″));<br />
numl.add(new Byte(”1″));<br />
numl.add(new Byte(”5″));<br />
numl.add(new Byte(”4″));<br />
System.out.print(”Max element is ” + getMax(numl));<br />
}<br />
public static Byte getMax(Collection&lt;Byte&gt; c){<br />
return Collections.max(c);<br />
}<br />
} </font></span><span><font face="Times New Roman">If the element type being store within the Collection is user defined, implementation of the Comparable interface must be provided. </font></span><span><font face="Times New Roman">import java.util.Set;<br />
import java.util.ArrayList;<br />
import java.util.Collection;<br />
import java.util.Collections; </font></span><span><font face="Times New Roman">public class Max{<br />
public static void main(String[] args){<br />
Collection&lt;Num&gt; numl = new ArrayList&lt;Num&gt;();<br />
numl.add(new Num(”2″));<br />
numl.add(new Num(”6″));<br />
numl.add(new Num(”3″));<br />
numl.add(new Num(”1″));<br />
numl.add(new Num(”5″));<br />
numl.add(new Num(”4″));<br />
System.out.print(”Max element is ” + getMax(numl).getNum());<br />
}<br />
public static Num getMax(Collection&lt;Num&gt; c){<br />
return Collections.max(c);<br />
}<br />
}<br />
class Num implements Comparable&lt;Num&gt;{<br />
private String i;<br />
public Num(String i){<br />
this.i = i;<br />
}<br />
public int compareTo(Num num){<br />
int x = i.compareTo(num.i);<br />
return x;<br />
}<br />
public String getNum(){<br />
return i;<br />
}<br />
} </font></span><strong><span style="color:red;"><font face="Times New Roman">45.How can I create an immutable List consisting of n Copies of an Object ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">nCopies(int n, T o) can be used to create an immutable List which contains a specific number of copies of an Object. Care must be taken when trying to add elements to or extract elements from the returned List. Both attempts will throw UnsupportedOperationExceptions. </font></span><span><font face="Times New Roman">public static &lt;T&gt; List&lt;T&gt; nCopies(int n, T o) </font></span><span><font face="Times New Roman">import java.util.List;<br />
import java.util.Collections; </font></span><span><font face="Times New Roman">public class NCopiesTest{<br />
public static void main(String[] args){<br />
List&lt;String&gt; slist = Collections.nCopies(5, “Java Blend”); //immutable list!<br />
slist.add(”Moca Blend”); // throws UnsupportedOperationException!<br />
String selement = slist.remove(0); // throws UnsupportedOperationException!<br />
}<br />
} </font></span><strong><span style="color:red;"><font face="Times New Roman">46.What is the difference between a Stack and a Queue ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">First things first. Where can Stacks and Queue’s be found? Java provides a Stack class, which can be found within the java.util namespace. Within the same namespace you will find a Queue interface. All concrete Queue implementations can be located within the java.util.concurrent package. </font></span><span><font face="Times New Roman">What is a Stack? </font></span><span><font face="Times New Roman">Java’s Stack class extends the Vector class. A Stack represents a Collection of objects that are in LIFO (Last In First Out Order). The Stack class provides operations that allow testing for zero elements, inspection of it’s top most element, removal of it’s top most element, and the addition of elements. </font></span><span><font face="Times New Roman">boolean empty() Tests if this stack is empty.<br />
E peek() Looks at the object at the top of this stack without removing it from the stack.<br />
E pop() Removes the object at the top of this stack and returns that object as the value of this function.<br />
E push(E item) Pushes an item onto the top of this stack.<br />
int search(Object o) Returns the 1-based position where an object is on this stack. </font></span><span><font face="Times New Roman">What is a Queue? </font></span><span><font face="Times New Roman">A Queue is also a Collection of Objects similar to a Stack. Queues typically order the elements contained within in FIFO order but this is not always the case. Elements that are inserted into a Queue are inserted at the tail end as opposed to a Stack where the elements are pushed into it at the head. Looking at the Queue interface the operations are similar to what a Stack provides. </font></span><span><font face="Times New Roman">E element() Retrieves, but does not remove, the head of this queue.<br />
boolean offer(E o) Inserts the specified element into this queue, if possible.<br />
E peek() Retrieves, but does not remove, the head of this queue, returning null if this queue is empty.<br />
E poll() Retrieves and removes the head of this queue, or null if this queue is empty.<br />
E remove() Retrieves and removes the head of this queue. </font></span><span><font face="Times New Roman">So what are the differences between Queues and Stacks? </font></span><span><font face="Times New Roman">Besides how each order their elements there really isn’t much difference. Both provided pretty much the same operations. The big difference is that one is a concrete implementation (Stack) while the other is an interface (Queue) thus implying that additional functionality is provided by the concrete classes that implement the Queue interface such as blocking, and synchronization. </font></span><strong><span style="color:red;"><font face="Times New Roman">47.How can I insert an element into a List ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">In order to insert an element into a List add(int index, E element) must be used since the List interface does not provide an insert method. If the index is out of rage ie. index &lt; 0 || index &gt; size() an exception will be thrown. </font></span><span><font face="Times New Roman">import java.util.*; </font></span><span><font face="Times New Roman">public class Insert{<br />
public static void main(String[] args){<br />
List&lt;String&gt; slist = new ArrayList&lt;String&gt;();<br />
slist.add(new String(”Java”));<br />
slist.add(new String(”Write”));<br />
slist.add(new String(”run”));<br />
slist.add(new String(”anywhere!”));<br />
slist.add(2,new String(”once”));<br />
for(String s:slist){<br />
System.out.println(s);<br />
}<br />
}<br />
} </font></span><strong><span style="color:red;"><font face="Times New Roman">48.How do I make a copy of an array ?</font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">The clone() in Clonable interface allows to make a copy of the array. In the previous case i.e. arraycopy() we must mention the source array,the source offset(starting position),the destination array,the destination offset(Starting position),and the final one is the number of elements to be copied.This method is useful when you copy a part of the array to a new one. But when we need to copy a whole array to another,then we need not mention all the arguments and have a tough time instead we go for clone() method.In case of the clone() we need to mention the source array and destination array. eg: Array a2=a1.clone(); here the elements of the Array a1 is copied to the Array a2.If we add new elements in Array a1 it will not be reflected in a2.For doing so we need to use the Object refrences. </font></span><strong><span style="color:red;"><font face="Times New Roman">49.What is the purpose of the CopyOnWriteArrayList and </font></span></strong><span><font face="Times New Roman">CopyOnWriteArraySet collections ?<br />
Synchronized operations are costly and if you aren’t modifying a collection much, it is best not to synchronize all collection accesses. The CopyOnWriteXXX collections avoid concurrent modification issues during traversal by traversing through the original collection and the modifications happening in a copy of the original store. Think of CopyOnWriteArrayList as a thread-safe version of ArrayList without the syncrhonized access limitations. CopyOnWriteArraySet acts as like a Set, backed by a CopyOnWriteArrayList. </font></span><strong><span style="color:red;"><font face="Times New Roman">50.What implementations of the Deque interface does Java offer ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">ArrayDeque offers an array backed deque. LinkedList offers a linked list version. LinkedBlockingDeque offers an optionally bounded blocking version. </font></span><strong><span style="color:red;"><font face="Times New Roman">51.Why do I keep getting an UnsupportedOperationException thrown when I try some collection operations ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">Several methods of the collection interfaces are considered optional. If an unsupported operation is called, that is when you’ll see the UnsupportedOperationException thrown. The javadoc for a collection implementation should document which methods are optional. </font></span><strong><span style="color:red;"><font face="Times New Roman">53.Is it better to use a for-each loop or an Iterator with a collection ?</font></span></strong><span><font face="Times New Roman"><br />
It depends on what you need to do. The remove() method of an Iterator is the safe way to remove elements from an underlying collection. You can’t safely remove elements with a for-each loop. For just “visiting” each element, either way works. </font></span><strong><span style="color:red;"><font face="Times New Roman">54.How can I detect if a duplicate is added to a Set ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">The add() method returns a boolean. If the set did not previously contain the element, true is returned. If the set previoulsy did contain the element, false is returned. Of course, you can also check with the contains() method to see if the element is contained in the Set first. </font></span><strong><span style="color:red;"><font face="Times New Roman">55.What is a multimap ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">A multimap is a map where a single key can map to multiple values.(And a value can be associated with multiple keys.) </font></span><strong><span style="color:red;"><font face="Times New Roman">54.How do I create a multimap in Java ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">Typically a Map maps a key to a single value. To have a multimap, the values of a Map should be a List instead. Thus there can be multiple values in the List associated with a single key. </font></span><strong><span style="color:red;"><font face="Times New Roman">56.What is the natural ordering of a Boolean? Does FALSE come first or second ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">Boolean.FALSE &lt; Boolean.TRUE </font></span><strong><span style="color:red;"><font face="Times New Roman">57.What does Class&lt;String&gt; mean ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">Class&lt;String&gt; is the generics way of writing String.class. It allows you to improve type safety. </font></span><strong><span style="color:red;"><font face="Times New Roman">58.What type of data structure would you use to store java objects (Element) with the following requirements ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">I.<br />
Created/loaded only once<br />
Elements are frequently read by multiple threads<br />
No updates (add/remove) to the data structure are required once loaded<br />
Order is not important<br />
Random object access is required by a unique key search. </font></span><span><font face="Times New Roman">Answer: HashMap </font></span><span><font face="Times New Roman">II.<br />
Created once<br />
Elements are added / removed in a multi threaded environment<br />
Elements are frequently read in a multi threaded environment<br />
Order is not important<br />
Random object access is required by a unique key search. </font></span><span><font face="Times New Roman">Answer: HashTable is synchronized<br />
III.<br />
Created/loaded only once<br />
Elements are frequently read in a multi threaded environment<br />
Elements are added / removed in a multi threaded environment<br />
Elements are frequently read in a multi threaded environment<br />
Order is important<br />
Random object access is require based on an index<br />
Duplicate objects allowed </font></span><span><font face="Times New Roman">Answer: Vector is synchronized </font></span><span><font face="Times New Roman">IV. </font></span><span><font face="Times New Roman">Created once<br />
Order is important<br />
Random object access is require based on an index<br />
Elements are frequently read in a multi threaded environment </font></span><span><font face="Times New Roman">Answer: ArrayList </font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2007/05/15/collections-drill-ii/#respond" title="Comment on Collections Drill - II"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>May 15th, 2007</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2007/05/15/collections-drill-i/"><font face="Times New Roman">Collections Drill - I</font></a></span></h3>
<p><strong><span style="color:red;"><font face="Times New Roman">1.How to find the maximum size of heap used in the memory in java ?</font></span></strong><strong><span style="color:red;"><br />
</span></strong><strong><span><font face="Times New Roman">maxMemory</font></span></strong><span><font face="Times New Roman">(): Returns the maximum amount of memory that the Java virtual machine will attempt to use<br />
<strong>totalMemory():</strong> Returns the total amount of memory in the Java virtual machine.<br />
Also you can use -Xmx parameter to set the max heap size. </font></span><strong><span style="color:red;"><font face="Times New Roman">2.What is the difference between marshalling and unmarshalling ?</font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">marshling is convert from byte code to network understand able format<br />
unmarshsling is converting from network understand able fromat to byte code convertion. </font></span><strong><span style="color:red;"><font face="Times New Roman">3.What is the Collections API ? </font></span></strong><span><br />
<font face="Times New Roman">The Collections API is a set of classes and interfaces that support operations on collections of objects. </font></span><strong><span style="color:red;"><font face="Times New Roman">4.What is the List interface ?</font></span></strong><span><br />
<font face="Times New Roman">The List interface provides support for ordered collections of objects. </font></span><strong><span style="color:red;"><font face="Times New Roman">5.What is the Vector class ? </font></span></strong><span><br />
<font face="Times New Roman">The Vector class provides the capability to implement a growable array of objects. </font></span><strong><span style="color:red;"><font face="Times New Roman">6.What is an Iterator interface ? </font></span></strong><span><br />
<font face="Times New Roman">The Iterator interface is used to step through the elements of a Collection . </font></span><strong><span style="color:red;"><font face="Times New Roman">7.Which java.util classes and interfaces support event handling? </font></span></strong><span><br />
<font face="Times New Roman">The EventObject class and the EventListener interface support event processing. </font></span><strong><span style="color:red;"><font face="Times New Roman">8.What is the GregorianCalendar class ? </font></span></strong><span><br />
<font face="Times New Roman">The GregorianCalendar provides support for traditional Western calendars </font></span><strong><span style="color:red;"><font face="Times New Roman">9.What is the Locale class ? </font></span></strong><span><br />
<font face="Times New Roman">The Locale class is used to tailor program output to the conventions of a particular geographic, political, or cultural region . </font></span><strong><span style="color:red;"><font face="Times New Roman">10.What is the SimpleTimeZone class ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">The SimpleTimeZone class provides support for a Gregorian calendar . </font></span><strong><span style="color:red;"><font face="Times New Roman">11.What is the Map interface ? </font></span></strong><span><br />
<font face="Times New Roman">The Map interface replaces the JDK 1.1 Dictionary class and is used associate keys with values. </font></span><strong><span style="color:red;"><font face="Times New Roman">12.What is the highest-level event class of the event-delegation model ?</font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">The java.util.EventObject class is the highest-level class in the event-delegation class hierarchy. </font></span><strong><span style="color:red;"><font face="Times New Roman">13.What is the Collection interface ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">The Collection interface provides support for the implementation of a mathematical bag &#8211; an unordered collection of objects that may contain duplicates. </font></span><strong><span style="color:red;"><font face="Times New Roman">14.What is the Set interface ? </font></span></strong><span><br />
<font face="Times New Roman">The Set interface provides methods for accessing the elements of a finite mathematical set. Sets do not allow duplicate elements. </font></span><strong><span style="color:red;"><font face="Times New Roman">15.What is the typical use of Hashtable ?</font></span></strong><span><br />
<font face="Times New Roman">Whenever a program wants to store a key value pair, one can use Hashtable. </font></span><strong><span style="color:red;"><font face="Times New Roman">16.I am trying to store an object using a key in a Hashtable. And some other object already exists in that location, then what will happen? The existing object will be overwritten? Or the new object will be stored elsewhere?</font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">The existing object will be overwritten and thus it will be lost. </font></span><strong><span style="color:red;"><font face="Times New Roman">17.What is the difference between the size and capacity of a Vector?</font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">The size is the number of elements actually stored in the vector, while capacity is the maximum number of elements it can store at a given instance of time. </font></span><strong><span style="color:red;"><font face="Times New Roman">18.Can a vector contain heterogenous objects ?</font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">Yes a Vector can contain heterogenous objects. Because a Vector stores everything in terms of Object. </font></span><strong><span style="color:red;"><font face="Times New Roman">19.Can a ArrayList contain heterogenous objects ?</font></span></strong><span><br />
<font face="Times New Roman">Yes a ArrayList can contain heterogenous objects. Because a ArrayList stores everything in terms of Object. </font></span><strong><span style="color:red;"><font face="Times New Roman">20.What is an enumeration ?</font></span></strong><span><br />
<font face="Times New Roman">An enumeration is an interface containing methods for accessing the underlying data structure from which the enumeration is obtained. It is a construct which collection classes return when you request a collection of all the objects stored in the collection. It allows sequential access to all the elements stored in the collection. </font></span><strong><span style="color:red;"><font face="Times New Roman">21.Considering the basic properties of Vector and ArrayList, where will you use Vector and where will you use ArrayList ?</font></span></strong><span><br />
<font face="Times New Roman">The basic difference between a Vector and an ArrayList is that, vector is synchronized while ArrayList is not. Thus whenever there is a possibility of multiple threads accessing the same instance, one should use Vector. While if not multiple threads are going to access the same instance then use ArrayList. Non synchronized data structure will give better performance than the synchronized one. </font></span><strong><span style="color:red;"><font face="Times New Roman">22.Can a vector contain heterogenous objects ?</font></span></strong><span><br />
<font face="Times New Roman">Yes a Vector can contain heterogenous objects. Because a Vector stores everything in terms of Object </font></span><strong><span style="color:red;"><font face="Times New Roman">23.How do I use an Iterator to go through a Collection ? </font></span></strong><span><br />
<font face="Times New Roman">Collection collection = …;<br />
Iterator iterator = collection.iterator();<br />
while (iterator.hasNext()) {<br />
Object element = iterator.next();<br />
// Do something with element<br />
}<br />
} </font></span><strong><span style="color:red;"><font face="Times New Roman">24.How do I use a ListIterator to go through a List backwards ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">List list = …;<br />
ListIterator iterator = list.listIterator(list.size());<br />
while (iterator.hasPrevious()) {<br />
Object element = iterator.previous();<br />
// Process element<br />
} </font></span><strong><span style="color:red;"><font face="Times New Roman">25.How do I count the frequency of some word/object ? </font></span></strong><span><br />
<font face="Times New Roman">The Map interface can be used to count the number of times a word/object appears. The following program demonstrates counting word frequency from the command line: </font></span><span><font face="Times New Roman">import java.util.*; </font></span><span><font face="Times New Roman">public class MapExample {<br />
public static void main(String args[]) {<br />
Map map = new HashMap();<br />
Integer ONE = new Integer(1);<br />
for (int i=0, n=args.length; i&lt;n; i++) {<br />
String key = args[i];<br />
Integer frequency = (Integer)map.get(key);<br />
if (frequency == null) {<br />
frequency = ONE;<br />
} else {<br />
int value = frequency.intValue();<br />
frequency = new Integer(value + 1);<br />
}<br />
map.put(key, frequency);<br />
}<br />
System.out.println(map);<br />
Map sortedMap = new TreeMap(map);<br />
System.out.println(sortedMap);<br />
}<br />
} </font></span><strong><span style="color:red;"><font face="Times New Roman">26. How do I sort objects into their reverse natural ordering ? </font></span></strong><span><br />
<font face="Times New Roman">The Collections.reverseOrder() method returns a Comparator that sorts objects that implement the Comparable interface in reverse order. </font></span><strong><span style="color:red;"><font face="Times New Roman">27.How do I use Enumeration to iterate through a collection ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">Enumeration enum = …;<br />
while (enum.hasMoreElements()) {<br />
Object element = iterator.nextElement();<br />
// process element<br />
} </font></span><strong><span style="color:red;"><font face="Times New Roman">28.How do I make an array larger ?</font></span></strong><span><br />
<font face="Times New Roman">You cannot directly make an array larger. You must make a new (larger) array and copy the original elements into it, usually with System.arraycopy(). If you find yourself frequently doing this, the Vector class does this automatically for you, as long as your arrays are not of primitive data types. </font></span><strong><span style="color:red;"><font face="Times New Roman">29.How do you store a primitive data type within a Vector or other collections class ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">You need to wrap the primitive data type into one of the wrapper classes found in the java.lang package, like Integer, Float, or Double, as in: </font></span><span><font face="Times New Roman">Integer in = new Integer(5); </font></span><strong><span style="color:red;"><font face="Times New Roman">30.How do I create linked lists if there are no pointers ? </font></span></strong><span><br />
<font face="Times New Roman">No pointers does not mean no reference variables. You just can’t deference them as you can in C/C++ or perform pointer arithmetic. You can still use abstract data types that require dynamic data structures. See the LinkedList class for an example of a linked list implementation. </font></span><strong><span style="color:red;"><font face="Times New Roman">31.How do I use an array with the Collections Framework ? </font></span></strong><span><br />
<font face="Times New Roman">The Arrays.asList() method provides a fixed-length List view of an array, where changes to the List are stored in the original array. The Arrays class also provides additional support methods for sorting and searching an array. </font></span><strong><span style="color:red;"><font face="Times New Roman">32.Which is faster, synchronizing a HashMap or using a Hashtable for thread-safe access ?</font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">Because a synchronized HashMap requires an extra method call, a Hashtable is faster for synchronized access. </font></span><strong><span style="color:red;"><font face="Times New Roman">33.Which is the preferred collection class to use for storing database result sets ? </font></span></strong><span style="color:red;"><br />
</span><span><font face="Times New Roman">When retrieving database results, the best collection implementation to use is the LinkedList.<br />
The benefits include:<br />
Retains the original retrieval order<br />
Has quick insertion at the head/tail<br />
Doesn’t have an internal size limitation like a Vector where when the size is exceeded a new internal structure is created (or you have to find out size beforehand to size properly)<br />
Permits user-controlled synchronization unlike the pre-Collections Vector which is always synchronized </font></span><span><font face="Times New Roman">Basically: </font></span><span><font face="Times New Roman">ResultSet result = stmt.executeQuery(”…”);<br />
List list = new LinkedList();<br />
while(result.next()) {<br />
list.add(result.getString(”col”));<br />
} </font></span><span><font face="Times New Roman">If there are multiple columns in the result set, you’ll have to combine them into their own data structure for each row. Arrays work well for that as you know the size, though a custom class might be best so you can convert the contents to the proper type when extracting from databse, instead of later. </font></span><strong><span style="color:red;"><font face="Times New Roman">34.Why doesn’t the Iterator interface extend the Enumeration interface ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">If the Iterator interface extended the Enumeration interface, the Iterator interface would end up with five methods where two methods just called other methods in the interface. The designers of the framework wanted to get rid of the cumbersome Enumeration method names so had the Iterator interface stand on its own with new shorter method names. </font></span><strong><span style="color:red;"><font face="Times New Roman">35.How do I print a Collection ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">The Collection Framework implementation classes override the toString() method to print out all the elements of the collection. If you create your own custom implementation, as long as your class is a subclass of AbstractMap or AbstractCollection you’ll inherit this behavior. (Keep in mind that AbstractList and AbstractSet subclass AbstractCollection.) </font></span><strong><span style="color:red;"><font face="Times New Roman">36.How do I synchronize a collection ? </font></span></strong><span><br />
<font face="Times New Roman">With the Collections Framework, the new implementations are all unsynchronized by default. If you need synchronized access, you must synchronize things yourself. The Collections class offers a wrapper method for each of the six core collection interfaces that add synchronization to an arbitrary collections implementation. To ensure thread-safety, direct access to the original backing collection must be avoided.<br />
For example, the following will synchronize an arbitrary List and lose the original reference so you can’t access it directly: </font></span><span><font face="Times New Roman">List list = …;<br />
list = Collections.synchronizedList(list); </font></span><strong><span style="color:red;"><font face="Times New Roman">37.How I do a case-sensitive sort in a language-insensitive manner ?</font></span></strong><span><br />
<font face="Times New Roman">If you have an array of primitives or an array of equivalent objects that implement the Comparable interface, all you need to do is call the sort() method of the java.util.Arrays class. If the class doesn’t implement Comparable, you need to provide your own Comparator implementation to the sort() method. </font></span><strong><span style="color:red;"><font face="Times New Roman">38.How do I get the length of an array ? </font></span></strong><span><br />
<font face="Times New Roman">To avoid getting an ArrayIndexOutOfBoundsException, you can check for the array length from either the length instance variable or using reflection and calling java.lang.reflect.Array.getLength(), passing the array as an argument to the method.<br />
int length = args.length;<br />
// or<br />
int length2 = Array.getLength(args); </font></span><strong><span style="color:red;"><font face="Times New Roman">39.How can I speed up array accesses and turn off array bounds checking ? </font></span></strong><span><br />
<font face="Times New Roman">You cannot. It is part of the security architecture of the Java runtime to ensure never accessing invalid memory space. </font></span><strong><span style="color:red;"><font face="Times New Roman">40.How do I get the list of system properties that tell me things like which version of Java a user is running and their platform-specific line separator ?</font></span></strong><span><br />
<font face="Times New Roman">The System.getProperties() method will return the standard property set. However, in untrusted applets, you can only ask for specific properties, as in System.getProperty(”java.version”). </font></span><strong><span style="color:red;"><font face="Times New Roman">41.How do I sort an array ? </font></span></strong><span><br />
<font face="Times New Roman">The Arrays class in java.util provides a series of sort() methods for sorting arrays. If the array is an array of primitives or an array of a class that implements Comparable then you can just call the method directly: </font></span><span><font face="Times New Roman">Arrays.sort(theArray); </font></span><span><font face="Times New Roman">If, however, it is an array of objects that don’t implement the Comparable interface then you need to provide a custom Comparator to help you sort the elements in the array. </font></span><span><font face="Times New Roman">Arrays.sort(theArray, theComparator); </font></span><strong><span style="color:red;"><font face="Times New Roman">42.How can I implement a List (ordered collection) that keeps an index (i.e. a Map) of its contents ?</font></span></strong><span><br />
<font face="Times New Roman">You can’t. Each of the Map and List interfaces define a remove(Object o) method. Each method returns a different type (Map returns an Object while List returns a boolean). Because the compiler doesn’t permit overloaded methods that differ by only return type, you cannot create a class that implements both the List and Map interface. </font></span><span><font face="Times New Roman">If you need a Map that maintains insertion order, see the LinkedHashMap added in Java 1.4. </font></span><strong><span style="color:red;"><font face="Times New Roman">43.Is there a way to create a homogenous collection in Java? How do I make a collection where all the elements within it are a specific data type ?</font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">You can wait for Generics to be added to Java or… </font></span><span><font face="Times New Roman">You’d have to build one yourself. Basically, you’re creating a class that is a Proxy (Gang Of Four design pattern) around the actual Collection. </font></span><span><font face="Times New Roman">This works similarly to the way the (hidden) Synchronized versions in Collections work. They contain a reference to the original collection object that does the “real” work, but restrict access to it by synchronizing all the access methods. </font></span><span><font face="Times New Roman">What you’d be doing in this case is restricting access by keeping a reference to the Class object of the class you want to restrict your collection to contain, and throw IllegalArgumentExceptions whenever there’s an attempt to add an object not of that class. </font></span><span><font face="Times New Roman">There’s no way to enforce compile-time type safety in this manner. The API using “Object” references can’t be changed in Collection. </font></span><span><font face="Times New Roman">You can also wrap a collection by a Proxy that provides the alternative API that has type-safe method signatures. This is what’s done by the StringTokenizer API (which implements Enumeration, and wraps Enumeration with String versions of the same methods). </font></span><span><font face="Times New Roman">Otherwise, create your own class with its own signatures, and keep the Collection strictly private. This is best done if you are designing a class for others to use. </font></span><strong><span style="color:red;"><font face="Times New Roman">44.How does a Hashtable internally maintain the key-value pairs ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">The Hashtable class uses an internal (private) class named Entry to hold the key-value pairs. All entries of the Hashtable are stored in an array of Entry objects with the hash value of the key serving as the index. If two or more different keys have the same hash value these entries are stored as a linked list under the same index. </font></span><strong><span style="color:red;"><font face="Times New Roman">45.How do I look through each element of a HashMap ?</font></span></strong><span><br />
<font face="Times New Roman">To go through all the elements of a HashMap, or any class that implements the Map interface, call the entrySet() or keySet() methods than loop through what is returned. The entrySet() returns a group of Map.Entry elements, whereas the keySet() method just returns a Set of key elements. If you then want what the key refers to, you’d have to look them up.<br />
Once you have a Set to work with, you would then use an Iterator to go through all its elements. The following demonstrates: </font></span><span><font face="Times New Roman">Map map = some hash map<br />
Set set = map.keySet();<br />
Iterator it = set.iterator();<br />
while (it.hasNext()) {<br />
System.out.println(it.next());<br />
} </font></span><strong><span style="color:red;"><font face="Times New Roman">46.How do I create a read-only collection ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">The Collections class has six methods to help out here:<br />
unmodifiableCollection(Collection c)<br />
unmodifiableList(List list)<br />
unmodifiableMap(Map m)<br />
unmodifiableSet(Set s)<br />
unmodifiableSortedMap(SortedMap m)<br />
unmodifiableSortedSet(SortedSet s) </font></span><span><font face="Times New Roman">If you then get an Iterator from one of these unmodifiable collections, when you call remove() it will throw an UnsupportedOperationException </font></span><strong><span style="color:red;"><font face="Times New Roman">47.How can I process through the keys of a Hashtable in sorted order ? </font></span></strong><strong><span><br />
</span></strong><span><font face="Times New Roman">In order to get all the keys for a Hashtable, you use the keys() method to get an Enumeration or the keySet() method to get a Set. If you are using Java 2, and can use the collections framework, what you should do is get the key set of the Hashtable and create a TreeSet from it. You can then get an iterator() from the created TreeSet that will have the keys in order. If you can’t use the collections framework, you’ll have the sort the Enumeration you get back from keys() yourself. </font></span><strong><span style="color:red;"><font face="Times New Roman">48.Which collections in Java are synchronized and which aren’t ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">The original collection classes in Java are all synchronized: Vector and Hashtable, along with their subclasses Stack and Properties. Those classes introduced with the Java 2 Collections Framework are all NOT synchronized by default, the sets, lists, and maps </font></span><strong><span style="color:red;"><font face="Times New Roman">49.What are the differences between ArrayList and LinkedList ?</font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">An ArrayList is a List implementation backed by a Java array, similar to the Vector class. As the number of elements in the collection increases, the internal array grows to fit them. If there are lots of growth periods, performance degrades as the old array needs to be copied into the new array. However, random access is very quick as it uses an array index to access.<br />
With a LinkedList, the List implementation is backed by a doubly linked list data structure, allowing easy inserts/deletions anywhere in the structure, but really slow random accesses as the access must start at an end to get to the specific position. </font></span><span><font face="Times New Roman">Which you use really depends on the type of operations you need to support. </font></span><font face="Times New Roman"><strong><span style="color:red;">50.How do I read input from a stream “one word at a time” ?           </span></strong><span>What you need to do is use the java.util.StringTokenizer or java.io.StreamTokenizer to parse your input into words. Each has a default set of delimiters like white space that can be changed. The following demonstrates the using of StringTokenizer to count words in a file. </span></font><span><font face="Times New Roman">import java.io.*;<br />
import java.util.*; </font></span><span><font face="Times New Roman">public class Test {<br />
static final Integer ONE = new Integer(1); </font></span><span><font face="Times New Roman">public static void main (String args[])<br />
throws IOException { </font></span><span><font face="Times New Roman">Map map = new TreeMap();<br />
FileReader fr = new FileReader(args[0]);<br />
BufferedReader br = new BufferedReader(fr);<br />
String line;<br />
while ((line = br.readLine()) != null) {<br />
processLine(line, map);<br />
}<br />
printMap(map);<br />
}<br />
static void processLine(String line, Map map) {<br />
StringTokenizer st = new StringTokenizer(line);<br />
while (st.hasMoreTokens()) {<br />
addWord(map, st.nextToken());<br />
}<br />
}<br />
static void addWord(Map map, String word) {<br />
Object obj = map.get(word);<br />
if (obj == null) {<br />
map.put(word, ONE);<br />
} else {<br />
int i = ((Integer)obj).intValue() + 1;<br />
map.put(word, new Integer(i));<br />
}<br />
}<br />
static void printMap(Map map) {<br />
Set set = map.entrySet();<br />
Iterator it = set.iterator();<br />
while (it.hasNext()) {<br />
Map.Entry entry = (Map.Entry)it.next();<br />
System.out.println(entry.getKey() +<br />
“: ” + entry.getValue());<br />
}<br />
}<br />
} </font></span><strong><span style="color:red;"><font face="Times New Roman">51.Do the keys() and elements() methods of a Hashtable enumerate things in the same order ?</font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">There is no requirement that the elements are returned in the same order, only that all of them are returned in the Enumeration. Your best bet for getting the element for a key is to loop through the keys() returned and fetch the element for each. Or, if you can use a HashMap instead of a Hashtable, work with the Map.Entry that is returned by the keySet(). This includes both the key and value together, not requiring a separate lookup. </font></span><strong><span style="color:red;"><font face="Times New Roman">52.How do I treat an object I get out of a Vector (collection) as the type I put into it  ?</font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">When you get an object out of a Vector (or any collection), the object is returned as being of type Object. You need to cast it back into the object type you put into the data structure if you need to call or treat the object as the original type.<br />
For instance, if you add an array to a vector: </font></span><span><font face="Times New Roman">String args[] = {”1″, “2″, “3″};<br />
Vector v = new Vector();<br />
v.addElement(args); </font></span><span><font face="Times New Roman">Then, when you get the object out of the vector, you need to cast it back to the original type:<br />
String args2[] = (String[])v.firstElement();<br />
System.out.println(args2.length); </font></span><strong><span style="color:red;"><font face="Times New Roman">53.What is the difference between a singly linked list and doubley linked list ?</font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">A singly linked list is one that has only a pointer/reference to the next element in the list. A doubley linked list has both a previous and next pointer/reference. </font></span><strong><span style="color:red;"><font face="Times New Roman">54.What is meant by natural ordering of objects in the context of the collections framework ?</font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">Java documentation refers to the natural ordering of objects when describing various algorithms and data structures in the collections framework. Object ordering is obviously needed by the sorting algorithms. It is also needed by the specifications of the interfaces such as SortedSet, SortedMap, etc., and the data structures used for container classes such as TreeSet, TreeMap, etc. Unless instructed otherwise via a Comparator object supplied as an argument to the constructor, the default behavior of a class such as TreeSet is to store its objects in an ascending natural order.<br />
The objects of a class exhibit natural ordering if the class has implemented the java.lang.Comparable interface. Such a class must provide an implementation for the compareTo method — referred to as the class’s natural comparison method — that can then be used by the algorithms and the data structures for comparing data objects. The compareTo method must return a negative integer, a zero, or a positive integer if the object on which it is invoked is less than, equal to, or greater than the argument object. </font></span><span><font face="Times New Roman">It is strongly recommended that a class’s natural ordering as dictated by the implementation of the compareTo method be consistent with equals. This consistency is achieved if and only if e1.compareTo( (Object) e2 ) == 0 has the same boolean value as e1.equals( (Object) e2 ) for every pair of objects e1 and e2 of the class. Lack of this consistency could elicit strange behavior from the data structures that need to compare objects. </font></span><span><font face="Times New Roman">Many of the system supplied classes possess natural ordering. These include String, Integer, Float, Double, Date, File and many others. For the String class, the natural order is lexicographic; it is chronological for the Date class; lexicographic on the pathname for the File class, etc. </font></span><strong><span style="color:red;"><font face="Times New Roman">55.What is a fail-fast iterator ?</font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">An iterator is considered fail-fast if it throws a ConcurrentModificationException under either of the following two conditions:<br />
In multithreaded processing: if one thread is trying to modify a Collection while another thread is iterating over it. </font></span><span><font face="Times New Roman">In single-threaded or in multithreaded processing: if after the creation of the Iterator, the container is modified at any time by any method other than the Iterator’s own remove or add methods.<br />
Note in particular what is implied by the second condition: After we create a container’s iterator, during a loop that iterates over the container we must only use the remove (and when applicable add) methods defined for the iterator and that we must NOT use the same methods defined for the container itself. To illustrate this point, suppose we declare and initialize a List in the following manner<br />
List list = new ArrayList();<br />
list.add(”Peter”);<br />
list.add(”Paul”);<br />
list.add(”Mary”); </font></span><span><font face="Times New Roman">Let’s say we wish to iterate over this list. We’d need to declare a ListIterator as follows:<br />
ListIterator iter = list.listIterator(); </font></span><span><font face="Times New Roman">Having created this iterator, we could now set up a loop like:<br />
while(iter1.hasNext()){<br />
String str = iter1.next();<br />
// do something with str<br />
} </font></span><span><font face="Times New Roman">Because iter is fail-fast, we are not allowed to invoke List’s add or remove methods inside the loop. Inside the loop, we are only allowed to use ListIterator’s add and remove methods. This makes sense because it is the Iterator object that knows where it is in a List as the List is being scanned. The List object itself would have no idea of that.<br />
The Iterators supported by all the work-horse container classes, such as ArrayList, LinkedList, TreeSet, and HashSet, are fail-fast. The Iterator type retrofitted to the older container class Vector is also fail-fast. For associative containers, such as HashMap and the older HashTable, the Iterator type for the Collections corresponding to either the keys or the values or the &lt;key, value&gt; pairs are fail-fast with respect to the container itself. That means that even if you are iterating over, say, just the keys of the container, any illegal concurrent modifications to the underlying container would be detected. </font></span><span><font face="Times New Roman">One final note regarding iterators versus enumerations: It is also possible to use an Enumeration object returned by the elements() method for iterating over the older container types such as Vector. However, Enumerations do not provide a fail-fast method. On the other hand, the more modern Iterator returned by a Vector’s iterator() and listIterator() methods are fail-fast. Hence, iterators are recommended over enumerations for iterating over the elements of the older container types. </font></span><strong><span style="color:red;"><font face="Times New Roman">56.How do you sort the elements of a vector ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">Since the Vector class implements the List interface, you can call the Collections.sort() method to sort the elements in place. You can also manually insert elements into a Vector to keep the vector sorted. Of course, if keeping sorted access is such a big deal, you should consider using a different, more appropriate data structure like a TreeSet.<br />
For 1.1 Java users, you’ll need to sort the elements yourself. There is no built-in support for this. A better option might be to sort the elements as you insert each element. </font></span><strong><span style="color:red;"><font face="Times New Roman">57.What is a WeakHashMap? What is its use and when should you use it ?</font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">A WeakHashMap is a special Map implementation where the keys of the map are stored in a java.lang.ref.WeakReference. By storing the keys in a weak reference, key-value pairs can dynamically be dropped from the map when the only reference to the key is from the weak reference. This makes the WeakHashMap an excellent implementation for a weakly referenced list, where entries that aren’t used elsewhere may be dropped with no side effects. Also, just because a key may be dropped, doesn’t mean it will immediately be dropped. If the system has sufficient resources, the weak key reference that isn’t externally referenced could stay around for a long time. </font></span><strong><span style="color:red;"><font face="Times New Roman">58.What is the function of a load factor in a Hashtable ?</font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">The load factor determines how full a hashtable may get before it expands its capacity. I think that the comments on the Hashtable API docs explain it very well:<br />
The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When the number of entries in the hashtable exceeds the product of the load factor and the current capacity, the capacity is increased by calling the rehash method. </font></span><span><font face="Times New Roman">Generally, the default load factor (.75) offers a good tradeoff between time and space costs. Higher values decrease the space overhead but increase the time cost to look up an entry (which is reflected in most Hashtable operations, including get and put). </font></span><strong><span style="color:red;"><font face="Times New Roman">59.How do you control growth of vectors when their internal arrays are full ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">The vector constructor can include either an initial capacity or a capacity and growth increment. When not specified, the initial size of the vector is 10 and growth will double when necessary. Otherwise, initialize size and growth will grow when needed as specified by the arguments to the constructor.<br />
If the argument to the constructor is a collection, the initial size of the internal structure is 10% larger than the collection. Since there is no second argument to control growth, the capacity will double when necessary. </font></span><strong><span style="color:red;"><font face="Times New Roman">60.How to sort the messages in JavaMail ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">Within the JavaMail classes there is no support for this. However, once you get the array of messages back from a folder, you can call the Arrays.sort() method in the collections framework to sort the messges. Since MimeMessage doesn’t implement Comparable, you’ll need to provide your own Comparator specifying how you want the messages to be sorted. </font></span><strong><span style="color:red;"><font face="Times New Roman">61.When did Strings start caching their hash codes ?</font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">Starting with the 1.3 release of Java, the java.lang.String class will only calculate the hashcode once, when its first needed. Future calls to hashCode() will return the previously calculated value. </font></span><strong><span style="color:red;"><font face="Times New Roman">62.How can you retrieve the Hashtable’s load factor ? </font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">There is no public interface to access the load factor setting.<br />
Some choices you can do to expose this value… </font></span><span><font face="Times New Roman">Subclass Hashtable and add a read-only accessor method<br />
Use reflection to access the private field (only possible in trusted environment)<br />
Serialize the Hashtable and get the value from the stream (when reading back the stream, you’ll have to figure out where the appropriate field is) </font></span><strong><span style="color:red;"><font face="Times New Roman">63.Why can’t I add a collection to itself ?</font></span></strong><strong><span><br />
</span></strong><span><font face="Times New Roman">This will cause a stack overflow exception to be generated on calls to methods like toString() and hashCode(), which recursively call the method on the elements of the collection.</font></span><span><font face="Times New Roman"> </font></span><span><font face="Times New Roman"> </font></span><span><font face="Times New Roman"> </font></span><span><font face="Times New Roman"> </font></span><span><font face="Times New Roman"> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2007/05/10/ant-drill-one/"><font face="Times New Roman">ANT drill - One</font></a></span></h3>
<p><span><font face="Times New Roman">  </font></span><font face="Times New Roman"><strong><span style="color:red;">What is ant?</span></strong><span> </span></font><span><font face="Times New Roman">Ant is a small animal who can build magnificent buildings. Ant builds!<br />
ANT is a Java based building tool, which is similar to make, and so much better than make.<br />
ANT, what a smart name for a building tool, even the original author of ANT, James Duncan Davidson, meant “Another Neat Tool”. </font></span><font face="Times New Roman"><strong><span style="color:red;">A win-win ant learning method</span></strong><span> </span></font><span><font face="Times New Roman">There is a shortcut.<br />
If you download a small jakarta project, such as Log4J, which is built by ant. It is a good and simple example for you to learn ant. Actually, you hit two birds with one stone.<br />
Ant is easy!<br />
The hard part is how to make a very complicated diversified system work very simple and elegant. Knowledge about ant is not enough, you need an elegant and simple design, you need great naming convention, you need to optimize the code reusability and flexibility, you need a least maintenance system…<br />
Then it is not easy now .. </font></span><font face="Times New Roman"><strong><span style="color:red;">How do I get started to use ant? Can you give me a “Hello World” ant script?</span></strong><span> </span></font><span><font face="Times New Roman">Simple.<br />
    * Download the most recent version of ant from Apache; unzip it some where on your machine.<br />
    * Install j2sdk 1.4 or above.<br />
    * Set JAVA_HOME and ANT_HOME to the directory your installed them respectively.<br />
    * Put %JAVA_HOME%/bin;%ANT_HOME%/bin on your Path. Use ${JAVA_HOME}/bin:${ANT_HOME}/bin on UNIX. Yes, you can use forward slash on windows.<br />
    * Write a “Hello world” build.xml<br />
                  &lt;project name=”hello” default=”say.hello” basedir=”.” &gt;<br />
                    &lt;property name=”hello.msg” value=”Hello, World!” /&gt;<br />
                    &lt;target name=”say.hello” &gt;<br />
                      &lt;echo&gt;${hello.msg}&lt;/echo&gt;<br />
                    &lt;/target&gt;<br />
                  &lt;/project&gt;<br />
    * Type ant in the directory your build.xml located.<br />
    * You are ready to go!!!! </font></span><strong><span style="color:red;"><font face="Times New Roman">How to delete files from a directory if it exists? The following code fails when directory does not exist!</font></span></strong><span><br />
<font face="Times New Roman">&lt;delete quiet=”true” dir=”${classes.dir}” includes=”*.class”/&gt; </font></span><span><font face="Times New Roman">Your code has many problems.<br />
   1. You should not use implicit fileset, which is deprecated. You should use nested fileset.<br />
   2. If dir does not exist, the build will fail, period!<br />
   3. If you are not sure, use a upper level dir, which exists for sure. See the following fileset.<br />
  &lt;delete&gt;<br />
    &lt;fileset dir=”${upperdir.which.exists}”&gt;<br />
      &lt;include name=”${classes.dir}/*.class” /&gt;<br />
    &lt;/fileset&gt;<br />
  &lt;/delete&gt; </font></span><font face="Times New Roman"><strong><span style="color:red;">How do I set classpath in ant?</span></strong><span> </span></font><span><font face="Times New Roman">Here is some snippet of code<br />
  &lt;path id=”build.classpath”&gt;<br />
    &lt;fileset dir=”${build.lib}” includes=”**/*.jar”/&gt;<br />
    &lt;fileset dir=”${build.classes}” /&gt;<br />
  &lt;/path&gt;<br />
  &lt;target….&gt;<br />
    &lt;javac ….&gt;<br />
      &lt;classpath refid=”build.classpath” /&gt;<br />
    &lt;/java&gt;<br />
  &lt;/target&gt;<br />
  &lt;target….&gt;<br />
    &lt;java ….&gt;<br />
      &lt;classpath refid=”build.classpath” /&gt;<br />
    &lt;/java&gt;<br />
  &lt;/target&gt; </font></span><font face="Times New Roman"><strong><span style="color:red;">How does ant read properties? How to set my property system?</span></strong><span> </span></font><span><font face="Times New Roman">Ant sets properties by order, when something is set, the later same properties cannot overwrite the previous ones. This is opposite to your Java setters.<br />
This give us a good leverage of preset all properties in one place, and overwrite only the needed. Give you an example here. You need password for a task, but don’t want to share it with your team members, or not the developers outside your team.<br />
Store your password in your ${user.home}/prj.properties<br />
pswd=yourrealpassword<br />
In your include directory master prj.properties<br />
pswd=password<br />
In your build-common.xml read properties files in this order<br />
   1. The commandline will prevail, if you use it: ant -Dpswd=newpassword<br />
   2. ${user.home}/prj.properties (personal)<br />
   3. yourprojectdir/prj.properties (project team wise)<br />
   4. your_master_include_directory/prj.properties (universal)<br />
&lt;cvsnttask password=”${pswd} … /&gt;<br />
Problem solved! </font></span><font face="Times New Roman"><strong><span style="color:red;">How to modify properties in ant?</span></strong><span> </span></font><span><font face="Times New Roman">No, you can’t!<br />
Properties in Ant are immutable. There is a good reason behind this, see this FAQ item for more details.<br />
Q. How to use ant-contrib tasks?<br />
A: Simple, just copy ant-contrib.jar to your ant*/lib directory<br />
And add this line into your ant script, all ant-contrib tasks are now available to you!<br />
    &lt;taskdef resource=”net/sf/antcontrib/antcontrib.properties” /&gt; </font></span><font face="Times New Roman"><strong><span style="color:red;">How to loop on a list or fileset?</span></strong><span> </span></font><span><font face="Times New Roman">Use ant-contrib &lt;for&gt; &lt;foreach&gt; tasks<br />
General to say, use &lt;for&gt; is better than use &lt;foreach&gt; since for each is actually open another ant property space, use more memory too. </font></span><font face="Times New Roman"><strong><span style="color:red;">Why do I get en exception when I use location=”D:\\Code\\include” as attribute of includepath?</span></strong><span> </span></font><span><font face="Times New Roman">See here.<br />
You need to escape the string to “D:\\\\Code\\\\include” or use “D:/Code/include” instead!<br />
Believe me or not? Forward slash works on windows in all ant or java code. It also works in windows environment variables. It does not work in cmd (dos) window before XP. It also works in XP dos window now! </font></span><font face="Times New Roman"><strong><span style="color:red;">Can I put the contents of a classpath or fileset into a property?</span></strong><span> </span></font><span><font face="Times New Roman">Yes, you can.<br />
This is very similar to the call of Java class toString() method and actually it is calling the toString() method inside ant. For example<br />
&lt;fileset id=”fs1″ dir=”t1″ includes=”**/*.java”/&gt;<br />
&lt;property name=”f1.contents” refid=”fs1″/&gt;<br />
&lt;echo&gt;f1.contents=${f1.contents}&lt;/echo&gt; </font></span><font face="Times New Roman"><strong><span style="color:red;">Where can I find the javadoc for ant API?</span></strong><span> </span></font><span><font face="Times New Roman">Download apache ant src version. Use ant javadocs command to see generated javadoc for ant in build/docs directory. </font></span><span><font face="Times New Roman">  </font></span><font face="Times New Roman"><strong><span style="color:red;">How can I use ant to run a Java application?</span></strong><span> </span></font><span><font face="Times New Roman">Here is a real world example.<br />
  &lt;target name=”run” depends=”some.target,some.other.target”&gt;<br />
    &lt;java classname=”${run.class}” fork=”yes”&gt;<br />
      &lt;classpath&gt;<br />
        &lt;path refid=”classpath” /&gt;<br />
      &lt;/classpath&gt;<br />
      &lt;jvmarg line=”${debug.jvmargs}” /&gt;<br />
      &lt;jvmarg line=”${my.jvmargs}” /&gt;<br />
      &lt;jvmarg value=”-Dname=${name}” /&gt;<br />
      &lt;jvmarg line=”${run.jvmargs}” /&gt;<br />
      &lt;arg line=”${run.args}” /&gt;<br />
    &lt;/java&gt;<br />
  &lt;/target&gt; </font></span><font face="Times New Roman"><strong><span style="color:red;">How to use ant to run commandline command? How to get perl script running result?</span></strong><span> </span></font><span><font face="Times New Roman">Use exec ant task.<br />
Don’t forget ant is pure Java. That is why ant is so useful, powerful and versatile. If you want ant receive unix command and result, you must think Unix. So does in MS-Windows. Ant just helps you to automate the process. </font></span><font face="Times New Roman"><strong><span style="color:red;">How do I debug my ant script?</span></strong><span> </span></font><span><font face="Times New Roman">Many ways<br />
    * Do an echo on where you have doubt. You will find out what is the problem easily. Just like the old c printf() or Java System.println()<br />
    * Use project.log(”msg”) in your javascript or custom ant task<br />
    * Run Ant with -verbose, or even -debug, to get more information on what it is doing, and where. However, you might be tired with that pretty soon, since it give you too much information. </font></span><font face="Times New Roman"><strong><span style="color:red;">How to exclude multi directories in copy or delete task?</span></strong><span> </span></font><span><font face="Times New Roman">Here is an example.<br />
  &lt;copy todir=”${to.dir}” &gt;<br />
    &lt;fileset dir=”${from.dir}” &gt;<br />
      &lt;exclude name=”dirname1″ /&gt;<br />
      &lt;exclude name=”dirname2″ /&gt;<br />
      &lt;exclude name=”abc/whatever/dirname3″ /&gt;<br />
      &lt;exclude name=”**/dirname4″ /&gt;<br />
    &lt;/fileset&gt;<br />
  &lt;/copy&gt; </font></span><font face="Times New Roman"><strong><span style="color:red;">How to use Runtime in ant?</span></strong><span> </span></font><span><font face="Times New Roman">You don’t need to use Runtime etc. Ant have exec task.<br />
The class name is org.apache.tools.ant.taskdefs.ExecTask. You can create the task by using the code in your customized ant Task.<br />
ExecTask compile = (ExecTask)project.createTask(”exec”); </font></span><font face="Times New Roman"><strong><span style="color:red;">How to rearrange my directory structure in my jar/war/ear/zip file? Do I need to unarchive them first?</span></strong><span> </span></font><span><font face="Times New Roman">No, you don’t need to unarchive them first.<br />
    * You don’t need to unzip the files from archive to put into your destination jar/ear/war files.<br />
    * You can use zipfileset in your jar/war/ear task to extract files from old archive to different directory in your new archive.<br />
    * You also can use zipfileset in your jar/war/ear task to send files from local directory to different directory in your new archive.<br />
See the follow example:<br />
  &lt;jar destfile=”${dest}/my.jar”&gt;<br />
    &lt;zipfileset src=”old_archive.zip” includes=”**/*.properties” prefix=”dir_in_new_archive/prop”/&gt;<br />
    &lt;zipfileset dir=”curr_dir/abc” prefix=”new_dir_in_archive/xyz”/&gt;<br />
  &lt;/jar&gt; </font></span><strong><span style="color:red;"><font face="Times New Roman">Why did I get such warning in ant?</font></span></strong><span><br />
<font face="Times New Roman">compile:<br />
[javac] Warning: commons-logging.properties modified in the future.<br />
[javac] Warning: dao\\DAO.java modified in the future.<br />
[javac] Warning: dao\\DBDao2.java modified in the future.<br />
[javac] Warning: dao\\HibernateBase.java modified in the future. </font></span><span><font face="Times New Roman">System time problem, possible reasons:<br />
    * You changed the system time<br />
    * I had the same problem before, I checked out files from cvs to windows, and transfer them to a unix machine, somehow, I got huge amount of such warnings because the system timing issue.<br />
    * If you transfer files from Australia/China/India to the United States, you will get the problem too. True enough, I did and met the problem once. </font></span><font face="Times New Roman"><strong><span style="color:red;">How can I write my own ant task?</span></strong><span> </span></font><span><font face="Times New Roman">Easy!<br />
Writing Your Own Task How-To from ant.<br />
In your own $ANT_HOME/docs/manual directory, there also is tutorial-writing-tasks-src.zip<br />
Use them! Use taskdef to define it in your script, define it before using it. </font></span><font face="Times New Roman"><strong><span style="color:red;">How to copy files without extention?</span></strong><span> </span></font><span><font face="Times New Roman">If files are in the directory:<br />
&lt;include name=”a,b,c”/&gt;<br />
If files are in the directory or subdirectories:<br />
&lt;include name=”**/a,**/b,**/c”/&gt;<br />
If you want all files without extension are in the directory or subdirectories:<br />
&lt;exclude name=”**/*.*”/&gt; </font></span><font face="Times New Roman"><strong><span style="color:red;">How do I use two different versions of jdk in ant script?</span></strong><span> </span></font><span><font face="Times New Roman">The followings are what I’m doing.<br />
   1. Don’t define java.home by yourself. Ant uses an internal one derived from your environment var JAVA_HOME. It is immutable.<br />
   2. I do the followings:<br />
          * In my build.properties (read first)<br />
            jdk13.bin=${tools.home}/jdk1.3.1_13/bin<br />
            jdk14.bin=${tools.home}/j2sdk1.4.2_08/bin/<br />
          * In my master properties file (read last), set default<br />
            javac.location=${jdk13.bin}<br />
          * In my prj.properties, if I need to use 1.4<br />
            javac.location=${jdk14.bin}<br />
          * in my javac task<br />
            executable=”${javac.location}/javac.exe” </font></span><font face="Times New Roman"><strong><span style="color:red;">How to pass -Xlint or -Xlint:unchecked to 1.5 javac task?</span></strong><span> </span></font><span><font face="Times New Roman">pass it as compilerarg nested &lt;compilerarg&gt; to specify.<br />
  &lt;compilerarg value=”-Xlint”/&gt;<br />
  &lt;!– or –&gt;<br />
  &lt;compilerarg value=”-Xlint:unchecked”/&gt; </font></span><font face="Times New Roman"><strong><span style="color:red;">Can you give me a simple ant xslt task example?</span></strong><span> </span></font><span><font face="Times New Roman">Here is a working one!<br />
    &lt;xslt style=”${xslfile}” in=”${infile}” out=”${outfile}” &gt;<br />
      &lt;classpath&gt;<br />
        &lt;fileset dir=”${xml.home}/bin”<br />
          includes=”*.jar” /&gt;<br />
      &lt;/classpath&gt;<br />
    &lt;/xslt&gt; </font></span><font face="Times New Roman"><strong><span style="color:red;">How to hide password input?</span></strong><span> </span></font><span><font face="Times New Roman">Override ant Input task.<br />
Response every user input with a backspace. Not the best, but it works </font></span><font face="Times New Roman"><strong><span style="color:red;">How do I add elements to an existing path dynamically?</span></strong><span> </span></font><span><font face="Times New Roman">Yes, it is possible. However, you need to write a custom ant task, get the path, and add/modify it, and put it in use. What I am doing is that I define a path reference lib.classpath, then add/modify the lib.classpath use my own task. </font></span><font face="Times New Roman"><strong><span style="color:red;">How to do conditional statement in ant?</span></strong><span> </span></font><span><font face="Times New Roman">There are many ways to solve the problem.<br />
    * Since target if/unless all depend on some property is defined or not, you can use condition to define different NEW properties, which in turn depends on your ant property values. This makes your ant script very flexible, but a little hard to read.<br />
    * Ant-contrib has &lt;if&gt; &lt;switch&gt; tasks for you to use.<br />
    * Ant-contrib also has &lt;propertyregex&gt; which can make very complicate decisions. </font></span><font face="Times New Roman"><strong><span style="color:red;">Can I change/override ant properties when I use ant-contrib foreach task?</span></strong><span> </span></font><span><font face="Times New Roman">&lt;foreach&gt; is actually using a different property space, you can pass any property name/value pair to it. Just use &lt;param&gt; nested tag inside foreach </font></span><font face="Times New Roman"><strong><span style="color:red;">How to let auto-detect platform and use platform specific properties?</span></strong><span> </span></font><span><font face="Times New Roman">Tell you a great trick, it works excellent.<br />
In your major build-include.xml, put in this line<br />
  &lt;property file=”${antutil.includes}/${os.name}-${os.arch}.properties” /&gt;<br />
This will auto-detect your platform, and you write one file for each environment specific variables. For example: HP-UX-PA_RISC2.0.properties SunOS-sparc.properties Windows XP-x86.properties … They work great!!! </font></span><font face="Times New Roman"><strong><span style="color:red;">How to make ant user interactive? I tried to use BufferedReader to get user input, it hangs.</span></strong><span> </span></font><span><font face="Times New Roman">See here.<br />
Use this class TimedBufferedReader instead of your BufferedReader will work. This is a working one in our installation process. The original author is credited in the code. I’ve made some improvement.<br />
TimedBufferedReader.java<br />
package setup;<br />
import java.io.Reader;<br />
import java.io.BufferedReader;<br />
import java.io.IOException;<br />
/**<br />
 * Provides a BufferedReader with a readLine method that<br />
 * blocks for only a specified number of seconds. If no<br />
 * input is read in that time, a specified default<br />
 * string is returned. Otherwise, the input read is returned.<br />
 * Thanks to Stefan Reich<br />
 * for suggesting this implementation.<br />
 * @author: Anthony J. Young-Garner<br />
 * @author: Roseanne Zhang made improvement.<br />
 */<br />
public class TimedBufferedReader extends BufferedReader<br />
{<br />
  private int    timeout    = 60; // 1 minute<br />
  private String defaultStr = “”;<br />
  /**<br />
   * TimedBufferedReader constructor.<br />
   * @param in Reader<br />
   */<br />
  TimedBufferedReader(Reader in)<br />
  {<br />
    super(in);<br />
  }<br />
  /**<br />
   * TimedBufferedReader constructor.<br />
   * @param in Reader<br />
   * @param sz int Size of the input buffer.<br />
   */<br />
  TimedBufferedReader(Reader in, int sz)<br />
  {<br />
    super(in, sz);<br />
  }<br />
  /**<br />
   * Sets number of seconds to block for input.<br />
   * @param seconds int<br />
   */<br />
  public void setTimeout(int timeout)<br />
  {<br />
    this.timeout=timeout;<br />
  }<br />
  /**<br />
   * Sets defaultStr to use if no input is read.<br />
   * @param str String<br />
   */<br />
  public void setDefaultStr(String str)<br />
  {<br />
    defaultStr = str;<br />
  }<br />
  /**<br />
   * We use ms internally<br />
   * @return String<br />
   */<br />
  public String readLine() throws IOException<br />
  {<br />
    int waitms = timeout*1000;<br />
    int ms     = 0;<br />
    while (!this.ready())<br />
    {<br />
      try<br />
      {<br />
        Thread.currentThread().sleep(10);<br />
        ms += 10;<br />
      }<br />
      catch (InterruptedException e)<br />
      {<br />
        break;<br />
      }<br />
      if (ms &gt;= waitms)<br />
      {<br />
          return defaultStr;<br />
      }<br />
    }<br />
    return super.readLine();<br />
  }<br />
} </font></span><font face="Times New Roman"><strong><span style="color:red;">What is a good directory structure for main code and junit test code?</span></strong><span> </span></font><span><font face="Times New Roman">Dev<br />
    |_src<br />
    |  |_com<br />
    |     |_mycom<br />
    |        |_mypkg<br />
    |           |_A.java<br />
    |_test<br />
       |_src<br />
         |_com<br />
            |_mycom<br />
               |_mypkg<br />
                  |_ATest.java</font></span><span><font face="Times New Roman"> </font></span><font size="2"><font face="Verdana"> </font></font><font size="2"><font face="Verdana"> </font></font><font size="2"><font face="Verdana"> </font></font><font size="2"><font face="Verdana"> </font></font><font size="2"><font face="Verdana"> </font></font><font size="2"><font face="Verdana"> </font></font><font size="2"><font face="Verdana"> </font></font><font size="2"><font face="Verdana"> </font></font><font size="2"><font face="Verdana"> </font></font><font size="2"><font face="Verdana"> </font></font></p>
<h3><span><font face="Times New Roman"> </font></span></h3>
<h3><span><a href="http://sharat.wordpress.com/2007/04/12/design-patterns-drill/"><font face="Times New Roman">Design Patterns : Drill</font></a></span></h3>
<p><strong><span style="color:red;"><font face="Times New Roman">What is a design pattern.</font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">————————————–<br />
“Design patterns are recurring solutions to design problems you see over<br />
Alpert, et. al., 1998).<br />
“Design patterns constitute a set of rules describing how to accomplish<br />
certain tasks in the realm of software development.” (Pree, 1994)</font></span><strong><span style="color:red;"><font face="Times New Roman">How many catogeries are there in Java patterns.</font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">————————————————–<br />
<strong>Creational patterns</strong> are ones that create objects for you, rather than<br />
having you instantiate objects directly. This gives your program more<br />
flexibility in deciding which objects need to be created for a given case.<br />
<strong>Structural patterns</strong> help you compose groups of objects into larger<br />
structures, such as complex user interfaces or accounting data.<br />
<strong>Behavioral patterns</strong> help you define the communication between objects<br />
in your system and how the flow is controlled in a complex program. </font></span><span><font face="Times New Roman">Design Patterns is a catalog of 23 generally useful patterns for writing objectoriented software.</font></span><strong><span style="color:red;"><font face="Times New Roman">Give me an example of Creational Patterns.</font></span></strong><span><br />
<font face="Times New Roman">——————————————————<br />
All of the creational patterns deal with the best way to create instances of objects. This is important because your program should not depend on how objects are created and arranged. In Java, of course, the simplest way to create an instance of an object is by using the new operator. </font></span><span><font face="Times New Roman">Sharat = new Sharat(); //instance of Fred class </font></span><span><font face="Times New Roman">However, this really amounts to hard coding how you create the object within your program. In many cases, the exact nature of the object that is created could vary with the needs of the program from time to time and abstracting the creation process into a special “creator” class can make your program more flexible and general. </font></span><strong><span style="color:blue;"><font face="Times New Roman">The Factory Pattern</font></span></strong><span><font face="Times New Roman"> is used to choose and return an instance of a class from a number of similar classes based on data you provide to the factory.<br />
<strong><span style="color:blue;">The Abstract Factory Pattern</span></strong> is used to return one of several groups of classes. In some cases it actually returns a Factory for that group of classes.<br />
<strong><span style="color:blue;">The Builder Pattern</span></strong> assembles a number of objects to make a new object, based on the data with which it is presented. Frequently, the choice of which way the objects are assembled is achieved using a Factory.<br />
<strong><span style="color:blue;">The Prototype Pattern</span></strong> copies or clones an existing class rather than creating a new instance when creating new instances is more expensive.<br />
<strong><span style="color:blue;">The Singleton Pattern</span></strong> is a pattern that insures there is one and only one instance of an object, and that it is possible to obtain global access to that one instance. </font></span><strong><span style="color:red;"><font face="Times New Roman">What is Singleton pattern. Where did you use it.</font></span></strong><span><br />
<font face="Times New Roman">——————————————————<br />
<strong><span style="color:blue;">Definition:</span></strong> Singleton pattern is used whenever we require a class to have only one instance throughout the application. Sometimes it’s appropriate to have exactly one instance of a class: If we need some data to be stored , used and manipulated the same instance by the whole application, we will use singleton pattern. </font></span></p>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">Ensure that only one instance of a class is created </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Provide a global point of access to the object </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Allow multiple instances in the future without affecting a singleton class’s clients </font></span></li>
</ul>
<p><font face="Times New Roman"><strong><span style="color:blue;">Examples:</span></strong><span></span></font></p>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">window managers, print spoolers, and filesystems are prototypical examples. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Search window in Microsoft word, even if multiple documents are open, only one search window will be opened.   </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">One important usage of singleton pattern is when you need to instantiate a subclass and check which class to be instantiated during run time. For example there are two subclasses one for production and one for development and we need to instantiate one during run time depending on the environment. </font></span></li>
</ul>
<p><font face="Times New Roman"><strong><span style="color:blue;">Syntax:</span></strong><span></span></font><span><font face="Times New Roman">public class ClassicSingleton {<br />
private static ClassicSingleton instance = null;<br />
protected ClassicSingleton() {<br />
// Exists only to defeat instantiation.<br />
}<br />
public static ClassicSingleton getInstance() {<br />
if(instance == null) {<br />
instance = new ClassicSingleton();<br />
}<br />
return instance;<br />
}<br />
The ClassicSingleton class maintains a static reference to the lone singleton instance and returns that reference from the static getInstance() method.</font></span><span><font face="Times New Roman">public class SingletonInstantiator {<br />
  public SingletonInstantiator() {<br />
   ClassicSingleton instance = ClassicSingleton.getInstance();<br />
<strong>ClassicSingleton anotherInstance =</strong><br />
<strong>new ClassicSingleton();</strong><br />
       …<br />
  }<br />
}<br />
<strong><span style="color:blue;">Explanation:</span></strong> In the first line we have created an instance of this class as null. We have declared a private constructor for the class to make sure that no object of class can be created outside the class.</font></span><span><font face="Times New Roman">getInstance() method is the entry point for the class which will return the only instance present for the class. This method is marked as static as it will be called directly from other classes. In this method we check if the instance of singletonExample is already present, if yes, return that instance else create the instance and return it. This makes sure that only one instance of the class is present at any point. </font></span><span><font face="Times New Roman">In the user class, that is calling our singleton class, we just need to call our singletonExample.getinstance() method which will return us the instance of singletonExample. </font></span><font face="Times New Roman"><strong><span style="color:blue;">Types:</span></strong><span> </span></font></p>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">we create an instance of singleton class only when it is called for the first time is called lazy instantiation. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Another variation can be creating the instance before it is called, this kind of instantiation is called eager instantiation. </font></span></li>
</ul>
<p><font face="Times New Roman"><strong><span style="color:blue;">Problems:</span></strong><span></span></font></p>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">A side effect of using singleton class: as the constructor of the singleton class is private, you cannot create a subclass of a singleton class, but in most of the cases that is desirable. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Additionally you can declare the class as final to explicitly show that the class can’t be subclassed. But in case if it is required to create a subclass of singleton class one can define the constructor of singleton class as protective instead of private. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Another point to note is that the singleton classes are not thread-safe. </font></span></li>
</ul>
<p><span><font face="Times New Roman">Making your singleton class thread-safe is easy, you just have to make your getinstance method synchronized. </font></span><span><font face="Times New Roman">Public synchronized static singletonExample getInstance()<br />
{<br />
//if instance is null create else return old instance<br />
} </font></span><strong><span style="color:red;"><font face="Times New Roman">What is Factory pattern. Explain it.</font></span></strong><span><br />
<font face="Times New Roman">—————————————————— </font></span></p>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">if we have a super class and n sub-classes, and based on data provided, we have to return the object of one of the sub-classes, we use a factory pattern. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">provides a simple decision making class which returns one of several possible subclasses of an abstract base class depending on data it is provided. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Usually all of the classes it returns have a common parent class and common methods,but each of them performs a task differently and is optimized for differentkinds of data. </font></span></li>
</ul>
<p><span><font face="Times New Roman">You should consider using a Factory pattern when<br />
· A class can’t anticipate which kind of class of objects it must create.<br />
· A class uses its subclasses to specify which objects it creates.<br />
· You want to localize the knowledge of which class gets created.</font></span><span><font face="Times New Roman">Needs to isolate concrete classes from their super classes. </font></span><span style="font-family:Symbol;">·</span><span><font face="Times New Roman"><span>  </span>A system needs independent of how its products are created, composed, and represented. </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2007/04/09/java-design-pattern/"><font face="Times New Roman">Java Design Pattern</font></a></span></h3>
<p><span><font face="Times New Roman">1.What is a design pattern.<br />
2.How many catogeries are there in Java patterns.<br />
3. How many patterns did you use in your project.<br />
4.Give me an example of Creational Patterns.<br />
5.What is Singleton pattern. Where did you use it.<br />
6.How do you use Factory pattern.<br />
7.Can explaing examples of Structural Patterns.<br />
8.What is difference between adapter pattern and decorator pattern.<br />
9.What is Facade Pattern. Give an example.<br />
10.Did you use proxy pattern.<br />
11.Give me some examples of Behavioral Patterns.<br />
12.What is Observer Pattern.<br />
13.Do you know the difference between Command pattern and interpreter pattern.<br />
14.What is Visitor Pattern.</font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2007/04/09/java-drill-2/"><font face="Times New Roman">Java Drill - 2</font></a></span></h3>
<p><span><font face="Times New Roman">1.Can a private method of a superclass be declared within a subclass?<br />
2.Why Java does not support multiple inheritence ?<br />
3.What is the difference between final, finally and finalize?<br />
4.Where and how can you use a private constructor.<br />
5.In System.out.println(),what is System,out and println,pls explain?<br />
6.What is meant by “Abstract Interface”?<br />
7.Can you make an instance of an abstract class? For example &#8211; java.util.Calender is an abstract class with a method getInstance() which returns an instance of the Calender class.<br />
8.What is the output of x&lt;y? a:b = p*q when x=1,y=2,p=3,q=4?<br />
9.Why Java does not support pointers?<br />
10.Parsers? DOM vs SAX parser<br />
11.What is the main difference between Java platform and other platforms?<br />
12.What is the Java Virtual Machine?<br />
13.What is the Java API?<br />
14.What is the package?<br />
15.What is native code?<br />
16.Is Java code slower than native code?<br />
17.What is the serialization?<br />
18.How to make a class or a bean serializable?<br />
19.How many methods in the Serializable interface?<br />
20.How many methods in the Externalizable interface?<br />
21.What is the difference between Serializalble and Externalizable interface?<br />
22.What is a transient variable?<br />
23.What is synchronization and why is it important?<br />
24.What are synchronized methods and synchronized statements?<br />
25.What are three ways in which a thread can enter the waiting state?<br />
26.Can a lock be acquired on a class?<br />
27.What is thread?<br />
28.What is multithreading?<br />
29.How does multithreading take place on a computer with a single CPU?<br />
30.What is the purpose of the wait(), notify(), and notifyAll() methods?<br />
31.What are the high-level thread states?<br />
32.What is the Collections API?<br />
33.How does Java handle integer overflows and underflows?<br />
34.What is the Vector class?<br />
35.If a method is declared as protected, where may the method be accessed?<br />
36.What is an Iterator interface?<br />
37.What is the difference between yielding and sleeping?<br />
38. Is sizeof a keyword?<br />
39.What are wrapped classes?<br />
40.What is the difference between preemptive scheduling and time slicing?<br />
41.How can you write a loop indefinitely?<br />
42.Can an anonymous class be declared as implementing an interface and extending a class?<br />
43.Which class is the superclass for every class.<br />
44. What is the difference between the Boolean &amp; operator and the &amp;&amp; operator?<br />
45.What is the GregorianCalendar class?<br />
46.What is the SimpleTimeZone class?<br />
47.What is the Properties class?<br />
48.What is the purpose of the Runtime class?<br />
49.What is the purpose of the System class?<br />
50.What is the purpose of the finally clause of a try-catch-finally statement?<br />
51.What must a class do to implement an interface?<br />
52.What is a static method?<br />
53.What is the difference between a static and a non-static inner class?<br />
54.What is an object’s lock and which object’s have locks?<br />
55.When can an object reference be cast to an interface reference?<br />
56.What’s the difference between J2SDK 1.5 and J2SDK 5.0?<br />
57.Does it matter in what order catch statements for FileNotFoundException and IOExceptipon are written?<br />
58.Can an inner class declared inside of a method access local variables of this method?<br />
59.What can go wrong if you replace &amp;&amp; with &amp; in the following code:<br />
String a=null; if (a!=null &amp;&amp; a.length()&gt;10) {…}<br />
60.What’s the main difference between a Vector and an ArrayList<br />
61.When should the method invokeLater()be used?<br />
62.How can a subclass call a method or a constructor defined in a superclass?<br />
63.What’s the difference between a queue and a stack?<br />
64.You can create an abstract class that contains only abstract methods. On the other hand, you can create an interface that declares the same methods. So can you use abstract classes instead of interfaces?<br />
65.What comes to mind when someone mentions a shallow copy in Java?<br />
66.What comes to mind when you hear about a young generation in Java?<br />
67.You are planning to do an indexed search in a list of objects. Which of the two Java collections should you use:<br />
ArrayList or LinkedList?<br />
68.How would you make a copy of an entire Java object with its state?<br />
69.There are two classes: A and B. The class B need to inform a class A when some important event has happened. What Java technique would you use to implement it?<br />
70.How can you minimize the need of garbage collection and make the memory use more effective?<br />
71.What access level do you need to specify in the class declaration to ensure that only classes from the same directory can access it?<br />
72.How do I deserilaize an Object?<br />
73.What are different types of inner classes ?<br />
74.Difference Between String and String Buffer?<br />
75.difference between verses (==) and .equals().</font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2007/04/09/java-drill-1/"><font face="Times New Roman">Java Drill - 1</font></a></span></h3>
<p><span><font face="Times New Roman">1.What is the difference between an Interface and an Abstract class?<br />
2.What is the purpose of garbage collection in Java, and when is it used?<br />
3.Describe synchronization in respect to multithreading.<br />
4.Explain different way of using thread?<br />
5.What are pass by reference and passby value?<br />
6.What is HashMap and Map?<br />
7.Difference between HashMap and HashTable?<br />
8.Difference between Vector and ArrayList?<br />
9.Difference between Swing and Awt?<br />
10.What is the difference between a constructor and a method?<br />
11.What is an Iterator?<br />
12.State the significance of public, private, protected, default modifiers both singly and in combination and state the effect of package relationships on declared items qualified by these modifiers.<br />
13.What is an abstract class?<br />
14.What is static in java?<br />
15.What is final?<br />
16.What if the main method is declared as private?<br />
17.What if the static modifier is removed from the signature of the main method?<br />
18.What if I write static public void instead of public static void?<br />
19.What if I do not provide the String array as the argument to the method?<br />
20.What is the first argument of the String array in main method?<br />
21.If I do not provide any arguments on the command line, then the String array of Main method will be empty or null?<br />
22.How can one prove that the array is not null but empty using one line of code?<br />
23.What environment variables do I need to set on my machine in order to be able to run Java programs?<br />
24.Can an application have multiple classes having main method?<br />
25.Can I have multiple main methods in the same class?<br />
26.Do I need to import java.lang package any time? Why ?<br />
27.Can I import same package/class twice? Will the JVM load the package twice at runtime?<br />
29.What are Checked and UnChecked Exception?<br />
30.What is Overriding?<br />
31.What are different types of inner classes?<br />
32.Are the imports checked for validity at compile time? e.g. will the code containing an import such as java.lang.ABCD compile?<br />
33.Does importing a package imports the subpackages as well? e.g. Does importing com.MyTest.* also import com.MyTest.UnitTests.*?<br />
34.What is the difference between declaring a variable and defining a variable?<br />
35.What is the default value of an object reference declared as an instance variable?<br />
36.Can a top level class be private or protected?<br />
37.What type of parameter passing does Java support?<br />
38.Primitive data types are passed by reference or pass by value?<br />
39.Objects are passed by value or by reference?<br />
40.What is serialization?<br />
41.How do I serialize an object to a file?<br />
42.Which methods of Serializable interface should I implement?<br />
43.How can I customize the seralization process? i.e. how can one have a control over the serialization process?<br />
44.What is the common usage of serialization?<br />
45.What is Externalizable interface?<br />
46.When you serialize an object, what happens to the object references included in the object?<br />
47.What one should take care of while serializing the object?<br />
48.What happens to the static fields of a class during serialization?<br />
49.Does Java provide any construct to find out the size of an object?<br />
50.Give a simplest way to find out the time a method takes for execution without using any profiling tool?<br />
51.What are wrapper classes?<br />
52.Why do we need wrapper classes?<br />
53.What are checked exceptions?<br />
54.What are runtime exceptions?<br />
55.What is the difference between error and an exception??<br />
56.How to create custom exceptions?<br />
57.If I want an object of my class to be thrown as an exception object, what should I do?<br />
58.If my class already extends from some other class what should I do if I want an instance of my class to be thrown as an exception object?<br />
59.How does an exception permeate through the code?<br />
60.What are the different ways to handle exceptions?<br />
61.What is the basic difference between the 2 approaches to exception handling…1&gt; try catch block and 2&gt; specifying the candidate exceptions in the throws clause?<br />
62.When should you use which approach?<br />
63.Is it necessary that each try block must be followed by a catch block?<br />
64.If I write return at the end of the try block, will the finally block still execute?<br />
65.If I write System.exit (0); at the end of the try block, will the finally block still execute?<br />
66.How are Observer and Observable used?<br />
67.What is synchronization and why is it important?<br />
68.How does Java handle integer overflows and underflows?<br />
69.Does garbage collection guarantee that a program will not run out of memory?<br />
70.What is the difference between preemptive scheduling and time slicing?<br />
71.When a thread is created and started, what is its initial state?<br />
72.What is the purpose of finalization?<br />
73.What is the Locale class?<br />
74.What is the difference between a while statement and a do statement?<br />
75.What is the difference between static and non-static variables?<br />
76.How are this() and super() used with constructors?<br />
77.What are synchronized methods and synchronized statements?<br />
78.What is daemon thread and which method is used to create the daemon thread?<br />
79.Can applets communicate with each other?<br />
80.What are the steps in the JDBC connection?<br />
81.How does a try statement determine which catch clause should be used to handle an exception?<br />
82.Can an unreachable object become reachable again?<br />
83.What method must be implemented by all threads?<br />
84.What are synchronized methods and synchronized statements?<br />
85.What is Externalizable?<br />
86.What modifiers are allowed for methods in an Interface?<br />
87.What are some alternatives to inheritance?<br />
88.What does it mean that a method or field is “static”? ?<br />
89.What is the difference between preemptive scheduling and time slicing?<br />
90.What is the catch or declare rule for method declarations? </font></span><span><font face="Times New Roman">91.Is Empty .java file a valid source file?<br />
92.Can a .java file contain more than one java classes?<br />
93.Is String a primitive data type in Java?<br />
94.Is main a keyword in Java?<br />
95.Is next a keyword in Java?<br />
96.Is delete a keyword in Java?<br />
97.Is exit a keyword in Java?<br />
98.What happens if you dont initialize an instance variable of any of the primitive types in Java?<br />
99.What will be the initial value of an object reference which is defined as an instance variable?<br />
100.What are the different scopes for Java variables?<br />
101.What is the default value of the local variables? </font></span><span><font face="Times New Roman">102.How many objects are created in the following piece of code?<br />
MyClass c1, c2, c3;<br />
c1 = new MyClass ();<br />
c3 = new MyClass (); </font></span><span><font face="Times New Roman">104.Can a public class MyClass be defined in a source file named YourClass.java?<br />
105.Can main method be declared final? </font></span><span><font face="Times New Roman">106.What will be the output of the following statement?<br />
System.out.println (”1″ + 3); </font></span><span><font face="Times New Roman">107.What will be the default values of all the elements of an array defined as an instance variable? </font></span><font size="2"><font face="Verdana"> </font></font><font size="2"><font face="Verdana"> </font></font><font size="2"><font face="Verdana"> </font></font><font size="2"><font face="Verdana"> </font></font><font size="2"><font face="Verdana"> </font></font><font size="2"><font face="Verdana"> </font></font><font size="2"><font face="Verdana"> </font></font><font size="2"><font face="Verdana"> </font></font><font size="2"><font face="Verdana"> </font></font><font size="2"><font face="Verdana"> </font></font><font size="2"><font face="Verdana"> </font></font><font size="2"><font face="Verdana"> </font></font><font size="2"><font face="Verdana"> </font></font><font size="2"><font face="Verdana"> </font></font></p>
<h3><span><a href="http://sharat.wordpress.com/2007/04/05/database-drill/"><font face="Times New Roman">Database Drill</font></a></span></h3>
<p><font face="Times New Roman"><strong><span style="color:red;">1.What is DDL and DML ?</span></strong><span> </span></font><span><font face="Times New Roman">DDL :-  Data defination Language </font></span><span><font face="Times New Roman">its having four commands </font></span><span><font face="Times New Roman"> Create , Alter , Drop , Truncate </font></span><span><font face="Times New Roman">DML :- Data manuplation language </font></span><span><font face="Times New Roman"> Insert , Update , Delete , select </font></span><font face="Times New Roman"><strong><span style="color:red;">2.How can you compare a part of the name rather than the entire name ?</span></strong><span> </span></font><em><span><font face="Times New Roman">SELECT * FROM people WHERE empname LIKE ‘%ab%’</font></span></em><em><span><br />
</span></em><span><font face="Times New Roman">Would return a recordset with records consisting empname the sequence ‘<strong>ab</strong>‘ in empname . </font></span><font face="Times New Roman"><strong><span style="color:red;">3.How could I get distinct entries from a table ?</span></strong><span> </span></font><span><font face="Times New Roman">The SELECT statement in conjunction with DISTINCT lets you select a set of distinct values from a table in a database. The values selected from the database table would of course depend on the various conditions that are specified in the SQL query. </font></span><font face="Times New Roman"><em><span>   SELECT DISTINCT empname FROM emptable </span></em><span></span></font><font face="Times New Roman"><strong><span style="color:red;">4.How to </span></strong><strong><span style="color:red;">get the results of a Query sorted in any order ?</span></strong><span></span></font><span><font face="Times New Roman">You can sort the results and return the sorted results to your program by using ORDER BY keyword thus saving you the pain of carrying out the sorting yourself. The ORDER BY keyword is used for sorting.<br />
</font><em><br />
<em><font face="Times New Roman">   SELECT empname, age, city FROM emptable ORDER BY empname</font></em></em></span><font face="Times New Roman"><strong><span style="color:red;">5.How can I find the total number of records in a table ?</span></strong><span></span></font><span><font face="Times New Roman">   You could use the COUNT keyword , example<br />
<em>       SELECT COUNT(*) FROM emp WHERE age&gt;40 </em></font></span><font face="Times New Roman"><strong><span style="color:red;">6.What is GROUP BY ?</span></strong><span></span></font><span><font face="Times New Roman">The GROUP BY keywords have been added to SQL because aggregate functions (like SUM) return the aggregate of all column values every time they are called. Without the GROUP BY functionality, finding the sum for each individual group of column values was not possible </font></span><font face="Times New Roman"><strong><span style="color:red;">7.What is the difference among “dropping a table”, “truncating a table” and “deleting all records” from a table ?</span></strong><span> </span></font><font face="Times New Roman"><strong><span>Dropping :</span></strong><span>  (Table structure  + Data are deleted), Invalidates the dependent objects ,Drops the indexes </span></font><font face="Times New Roman"><strong><span>Truncating:</span></strong><span>  (Data alone deleted), Performs an automatic commit, Faster than delete </span></font><font face="Times New Roman"><strong><span>Delete :</span></strong><span> (Data alone deleted), Doesn’t perform automatic commit </span></font><font face="Times New Roman"><strong><span style="color:red;">8.What are the Large object types suported by Oracle ? </span></strong><span></span></font><span><font face="Times New Roman">Blob and Clob </font></span><font face="Times New Roman"><strong><span style="color:red;">9.Difference between a “where” clause and a “having” clause ?</span></strong><span> </span></font><span><font face="Times New Roman">Having clause is used only with group functions whereas Where is not used with. </font></span><font face="Times New Roman"><strong><span style="color:red;">10.What’s the difference between a primary key and a unique key ? </span></strong><span></span></font><span><font face="Times New Roman">Both primary key and unique enforce uniqueness of the column on which they are defined. But by default primary key creates a clustered index on the column, where are unique creates a nonclustered index by default. Another major difference is that, primary key doesn’t allow NULLs, but unique key allows one NULL only. </font></span><font face="Times New Roman"><strong><span style="color:red;">11.What are triggers? How to invoke a trigger on demand ? </span></strong><span></span></font><span><font face="Times New Roman">Triggers are special kind of stored procedures that get executed automatically when an INSERT, UPDATE or DELETE operation takes place on a table. </font></span><span><font face="Times New Roman">Triggers can’t be invoked on demand. They get triggered only when an associated action (INSERT, UPDATE, DELETE) happens on the table on which they are defined. </font></span><span><font face="Times New Roman">Triggers are generally used to implement business rules, auditing. Triggers can also be used to extend the referential integrity checks, but wherever possible, use constraints for this purpose, instead of triggers, as constraints are much faster. </font></span><font face="Times New Roman"><strong><span style="color:red;">12. What is a join and explain different types of joins ?</span></strong><span> </span></font><span><font face="Times New Roman">Joins are used in queries to explain how different tables are related. Joins also let you select data from a table depending upon data from another table. </font></span><span><font face="Times New Roman">Types of  Joins </font></span><font face="Times New Roman"><strong><span>INNER JOINS:</span></strong><span> It will pull all the rows from both tables where there is a match. </span></font><font face="Times New Roman"><em><span>SELECT postid, posts.title, commentid, comments.title FROM posts INNER JOIN comments ON posts.postid = comments.postid</span></em><span> </span></font><font face="Times New Roman"><strong><span>LEFT OUTER JOIN:</span></strong><span> It will pull all the rows from left side of a join regardless of whether there is a match on the right side of join. Anything missing on the right side will be replaced by NULL. </span></font><font face="Times New Roman"><em><span>SELECT postid, posts.title, commentid, comments.title FROM posts LEFT OUTER JOIN comments ON posts.postid = comments.postid</span></em><span> </span></font><font face="Times New Roman"><strong><span>RIGHT OUTER JOIN:</span></strong><span> It will pull all the rows from the right side of a join regardless of whether there is a match on the left side of join. Anything missing on the left side will be replaced by NULL. </span></font><em><span><font face="Times New Roman">SELECT postid, posts.title, commentid, comments.title FROM posts </font></span></em><em><span><br />
<em><font face="Times New Roman">RIGHT OUTER JOIN comments ON posts.postid = comments.postid</font></em></span></em><span><font face="Times New Roman"> </font></span><font face="Times New Roman"><strong><span>CROSS JOIN:</span></strong><span> It returns a row of each combination of rows between the two tables. If you had 5 rows in each table, you’ed end up with 25 rows in the result set. </span></font><font face="Times New Roman"><em><span>SELECT Products.ProductName, Products.UnitPrice, Temporary.Amount FROM Products CROSS JOIN Temporary ORDER BY Products.ProductName, Temporary.Amount</span></em><span> </span></font><font face="Times New Roman"><strong><span style="color:red;">13.What is a Self Join ?</span></strong><span> </span></font><span><font face="Times New Roman">Self join is just like any other join, except that two instances of the same table will be joined in the query. </font></span><em><span><font face="Times New Roman">SELECT DISTINCT c1.ContactName, c1.Address, c1.City, c1.Region</font></span></em><em><span><br />
<font face="Times New Roman"><em>FROM Customers AS c1, Customers AS c2</em><br />
<em>WHERE c1.Region = c2.Region</em><br />
<em>AND c1.ContactName &lt;&gt; c2.ContactName</em><br />
<em>ORDER BY c1.Region, c1.ContactName;</em></font></span></em><span><font face="Times New Roman"> </font></span><font face="Times New Roman"><strong><span style="color:red;">14.What is Cursor  ? </span></strong><span></span></font><span><font face="Times New Roman">It is private SQL area to execute the sql quries and store the information. There are two types of cursors implicit cursors and explicit cursors. </font></span><span><font face="Times New Roman">Implicit cursors are system defined and returns one row. Explicit cursors are user defined and returns multiple rows </font></span><font face="Times New Roman"><strong><span style="color:red;">15.How do you get Column names only for a table (SQL Server)  ?</span></strong><span style="color:red;"> </span><span></span></font><em><span><font face="Times New Roman">select name from syscolumns</font></span></em><em><span><br />
<font face="Times New Roman"><em>where id=(select id from sysobjects where name=’user_hdr’)</em><br />
<em>order by colid –user_hdr is the table name</em></font></span></em><span></span><font face="Times New Roman"><strong><span style="color:red;">16.How to find stored procedures  ?</span></strong><span></span></font><font face="Times New Roman"><em><span>select procedure_name from all_procedures </span></em><span></span></font><font face="Times New Roman"><strong><span style="color:red;">17.What are Extended stored procedures  ?</span></strong><span></span></font><span><font face="Times New Roman">Extended stored procedures let you create your own external routines in a programming language such as C. Extended stored procedures are DLLs that an instance of Microsoft SQL Server can dynamically load and run. Extended stored procedures run directly in the address space of an instance of SQL Server and are programmed by using the SQL Server Extended Stored Procedure API.</font></span><font face="Times New Roman"><strong><span style="color:red;">18. Explain about Cursors ?</span></strong><span></span></font><span><font face="Times New Roman">Cursors are database objects used to traverse the results of an SQL query. They point to a certain location within a recordset and allow the operator to move forward (and sometimes backward, depending upon the cursor type) through the results one record at a time. There are different types of  cursors: Static, Dynamic, Forward-only, Keyset-driven. </font></span><font face="Times New Roman"><strong><span>Cursors extend result processing by: </span></strong><span></span></font></p>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">Allowing positioning at specific rows of the result set. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Retrieving one row or block of rows from the current position in the result set. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Supporting data modifications to the rows at the current position in the result set. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Supporting different levels of visibility to changes made by other users to the database data that is presented in the result set. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Providing Transact-SQL statements in scripts, stored procedures, and triggers access to the data in a result set. </font></span></li>
</ul>
<p><font face="Times New Roman"><strong><span>Disadvantages of cursors: </span></strong><span></span></font><span><font face="Times New Roman">Each time you fetch a row from the cursor, it results in a network roundtrip, where as a normal SELECT query makes only one rowundtrip, however large the resultset is. Cursors are also costly because they require more resources and temporary storage (results in more IO operations). Furthere, there are restrictions on the SELECT statements that can be used with some types of cursors.</font></span><span><font face="Times New Roman">Example:</font></span><em><span><font face="Times New Roman">CREATE OR REPLACE Function FindCourse</font></span></em><em><span><br />
<font face="Times New Roman"><em>( name_in IN varchar2 )</em><br />
<em>RETURN number</em><br />
<em>IS</em><br />
<em>cnumber number; </em></font></span></em><span></span><em><span><font face="Times New Roman">CURSOR c1</font></span></em><em><span><br />
<font face="Times New Roman"><em>IS</em><br />
<em>SELECT course_number</em><br />
<em>from courses_tbl</em><br />
<em>where course_name = name_in; </em></font></span></em><span></span><font face="Times New Roman"><em><span>BEGIN </span></em><span></span></font><em><span><font face="Times New Roman">open c1;</font></span></em><em><span><br />
<em><font face="Times New Roman">fetch c1 into cnumber; </font></em></span></em><span></span><em><span><font face="Times New Roman">if c1%notfound then</font></span></em><em><span><br />
<font face="Times New Roman"><em>cnumber := 9999;</em><br />
<em>end if; </em></font></span></em><span></span><font face="Times New Roman"><em><span>close c1; </span></em><span></span></font><font face="Times New Roman"><em><span>RETURN cnumber; </span></em><span></span></font><font face="Times New Roman"><em><span>END;</span></em><span></span></font><font face="Times New Roman"><strong><span style="color:red;">19.Explain about Stored Procedures ?</span></strong><span></span></font><span><font face="Times New Roman">A stored procedure is a group of SQL statements that form a logical unit and perform a particular task. Stored procedures are used to encapsulate a set of operations or queries to execute on a database server.For example, operations on an employee database (hire, fire, promote, lookup) could be coded as stored procedures executed by application code. Stored procedures can be compiled and executed with different parameters and results, and they may have any combination of input, output, and input/output parameters.</font></span><strong><em><span><font face="Times New Roman">No Param</font></span></em></strong><em><span><br />
<font face="Times New Roman"><em>CREATE OR REPLACE PROCEDURE myproc IS </em><br />
<em>BEGIN </em><br />
<em>INSERT INTO oracle_table VALUES(’string 1′);</em><br />
<em>END;</em><br />
<strong>IN Param</strong><em> </em><br />
<em>CREATE OR REPLACE PROCEDURE myprocin(x VARCHAR) IS </em><br />
<em>BEGIN </em><br />
<em>INSERT INTO oracle_table VALUES(x); </em><br />
<em>END;</em><br />
<strong>OUT Param</strong><br />
<em>CREATE OR REPLACE PROCEDURE myprocout(x OUT VARCHAR) IS </em><br />
<em>BEGIN </em><br />
<em>INSERT INTO oracle_table VALUES(’string 2′); </em><br />
<em>x := ‘outvalue’; // Assign a value to x</em><br />
<em>END;</em><br />
<strong>IN/OUT Param</strong><br />
<em>CREATE OR REPLACE PROCEDURE myprocinout(x IN OUT VARCHAR) IS </em><br />
<em>BEGIN </em><br />
<em>INSERT INTO oracle_table VALUES(x); // Use x as IN parameter</em><br />
<em>x := ‘outvalue’; // Use x as OUT parameter</em><br />
<em>END;</em></font></span></em><span></span><font face="Times New Roman"><strong><span style="color:red;">20.Explain about Views ?</span></strong><span> </span></font><span><font face="Times New Roman">View is a virtual or logical table composed of the result set of a query. Unlike orginary tables in a relational database, a view is not part of the physical schema. It is a dynamic virtual table computed or collated from data in the database. Chainging the data in a table alters the data shown in the view.</font></span><span><font face="Times New Roman">Views can provide advantages over tables</font></span></p>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">They can subset the data contained in a table </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">They can join and simplify multiple tables into a single virtual table </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Views can act as aggregated tables, where aggregated data (sum,average etc.) are calculated and presented as part of the data </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Views can hide the complexity of data, </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Views do not incur any extra storage overhead </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Depending on the SQL engine used, views can provide extra security. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Limit the exposure to which a table or tables are exposed to outer world </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Rows in a view are not sorted. So we cannot use ORDER BY clause in the view definition. </font></span></li>
</ul>
<p><em><span><font face="Times New Roman">          CREATE VIEW V_Customer</font></span></em><em><span><br />
<font face="Times New Roman"><em>          AS SELECT First_Name, Last_Name, Country</em><br />
<em>         FROM Customer</em></font></span></em><span></span><span><font face="Times New Roman"> </font></span><font face="Times New Roman"><strong><span style="color:red;">21.Stored Function Vs Stored Procedure </span></strong><span></span></font><font face="Times New Roman"><strong><span>Function :</span></strong><span> </span></font><span><font face="Times New Roman">1. Should return atleast one output parameter.Can return more than one parameter using OUT argument. </font></span><span><font face="Times New Roman">2. Parsed and compiled at runtime. </font></span><span><font face="Times New Roman">3.Cannot affect the state of database. </font></span><span><font face="Times New Roman">4.Can be invoked from SQL statement e.g. SELECT. </font></span><font face="Times New Roman"><em><strong><span>5. Functions are mainly used to compute values.</span></strong></em><span> </span></font><font face="Times New Roman"><strong><span>Procedure:</span></strong><span> </span></font><span><font face="Times New Roman">1. Doesn’t need to return values, but can return value. </font></span><span><font face="Times New Roman">2.Stored as a pseudo-code in database i.e. compiled form. </font></span><span><font face="Times New Roman">3.Can affect the state of database using commit etc. </font></span><span><font face="Times New Roman">4.Cannnot be invoked from SQL statements e.g. SELECT. </font></span><font face="Times New Roman"><strong><em><span>5.Procedures are mainly used to process the tasks.</span></em></strong><span></span></font><font face="Times New Roman"><strong><u><span style="color:blue;">Java based DB Questions</span></u></strong><span> </span></font><font face="Times New Roman"><strong><span style="color:red;">1.How do you call a Stored Procedure from JDBC ? </span></strong><span> </span></font><span><font face="Times New Roman">The first step is to create a CallableStatement object. As with Statement and PreparedStatement objects, this is done with an open Connection object. A CallableStatement object contains a call to a stored procedure. </font></span></p>
<pre><span><font size="2"><span>        </span>CallableStatement cs =</font></span></pre>
<pre><span><font size="2"><span>               </span>con.prepareCall("{call SHOW_SUPPLIERS}");</font></span></pre>
<pre><span><font size="2"><span>        </span>ResultSet rs = cs.executeQuery();</font></span></pre>
<p><font face="Times New Roman"><strong><span style="color:red;">2.What is cold backup, hot backup ? </span></strong><span style="color:red;"> </span><span></span></font><span><font face="Times New Roman">Cold backup means all these files must be backed up at the same time, before the databaseis restarted. Hot backup or online backup is a backup taken of each tablespace while the database is running and is being accessed by the users. </font></span><font face="Times New Roman"><strong><span style="color:red;">3.What is a “dirty read” ? </span></strong><span style="color:red;"> </span><span></span></font><span><font face="Times New Roman">Quite often in database processing, we come across the situation wherein one transaction can change a value, and a second transaction can read this value before the original change has been committed or rolled back. This is known as a dirty read scenario because there is always the possibility that the first transaction may rollback the change, resulting in the second transaction having read an invalid value. While you can easily command a database to disallow dirty reads, this usually degrades the performance of your application due to the increased locking overhead. Disallowing dirty reads also leads to decreased system concurrency. </font></span><font face="Times New Roman"><strong><span style="color:red;">4.What is Metadata and why should I use it  </span></strong><span style="color:red;"> </span><span></span></font><span><font face="Times New Roman">Metadata (’data about data’) is information about one of two things: Database information (java.sql.DatabaseMetaData), or Information about a specific ResultSet (java.sql.ResultSetMetaData). Use DatabaseMetaData to find information about your database, such as its capabilities and structure. Use ResultSetMetaData to find information about the results of an SQL query, such as size and types of columns </font></span><font face="Times New Roman"><strong><span style="color:red;">5.Different types of Transaction Isolation Levels</span></strong><span style="color:red;"> </span><span></span></font><span><font face="Times New Roman">The isolation level describes the degree to which the data being updated is visible to other transactions. This is important when two transactions are trying to read the same row of a table. Imagine two transactions: A and B. Here three types of inconsistencies can occur: </font></span></p>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">Dirty-read: A has changed a row, but has not committed the changes. B reads the uncommitted data but his view of the data may be wrong if A rolls back his changes and updates his own changes to the database. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Non-repeatable read: B performs a read, but A modifies or deletes that data later. If B reads the same row again, he will get different data. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Phantoms: A does a query on a set of rows to perform an operation. B modifies the table such that a query of A would have given a different result. The table may be inconsistent. </font></span></li>
</ul>
<p><font face="Times New Roman"><strong><span>1.TRANSACTION_READ_UNCOMMITTED :</span></strong><span> Dirty-read, Non-repeatable read and Phantoms can occur</span></font><font face="Times New Roman"><strong><span>2.TRANSACTION_READ_COMMITTED :</span></strong><span> Dirty-reads are prevented. Non-repeatable read and Phantoms can occur.</span></font><font face="Times New Roman"><strong><span>3.TRANSACTION_REPEATABLE_READ : </span></strong><span>Dirty-reads and Non-repeatable read are prevented and Phantoms can occur.</span></font><font face="Times New Roman"><strong><span>4.TRANSACTION_SERIALIZABLE :</span></strong><span> Dirty-reads,Non-repeatable read and Phantoms are prevented.</span></font><font face="Times New Roman"><strong><span style="color:red;">6.How do you handle your own transaction ?</span></strong><span></span></font><span><font face="Times New Roman">Connection Object has a method called setAutocommit(Boolean istrue)<br />
Default is true. Set the Parameter to false , and begin your transaction</font></span><font size="2"><font face="Verdana"> </font></font><strong><span style="font-size:9pt;color:#333333;"><font face="Verdana"> </font></span></strong><strong><span style="font-size:9pt;color:#333333;"><font face="Verdana"> </font></span></strong><strong><span style="font-size:9pt;color:#333333;"><font face="Verdana"> </font></span></strong><strong><span style="font-size:9pt;color:#333333;"><font face="Verdana"> </font></span></strong></p>
<h3><span><a href="http://sharat.wordpress.com/2007/03/29/explain-about-validator-rulesxml-and-validationxml-files/"><font face="Times New Roman">Explain about validator-rules.xml and validation.xml files ?</font></a></span></h3>
<p><span><font face="Times New Roman">Validator framework is used to validate the form data and you no need to write any code in your form bean for validations and storing the error messages.Your Form Bean, extending one of the Validator’s Action Form subclasses, does the required validations.If you do not use Validator, you need to handle the data validations in the validate() method of Form Bean objects, and also handle storing of error messages if validationis fails.</font></span><span><font face="Times New Roman">Two configuration files used by Validator are validation.xml and validator-rules.xml. In order to enable and use the Validator, plug-in should be added in sturts-config.xml </font></span><font face="Times New Roman"><span style="color:blue;">&lt;plug-in className=”org.apache.struts.validator.ValidatorPlugin”&gt;<br />
      &lt;set-property property=”pathnames”<br />
                            value=”/WEB-INF/validator-rules.xml,<br />
                                       /WEB-INF/validation.xml”/&gt;<br />
&lt;/plug-in&gt;</span><span> </span></font><font face="Times New Roman"><strong><span style="color:red;">validator-rules.xml: </span></strong><span>This declares the validation routines and logical names are provided for the validations.It is also contains the client side javascript code for each validation routine. This validation routines are java methods plugged into the system to perform the specific validations. </span></font><font face="Times New Roman"><span style="color:blue;">&lt;validator name=”required”<br />
                  classname=”org.apache.struts.validator.FieldChecks”<br />
                  method=”validateRequired”<br />
                  methodParams=”java.lang.Object,<br />
                                            org.apache.commons.validator.ValidatorAction,<br />
                                            org.apache.commons.validator.Field,<br />
                                            org.apache.struts.action.ActionErrors,<br />
                                            javax.servlet.http.HttpServletRequest”<br />
                  msg=”errors.required”&gt; </span><span></span></font><font face="Times New Roman"><span style="color:blue;">           &lt;javascript&gt;<br />
                    &lt;![CDATA[// place the scripts here]]&gt;<br />
         &lt;/javascript&gt;<br />
&lt;/validator&gt;</span><span></span></font><span><font face="Times New Roman">If the validation fails when it run, the message(i.e..in ApplicationResources.properties) corresponding to the key specified by the msg attribute will be returned.</font></span><font face="Times New Roman"><strong><span style="color:red;">validation.xml: </span></strong><span>This configuration files defines which validation routines must be applied for which form beans. Each form bean you want to validate has its own defination in this file. Inside that definations, you want to apply to the Form Beans’s fields. The definition in the file use the logical names of Form Beans from struts-config.xml file along with logical names of the validation routines from validation-rules.xml to tie together </span></font><font face="Times New Roman"><span style="color:blue;">&lt;form-validation&gt;<br />
     &lt;formset&gt;<br />
             &lt;form name=”logonForm”&gt;<br />
                    &lt;field property=”username” depends=”required”&gt;<br />
                                &lt;arg0 key=”prompt.username”/&gt;<br />
                   &lt;/field&gt;<br />
                   &lt;field property=”username” depends=”required”&gt;<br />
                                &lt;arg0 key=”prompt.password”/&gt;<br />
                   &lt;/field&gt;<br />
            &lt;/form&gt;<br />
    &lt;/formset&gt;<br />
&lt;/form-validation&gt;</span><span> </span></font><span><font face="Times New Roman">The depends attribute specifies the logical names of validation routines from the validator-rules.xml that should be applied to the field.</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2007/03/29/explain-about-validator-rulesxml-and-validationxml-files/#respond" title="Comment on Explain about validator-rules.xml and validation.xml files ?"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>March 29th, 2007</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2007/03/29/what-is-difference-between-actionform-and-dynaactionform/"><font face="Times New Roman">What is difference between ActionForm and DynaActionForm</font></a></span></h3>
<p><span><font face="Times New Roman">If the ActionForm is used, then user himself has to write the setters and getters when ever he adds a control. The same process is repeated again and again when user creates a view. DynaActionForm eliminates this burden and creates the form bean itself. This way user dont have to write setters and getters. No bean class is required for the DynaActionForm and we will declare the form beans as DynaActionForm type in struts-confing.xml. We will declare the properties and their type in the struts-config.xml</font></span><font face="Times New Roman"><span style="color:blue;">&lt;form-beans&gt;<br />
&lt;form-bean name=”employeeForm”   type=”org.apache.struts.action.DynaActionForm”&gt;<br />
&lt;form-property name=”name” type=”java.lang.String”/&gt;<br />
&lt;form-property name=”age” type=”java.lang.String”/&gt;<br />
&lt;/form-bean&gt;<br />
&lt;/form-beans&gt;</span><span></span></font><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2007/03/29/what-is-difference-between-actionform-and-dynaactionform/#comments" title="Comment on What is difference between ActionForm and DynaActionForm"><font face="Times New Roman">1 comment</font></a><font face="Times New Roman"> <em>March 29th, 2007</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2007/03/29/describe-validate-and-reset-methods/"><font face="Times New Roman">Describe validate() and reset() methods ?</font></a></span></h3>
<p><font face="Times New Roman"><strong><span>validate() : </span></strong><span>ActionForm class provides a validate() method hook that can be overridden by subclasses to perform validations on incoming form data.The validate() method hook gets called after a Form Bean has been populated with incoming form data. Following is the method signature for the validate() method</span></font><font face="Times New Roman"><span style="color:blue;">public ActionErrors validate(ActionMapping mapping,HttpServletRequest request)</span><span></span></font><span><font face="Times New Roman">validate() method has return type ActionErrors</font></span><font face="Times New Roman"><strong><span>reset(): </span></strong><span>ActionForm class has a reset() method that subclasses can override. The method is a hook that gets called before FormBean is populated with request data from an HTML form. Using this reset() method, all the form fields will be reset.i.e..Unchecking the check boxes, making the text fileds empty..etc </span></font><font face="Times New Roman"><span style="color:blue;">public void reset() {}</span><span></span></font><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2007/03/29/describe-validate-and-reset-methods/#respond" title="Comment on Describe validate() and reset() methods ?"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>March 29th, 2007</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2007/03/29/explain-deployment-descriptorwebxml-configuration-for-struts-application/"><font face="Times New Roman">Explain deployment descriptor(web.xml) configuration for struts application ?</font></a></span></h3>
<p><span><font face="Times New Roman">Action Servlet and strtus-config.xml files and configured as below</font></span><font face="Times New Roman"><span style="color:blue;">&lt;servlet&gt;<br />
      &lt;servlet-name&gt;action&lt;/servlet-name&gt;<br />
      &lt;servlet-class&gt;org.apache.struts.action.ActionServlet&lt;/servlet-class&gt;<br />
      &lt;init-param&gt;<br />
         &lt;param-name&gt;config&lt;/param-name&gt;<br />
         &lt;param-value&gt;/WEB-INF/struts-config.xml&lt;/param-value&gt;<br />
       &lt;/init-param&gt;<br />
&lt;/servlet&gt;</span><span></span></font><span><font face="Times New Roman">This <strong>config</strong> parameter tells the Strut’s Action Servlet where to find its central configuration file struts-config.xml</font></span><font face="Times New Roman"><span style="color:blue;">&lt;servlet-mapping&gt;<br />
             &lt;servlet-name&gt;action&lt;/servlet-name&gt;<br />
             &lt;url-pattern&gt;*.do&lt;/url-pattern&gt;<br />
&lt;servlet-mapping&gt;</span><span></span></font><span><font face="Times New Roman">In servlet mapping &lt;url-pattern&gt; tag is sayinng that ActionServlet should process any requests for pages thats end in .do</font></span><span><font face="Times New Roman">Tag Library descriptors will be configured as below</font></span><font face="Times New Roman"><span style="color:blue;">&lt;taglib&gt;<br />
&lt;taglib-uri&gt; /WEB-INF/tlds/struts-bean.tld&lt;/taglib-uri&gt;<br />
&lt;taglib-location&gt; /WEB-INF/tlds/struts-bean.tld&lt;/taglib-location&gt;<br />
&lt;/taglib&gt; </span><span></span></font><span><font face="Times New Roman">Simillarly struts-html.tld,struts-logic.tld will be configured</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2007/03/29/explain-deployment-descriptorwebxml-configuration-for-struts-application/#respond" title="Comment on Explain deployment descriptor(web.xml) configuration for struts application ?"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>March 29th, 2007</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2007/03/29/is-struts-thread-safe/"><font face="Times New Roman">Is Struts Thread Safe ?</font></a></span></h3>
<p><span><font face="Times New Roman">Struts is not only thread-safe but thread-depenedent. The response to a requeast is handled by light weight Action object, rather than an individual servlet.Struts instanciate each Action class once and allows other request to threaded through the orginal object. This core strategy conserves resources and provides the best posible throughput. A properyly-designed application will exploit this further by routing realated operations through a single action.</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2007/03/29/is-struts-thread-safe/#respond" title="Comment on Is Struts Thread Safe ?"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>March 29th, 2007</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2007/03/27/95-hibernate-drill-one/"><font face="Times New Roman">95. Hibernate Drill One</font></a></span></h3>
<p><span><font face="Times New Roman">1. What is Hibernate<br />
2. What are ORM tools<br />
3. What is Object/relation paradigm mismatch<br />
4. What role does the session interface play in Hibernate<br />
5. What is sessionFactory interface<br />
6. What is Configuration interface<br />
7. What is the naming convention of Hibernate XML mapping file extensions<br />
8. What are the most common methods of configurating hibernate<br />
9. How can the mapping files be configured in Hibernate<br />
10. What happens when both hibernate.properties and hibernate.cfg.xml are in the classpath<br />
11. Sinsce sessionFactory instances arenot lightweight, where is the single created instance planced in j2ee environment.<br />
12. How to set Hibernate to log all generated SQL to the console.<br />
13. In hibernate, what inerfaces/classes must the persistent classes<br />
14. Does hibernate require persistent classes to implementn Serializable.<br />
15. What methods must be the persistent classes implement in Hibernate<br />
16. How can Hibernate be configured to access a instance variable directly and not through a setter method.<br />
17. What is dirty checking in Hibernate.<br />
18. What is the root level element in a hibernate mapping file?<br />
19. Is it possible to declare mappings for multiple classes in one mapping file?<br />
20. How are the individual properties mapped to different table columns?<br />
21. What are derived properties?<br />
22. How can you make a property be read from the database but not modified in anyway (make it immutable)?<br />
23. How can a whole class be mapped as immutable?<br />
24. What is the use of dynamic-insert and dynamic-update attributes in a class mapping?<br />
25. Name types of inheritence models and describe how they work? (such as vertical inheritence, horizontal, etc)<br />
26. Study some hibernate files and write a query based on some set of information.<br />
27. How would you go about creating hibernate classes and XML on a new set of tables? (hint: think code generation).<br />
28. How hibernate is related to J2EE<br />
29. What is component mapping in hibernate?<br />
30. what is lazy fetching in hibernate<br />
31. What are the types of inheritence models and describe how they work like vertical inheritence and horizontal<br />
32. what is the difference between beans and hibernate(which one is good)?<br />
33. what is the advantage of Hibernate over jdbc?<br />
34. how to create primary key using hibernate?<br />
35. What is the difference between sorted and orderd collection in hibernate?<br />
36. Why do you need ORM tools like hibernate? </font></span><span><font face="Times New Roman">(Aboved questions are not yet edited for format. Answers are on its way.. till then test your self with the above questions.)</font></span><strong><span style="font-size:9pt;color:#333333;"><font face="Verdana"> </font></span></strong><strong><span style="font-size:9pt;color:#333333;"><font face="Verdana"> </font></span></strong><strong><span style="font-size:9pt;color:#333333;"><font face="Verdana"> </font></span></strong></p>
<h3><span><a href="http://sharat.wordpress.com/2007/03/22/93-ejb-drill-3/"><font face="Times New Roman">93. EJB Drill 3</font></a></span></h3>
<p><font face="Times New Roman"><strong><span>Can you control when passivation occurs? </span></strong><span></span></font><span><font face="Times New Roman">The developer, according to the specification, cannot directly control when passivation occurs. Although for Stateful Session Beans, the container cannot passivate an instance that is inside a transaction. So using transactions can be a a strategy to control passivation. The ejbPassivate() method is called during passivation, so the developer has control over what to do during this exercise and can implement the require optimized logic. Some EJB containers, such as BEA WebLogic, provide the ability to tune the container to minimize passivation calls. Taken from the WebLogic 6.0 DTD -The passivation-strategy can be either default or transaction. With the default setting the container will attempt to keep a working set of beans in the cache. With the transaction setting, the container will passivate the bean after every transaction (or method call for a non-transactional invocation). </font></span><font face="Times New Roman"><strong><span>What is the advantage of using Entity bean for database operations, over directly using JDBC API to do database operations? When would I use one over the other? </span></strong><span></span></font><span><font face="Times New Roman">Entity Beans actually represents the data in a database. It is not that Entity Beans replaces JDBC API. There are two types of Entity Beans Container Managed and Bean Mananged. In Container Managed Entity Bean &#8211; Whenever the instance of the bean is created the container automatically retrieves the data from the DB/Persistance storage and assigns to the object variables in bean for user to manipulate or use them. For this the developer needs to map the fields in the database to the variables in deployment descriptor files (which varies for each vendor). In the Bean Managed Entity Bean &#8211; The developer has to specifically make connection, retrive values, assign them to the objects in the ejbLoad() which will be called by the container when it instatiates a bean object. Similarly in the ejbStore() the container saves the object values back the the persistance storage. ejbLoad and ejbStore are callback methods and can be only invoked by the container. Apart from this, when you use Entity beans you dont need to worry about database transaction handling, database connection pooling etc. which are taken care by the ejb container. </font></span><font face="Times New Roman"><strong><span>The EJB container implements the EJBHome and EJBObject classes. For every request from a unique client, does the container create a separate instance of the generated EJBHome and EJBObject classes? </span></strong><span></span></font><span><font face="Times New Roman">The EJB container maintains an instance pool. The container uses these instances for the EJB Home reference irrespective of the client request. while refering the EJB Object classes the container creates a separate instance for each client request. The instance pool maintenance is up to the implementation of the container. If the container provides one, it is available otherwise it is not mandatory for the provider to implement it. Having said that, yes most of the container providers implement the pooling functionality to increase the performance of the application server. The way it is implemented is, again, up to the implementer. </font></span><font face="Times New Roman"><strong><span>What are the special design care that must be taken when you work with local interfaces? </span></strong><span></span></font><span><font face="Times New Roman">It is important to understand that the calling semantics of local interfaces are different from those of remote interfaces. For example, remote interfaces pass parameters using call-by-value semantics, while local interfaces use call-by-reference. This means that in order to use local interfaces safely, application developers need to carefully consider potential deployment scenarios up front, then decide which interfaces can be local and which remote, and finally, develop the application code with these choices in mind. While EJB 2.0 local interfaces are extremely useful in some situations, the long-term costs of these choices, especially when changing requirements and component reuse are taken into account, need to be factored into the design decision. </font></span><font face="Times New Roman"><strong><span>What is session Facade? </span></strong><span></span></font><span><font face="Times New Roman">Session Facade is a design pattern to access the Entity bean through local interface than accessing directly. It increases the performance over the network. In this case we call session bean which on turn call entity bean. </font></span><font face="Times New Roman"><strong><span>Is is possible for an EJB client to marshal an object of class java.lang.Class to an EJB? </span></strong><span></span></font><span><font face="Times New Roman">Technically yes, spec. compliant NO! &#8211; The enterprise bean must not attempt to query a class to obtain information about the declared members that are not otherwise accessible to the enterprise bean because of the security rules of the Java language. </font></span><font face="Times New Roman"><strong><span>Is there any default cache management system with Entity beans ? </span></strong><span></span></font><span><font face="Times New Roman">In other words whether a cache of the data in database will be maintained in EJB ? &#8211; Caching data from a database inside the AAApplication Server are what Entity EJB’s are used for.The ejbLoad() and ejbStore() methods are used to synchronize the Entity Bean state with the persistent storage(database). Transactions also play an important role in this scenario. If data is removed from the database, via an external application &#8211; your Entity Bean can still be alive the EJB container. When the transaction commits, ejbStore() is called and the row will not be found, and the transaction rolled back. </font></span><font face="Times New Roman"><strong><span>Why is ejbFindByPrimaryKey mandatory? </span></strong><span></span></font><span><font face="Times New Roman">An Entity Bean represents persistent data that is stored outside of the EJB Container/Server. The ejbFindByPrimaryKey is a method used to locate and load an Entity Bean into the container, similar to a SELECT statement in SQL. By making this method mandatory, the client programmer can be assured that if they have the primary key of the Entity Bean, then they can retrieve the bean without having to create a new bean each time &#8211; which would mean creating duplications of persistent data and break the integrity of EJB. </font></span><font face="Times New Roman"><strong><span>Is method overloading allowed in EJB? </span></strong><span></span></font><span><font face="Times New Roman">Yes you can overload methods Should synchronization primitives be used on bean methods? &#8211; No. The EJB specification specifically states that the enterprise bean is not allowed to use thread primitives. The container is responsible for managing concurrent access to beans at runtime. </font></span><font face="Times New Roman"><strong><span>What is the difference between a Server, a Container, and a Connector? </span></strong><span></span></font><span><font face="Times New Roman">An EJB server is an application, usually a product such as BEA WebLogic, that provides (or should provide) for concurrent client connections and manages system resources such as threads, processes, memory, database connections, network connections, etc. An EJB container runs inside (or within) an EJB server, and provides deployed EJB beans with transaction and security management, etc. The EJB container insulates an EJB bean from the specifics of an underlying EJB server by providing a simple, standard API between the EJB bean and its container. A Connector provides the ability for any Enterprise Information System (EIS) to plug into any EJB server which supports the Connector architecture. See Sun’s J2EE Connectors for more in-depth information on Connectors. </font></span><font face="Times New Roman"><strong><span>For Entity Beans, What happens to an instance field not mapped to any persistent storage, when the bean is passivated? </span></strong><span></span></font><span><font face="Times New Roman">The specification infers that the container never serializes an instance of an Entity bean (unlike stateful session beans). Thus passivation simply involves moving the bean from the ready to the pooled bin. So what happens to the contents of an instance variable is controlled by the programmer. Remember that when an entity bean is passivated the instance gets logically disassociated from it’s remote object. Be careful here, as the functionality of passivation/activation for Stateless Session, Stateful Session and Entity beans is completely different. For entity beans the ejbPassivate method notifies the entity bean that it is being disassociated with a particular entity prior to reuse or for dereference. </font></span><font face="Times New Roman"><strong><span>Does the container create a separate instance of the generated EJBHome and EJBObject classes? </span></strong><span></span></font><span><font face="Times New Roman">The EJB container maintains an instance pool. The container uses these instances for the EJB Home reference irrespective of the client request. while refering the EJB Object classes the container creates a separate instance for each client request. The instance pool maintainence is up to the implementation of the container. If the container provides one, it is available otherwise it is not mandatory for the provider to implement it. Having said that, yes most of the container providers implement the pooling functionality to increase the performance of the application server. The way it is implemented is again up to the implementer. </font></span><font face="Times New Roman"><strong><span>What is the advantage of putting an Entity Bean instance from the Ready State to Pooled state </span></strong><span></span></font><span><font face="Times New Roman">The idea of the Pooled State is to allow a container to maintain a pool of entity beans that has been created, but has not been yet synchronized or assigned to an EJBObject. This mean that the instances do represent entity beans, but they can be used only for serving Home methods (create or findBy), since those methods do not relay on the specific values of the bean. All these instances are, in fact, exactly the same, so, they do not have meaningful state. Jon Thorarinsson has also added: It can be looked at it this way: If no client is using an entity bean of a particular type there is no need for cachig it (the data is persisted in the database). Therefore, in such cases, the container will, after some time, move the entity bean from the Ready State to the Pooled state to save memory. Then, to save additional memory, the container may begin moving entity beans from the Pooled State to the Does Not Exist State, because even though the bean’s cache has been cleared, the bean still takes up some memory just being in the Pooled State. </font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2007/03/22/93-ejb-drill-3/#comments" title="Comment on 93. EJB Drill 3"><font face="Times New Roman">1 comment</font></a><font face="Times New Roman"> <em>March 22nd, 2007</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2007/03/22/92-ejb-drill-two/"><font face="Times New Roman">92. EJB Drill Two</font></a></span></h3>
<p><span><font face="Times New Roman">What is the differnece between EJB and Java Beans<br />
Explain Local Interfaces<br />
What is EJB Container<br />
What is in-memory replication<br />
What is ripple effect<br />
What’s new in the EJB 2.0 Specification<br />
What is the difference between a Coarse Grained Entiry bean and Fine Grained Entity Bean.<br />
What are transaction isolation levels in EJB<br />
What is the software architectury of EJBs.<br />
Waht is the need of Remote and Home interface. Why can’t it be in one.<br />
What is an EJB Context<br />
How can you call one EJB from inside of another EJB.<br />
What happens if remove() is never inoved on a session bean.<br />
What is EJB QL<br />
Can the primary key in the entity bean be a Java Primitive Type such as int<br />
How EJB Invocation Happens.<br />
What are the transaction attributes.<br />
What is the difference between find and select methods in EJB<br />
What is abstract schema<br />
What are the services provided by container<br />
What is a deployment descriptor<br />
How many EJB objects are created for a bean<br />
What is re-entrant. Is session beans reentrant. Is entity beans reentrant.<br />
What is the difference between EAT,JAT,WAR files<br />
What is lazy loading<br />
Can i amp more than one table in a CMP<br />
Is decorator an EJB design pattern<br />
What is the difference between session context and entity context<br />
Does stateless session bean create() method contain any parameters.<br />
What is the difference between ejbCreate and ejbPostCreate<br />
What are the optional clauses in EJB QL<br />
Can i invoke runtime.gc() in an EJB<br />
What is a remote client view<br />
What is local client view<br />
What is EJB client JAR File.<br />
What are the call back methods in Entity bean?<br />
If session has thrown ApplicaitonException would you use<br />
EJBContext.setRollBackOnly method?<br />
How do you create a entity bean in sun one application server.<br />
How do you map table in CMP.<br />
How do you deploy a EJB<br />
What EAR file. How it will be created.<br />
What is clustering.</font></span><span><font face="Times New Roman">Explain the packaging structure of an <strong>EJB</strong> application?</font></span><span><font face="Times New Roman">more questions: http://groups.google.com/group/lookhere/browse_thread/thread/47716d3565424726/25123fc4710e1839?lnk=st&amp;q=EJB+Interview+questions&amp;rnum=23&amp;hl=en#25123fc4710e1839</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2007/03/22/92-ejb-drill-two/#respond" title="Comment on 92. EJB Drill Two"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>March 22nd, 2007</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2007/03/20/91-ejb-short-drill-one/"><font face="Times New Roman">91. EJB Short Drill - One</font></a></span></h3>
<p><font face="Times New Roman"><strong><span style="color:red;">What are the differnt kids of EJBs</span></strong><span></span></font><strong><span><font face="Times New Roman">Stateless session bean</font></span></strong><span><font face="Times New Roman">- An instance of these non-persistent EJBs provides a service without storing an interaction or conversation state between methods. Any instance can be used for any client.<br />
<strong>Stateful session bean-</strong> An instance of these non-persistent EJBs maintains state across methods and transactions. Each instance is associated with a particular client.<br />
<strong>Entity bean-</strong> An instance of these persistent EJBs represents an object view of the data, usually rows in a database. They have a primary key as a unique identifier. Entity bean persistence can be either container-managed or bean-managed.<br />
<strong>Message-driven</strong> <strong>bean</strong>- An instance of these EJBs is integrated with the Java Message Service (JMS) to provide the ability for message-driven beans to act as a standard JMS message consumer and perform asynchronous processing between the server and the JMS message producer. </font></span><font face="Times New Roman"><strong><span style="color:red;">Explain Session Beans</span></strong><span></span></font><span><font face="Times New Roman">A session bean is a non-persistent object that implements some business logic running on the server. One way to think of a session object is as a logical extension of the client program that runs on the server.<br />
Session beans are used to manage the interactions of entity and other session beans,access resources, and generally perform tasks on behalf of the client.<br />
There are two basic kinds of session bean: stateless and stateful.<br />
<strong>Stateless session beans</strong> are made up of business methods that behave like procedures; they operate only on the arguments passed to them when they are invoked. Stateless beans are called stateless because they are transient; they do not maintain business state between method invocations.Each invocation of a stateless business method is independent from previous invocations. Because stateless session beans are stateless, they are easier for the EJB container to manage, so they tend to process requests faster and use less resources.<br />
<strong>Stateful session beans</strong> encapsulate business logic and state specific to a client. Stateful beans are called “stateful” because they do maintain business state between method invocations, held in memory and not persistent. Unlike stateless session beans, clients do not share stateful beans. When a client creates a stateful bean, that bean instance is dedicated to service only that client. This makes it possible to maintain conversational state, which is business state that can be shared by methods in the same stateful bean.</font></span><font face="Times New Roman"><strong><span style="color:red;">What is Entity Bean</span></strong><span></span></font><span><font face="Times New Roman">The entity bean is used to represent data in the database. It provides an object-oriented interface to data that would normally be accessed by the JDBC or some other back-end API. More than that, entity beans provide a component model that allows bean developers to focus their attention on the business logic of the bean, while the container takes care of managing persistence,transactions, and access control. </font></span><span><font face="Times New Roman">There are two basic kinds of entity beans: container-managed ersistence (CMP) andbean-managed persistence (BMP). </font></span><font face="Times New Roman"><strong><span>Container-managed persistence beans</span></strong><span> are the simplest for the bean developer to create and the most difficult for the EJB server to support. This is because all the logic for synchronizing the bean’s state with the database is handled automatically by the container. This means that the bean developer doesn’t need to write any data access logic, while the EJB server is supposed to take care of all the persistence needs automatically. With CMP, the container manages the persistence of the entity bean. Vendor tools are used to map the entity fields to the database and absolutely no database access code is written in the bean class. </span></font><font face="Times New Roman"><strong><span>The bean-managed persistence (BMP)</span></strong><span> enterprise bean manages synchronizing its state with the database as directed by the container. The bean uses a database API to read and write its fields to the database, but the container tells it when to do each synchronization operation and manages the transactions for the bean automatically. Bean-managed persistence gives the bean developer the flexibility to perform persistence operations that are too complicated for the container or to use a data source that is not supported by the container.</span></font><font face="Times New Roman"><strong><span style="color:red;">What are the methods of Entity Bean?</span></strong><span></span></font><span style="font-size:10pt;font-family:Verdana;">An entity bean consists of 4 groups of methods: </span><strong><span style="font-size:10pt;font-family:Verdana;">1</span></strong><span style="font-size:10pt;font-family:Verdana;">. <strong>create methods:</strong> To create a new instance of a CMP entity bean, and therefore insert data into the database, the create() method on the bean’s home interface must be invoked. They look like this: EntityBeanClass ejbCreateXXX(parameters), where EntityBeanClass is an Entity Bean you are trying to instantiate, ejbCreateXXX(parameters) methods are used for creating Entity Bean instances according to the parameters specified and to some programmer-defined conditions. </span><span style="font-size:10pt;font-family:Verdana;">A bean’s home interface may declare zero or more create() methods, each of which must have corresponding ejbCreate() and ejbPostCreate() methods in the bean class. These creation methods are linked at run time, so that when a create() method is invoked on the home interface, the container delegates the invocation to the corresponding ejbCreate() and ejbPostCreate() methods on the bean class. </span><strong><span style="font-size:10pt;font-family:Verdana;">2</span></strong><span style="font-size:10pt;font-family:Verdana;">. <strong>finder methods:</strong> The methods in the home interface that begin with “find” are called the find methods. These are used to query the EJB server for specific entity beans, based on the name of the method and arguments passed. Unfortunately, there is no standard query language defined for find methods, so each vendor will implement the find method differently. In CMP entity beans, the find methods are not implemented with matching methods in the bean class; containers implement them when the bean is deployed in a vendor specific manner. The deployer will use vendor specific tools to tell the container how a particular find method should behave. Some vendors will use object-relational mapping tools to define the behavior of a find method while others will simply require the deployer to enter the appropriate SQL command.</span><span style="font-size:10pt;font-family:Verdana;">There are two basic kinds of find methods: single-entity and multi-entity. Single-entity find methods return a remote reference to the one specific entity bean that matches the find request. If no entity beans are found, the method throws an ObjectNotFoundException . Every entity bean must define the single-entity find method with the method name findByPrimaryKey(), which takes the bean’s primary key type as an argument.</span><span style="font-size:10pt;font-family:Verdana;">The multi-entity find methods return a collection ( Enumeration or Collection type) of entities that match the find request. If no entities are found, the multi-entity find returns an empty collection. </span><strong><span style="font-size:10pt;font-family:Verdana;">3</span></strong><span style="font-size:10pt;font-family:Verdana;">. <strong>remove methods:</strong> These methods (you may have up to 2 remove methods, or don’t have them at all) allow the client to physically remove Entity beans by specifying either Handle or a Primary Key for the Entity Bean. </span><strong><span style="font-size:10pt;font-family:Verdana;">4</span></strong><span style="font-size:10pt;font-family:Verdana;">. <strong>home methods:</strong> These methods are designed and implemented by a developer, and EJB specification doesn’t have any requirements for them except the need to throw a RemoteException is each home method.</span><span></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2007/03/20/91-ejb-short-drill-one/#comments" title="Comment on 91. EJB Short Drill - One"><font face="Times New Roman">1 comment</font></a><font face="Times New Roman"> <em>March 20th, 2007</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2007/03/20/90-what-is-jndi/"><font face="Times New Roman">90. What is JNDI</font></a></span></h3>
<p><span><font face="Times New Roman">The Java Naming and Directory Interface (JNDI) is an application programming interface (API) for accessing different kinds of naming and directory services. JNDI is not specific to a particular naming or directory service, it can be used to access many different kinds of systems including file systems; distributed objects systems like CORBA, Java RMI, and EJB; and directory services like LDAP, Novell NetWare, and NIS+. </font></span><span><font face="Times New Roman">JNDI is similar to JDBC in that they are both Object-Oriented Java APIs that provide a common abstraction for accessing services from different vendors. While JDBC can be used to access a variety of relational databases, JNDI can be used to access a variety of of naming and directory services. Using one API to access many different brands of a service is possible because both JDBC and JNDI subscribe to the same architectural tenet: Define a common abstraction that most vendors can implement. The common abstraction is the API. It provides an objectified view of a service while hiding the details specific to any brand of service. The implementation is provided by the vendor, it plugs into the API and implements code specific to accessing that vendor’s product. </font></span><span><font face="Times New Roman">JNDI provides two APIs and one SPI. JNDI has a naming API that allows Java applications to access naming systems like CORBA’s Naming services and a directory API that extends the naming service to provide access to directory services like LDAP. JNDI also has a SPI (Service-Provider Interface) which is a programming model that vendors use to write JNDI plug-ins or implementations for their specific product. Each vendor’s plug-in is called a <em>service-provider</em>. A service-provider implements the JNDI APIs so that a Java application can access that vendor’s product. For the most part, JNDI hides the implementation details of the a service-provider so that Java developer that uses JNDI can use the same objects and method regardless of the brand of naming or directory service accessed. This is the real power behind APIs like JDBC and JNDI: They provide one programming model for accessing many different products; there is no need to learn a different programming model every time a different product is used.</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2007/03/20/90-what-is-jndi/#respond" title="Comment on 90. What is JNDI"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>March 20th, 2007</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2007/03/20/89-whats-the-difference-between-servletjsp-session-and-ejb-session/"><font face="Times New Roman">89. What’s the difference between servlet/JSP session and EJB Session</font></a></span></h3>
<p><span><font face="Times New Roman">A session in a Servlet, is maintained by the Servlet Container through the HttpSession object, that is acquired through the request object. You cannot really instantiate a new HttpSession object, and it doesn’t contains any business logic, but is more of a place where to store objects. </font></span><span><font face="Times New Roman">A session in EJB is maintained using the SessionBeans. You design beans that can contain business logic, and that can be used by the clients. You have two different session beans: Stateful and Stateless. The first one is somehow connected with a single client. It maintains the state for that client, can be used only by that client and when the client “dies” then the session bean is “lost”. </font></span><span><font face="Times New Roman">A Stateless Session Bean doesn’t maintain any state and there is no guarantee that the same client will use the same stateless bean, even for two calls one after the other. The lifecycle of a Stateless Session EJB is slightly different from the one of a Stateful Session EJB. Is EJB Container’s responsability to take care of knowing exactly how to track each session and redirect the request from a client to the correct instance of a Session Bean. The way this is done is vendor dependant, and is part of the contract. </font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2007/03/20/89-whats-the-difference-between-servletjsp-session-and-ejb-session/#comments" title="Comment on 89. What’s the difference between servlet/JSP session and EJB Session"><font face="Times New Roman">2 comments</font></a><font face="Times New Roman"> <em>March 20th, 2007</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2007/03/20/88-compare-stateless-and-statefull-session-bean/"><font face="Times New Roman">88. Compare stateless and statefull session bean</font></a></span></h3>
<table border="1" cellPadding="0" cellSpacing="0">
<tr>
<td vAlign="top" style="background:#cccccc;border:#ece9d8;padding:2.25pt;"><strong><font size="3"><font face="Times New Roman">Stateless Session Beans</font></font></strong></td>
<td vAlign="top" style="background:#cccccc;border:#ece9d8;padding:2.25pt;"><strong><font size="3"><font face="Times New Roman">Stateful Sessions Beans</font></font></strong></td>
</tr>
<tr>
<td vAlign="top" style="background-color:transparent;border:#ece9d8;padding:2.25pt;">
<p style="margin:0;" class="table"><font size="3" face="Times New Roman">Are pooled in memory, to save the overhead of creating a bean every time one is needed. WebLogic Server uses a bean instance when needed and puts it back in the pool when the work is complete. </font></p>
<p style="margin:0;" class="table"><font size="3" face="Times New Roman">Stateless sessions beans provide faster performance than stateful beans.</font></p>
</td>
<td vAlign="top" style="background-color:transparent;border:#ece9d8;padding:2.25pt;">
<p style="margin:0;" class="table"><font size="3" face="Times New Roman">Each client creates a new instance of a bean, and eventually removes it. Instances may be passivated to disk if the cache fills up.</font></p>
<p style="margin:0;" class="table"><font size="3" face="Times New Roman">An application issues an </font><code><span style="font-size:10pt;font-family:'Courier New';">ejbRemove()</span></code><font size="3" face="Times New Roman"> to remove the bean from the cache.</font></p>
<p style="margin:0;" class="table"><font size="3" face="Times New Roman">Stateful sessions beans do not perform as well as stateless sessions beans.</font></p>
</td>
</tr>
<tr>
<td vAlign="top" style="background-color:transparent;border:#ece9d8;padding:2.25pt;">
<p style="margin:0;" class="table"><font size="3" face="Times New Roman">Have no identity and no client association; they are anonymous.</font></p>
</td>
<td vAlign="top" style="background-color:transparent;border:#ece9d8;padding:2.25pt;">
<p style="margin:0;" class="table"><font size="3" face="Times New Roman">Are bound to particular client instances.Each bean has an implicit identity. Each time a client interacts with a stateful session bean during a session, it is the same object.</font></p>
</td>
</tr>
<tr>
<td vAlign="top" style="background-color:transparent;border:#ece9d8;padding:2.25pt;">
<p style="margin:0;" class="table"><font size="3" face="Times New Roman">Do not persist. The bean has no state between calls.</font></p>
</td>
<td vAlign="top" style="background-color:transparent;border:#ece9d8;padding:2.25pt;">
<p style="margin:0;" class="table"><font size="3" face="Times New Roman">Persist. A stateful session bean’s state is preserved for the duration of a session.</font></p>
</td>
</tr>
</table>
<p><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2007/03/20/88-compare-stateless-and-statefull-session-bean/#comments" title="Comment on 88. Compare stateless and statefull session bean"><font face="Times New Roman">1 comment</font></a><font face="Times New Roman"> <em>March 20th, 2007</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2007/03/20/87-how-to-choose-between-stateless-and-statefull-beans/"><font face="Times New Roman">87. How to choose between stateless and statefull beans</font></a></span></h3>
<p><span><font face="Times New Roman">Stateless session beans are a good choice if your application does not need to maintain state for a particular client between business method calls. WebLogic Server is multi-threaded, servicing multiple clients simultaneously. With stateless session beans, the EJB container is free to use any available, pooled bean instance to service a client request, rather than reserving an instance for each client for the duration of a session. This results in greater resource utilization, scalability and throughput. </font></span><span><font face="Times New Roman">Stateless session beans are preferred for their light-weight implementation. They are a good choice if your application’s beans perform autonomous, distinct tasks without bean-to-bean interaction. </font></span><span><font face="Times New Roman">Stateful session beans are a good choice if you need to preserve the bean’s state for the duration of the session. </font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2007/03/20/87-how-to-choose-between-stateless-and-statefull-beans/#respond" title="Comment on 87. How to choose between stateless and statefull beans"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>March 20th, 2007</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2007/03/20/what-is-the-difference-between-cmp-and-bmp/"><font face="Times New Roman">What is the difference between CMP and BMP</font></a></span></h3>
<p><span><font face="Times New Roman">Short answer: with bean-managed persistence, you can optimize your queries and improve performance over the generalized container-managed heuristics. But container-managed persistence is very convenient, and vendors will be working to improve its performance as time goes on.] </font></span><span><font face="Times New Roman">There is of course a difference as many CMPs use O-R mapping using metadata, which is slower than hardcoded queries (except vendors like GemStone that use a OODB which is slow anyway!) As always, a lot depends on the database schema. Given that CMP is still evolving, complex relationships (e.g.inheritance) and distributed transactions are not even supported by most EJB server vendors, leave alone performance. </font></span><span><font face="Times New Roman">Having said that however, it does not seem right to compare BMP and CMP on performance because the motivation of CMP is precisely to relieve bean providers from thinking about this! In (J2EE) theory, a good CMP implementation should perform well in a production environment; in practice, except for a couple of vendors who have traditionally been strong in persistent storage space (e.g. Persistence Software, GemStone) you will not find great CMP support at this very moment. </font></span><span><font face="Times New Roman">BMP offers a tactical approach while CMP is more strategic. Which implies that if you can work-around some (perhaps severe) limitations for near-term, there may be much to gain with CMP as the vendor offering matures.</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2007/03/20/what-is-the-difference-between-cmp-and-bmp/#comments" title="Comment on What is the difference between CMP and BMP"><font face="Times New Roman">1 comment</font></a><font face="Times New Roman"> <em>March 20th, 2007</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/10/09/85-explain-about-xml-attributes/"><font face="Times New Roman">85. Explain about XML attributes</font></a></span></h3>
<p><span><font face="Times New Roman">Attributes are simple name/value pairs associated with an element. </font></span><span><font face="Times New Roman">They are attached to the start-tag, as shown below, but not to the end-tag: </font></span><span><font face="Times New Roman">&lt;name nickname=”sharat”&gt;<br />
&lt;first&gt;sharat&lt;/first&gt;<br />
&lt;middle&gt;&lt;/middle&gt;<br />
&lt;last&gt;lastname&lt;/last&gt;<br />
&lt;/name&gt; </font></span><span><font face="Times New Roman">Attributes must have values–even if that value is just an empty string (like “”)–and those values must be in quotes. </font></span><span><font face="Times New Roman">Attributes Can Provide Meta Data that May Not be Relevant to Most Applications Dealing with Our XML </font></span><span><font face="Times New Roman">Attributes are Un-Ordered </font></span><span><font face="Times New Roman">Elements Can Be More Complex Than Attributes.</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/10/09/85-explain-about-xml-attributes/#respond" title="Comment on 85. Explain about XML attributes"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>October 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/10/09/84-what-do-you-mean-by-well-formed-xml/"><font face="Times New Roman">84. What do you mean by well-formed XML</font></a></span></h3>
<p><span><font face="Times New Roman">XML documents must adhere to following rules to be well-formed. </font></span></p>
<ol>
<li class="MsoNormal"><span><font face="Times New Roman">Every start-tag must have a matching end-tag, or be a self-closing tag </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Tags can’t overlap </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">XML documents can have only one root element </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Element names must obey XML naming conventions </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">XML is case-sensitive </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">XML will keep whitespace in your text </font></span></li>
</ol>
<p><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/10/09/84-what-do-you-mean-by-well-formed-xml/#respond" title="Comment on 84. What do you mean by well-formed XML"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>October 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/27/83-what-are-the-differences-between-sax-and-dom-parser/"><font face="Times New Roman">83. What are the differences between SAX and DOM parser.</font></a></span></h3>
<table border="1" width="100%" cellPadding="0" cellSpacing="0" style="width:100%;border-collapse:collapse;">
<tr style="height:12pt;">
<td width="50%" style="width:50%;height:12pt;background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><strong><span style="font-size:10pt;color:red;font-family:Verdana;">SAX</span></strong></p>
</td>
<td width="50%" style="width:50%;height:12pt;background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><strong><span style="font-size:10pt;color:red;font-family:Verdana;">DOM</span></strong></p>
</td>
</tr>
</table>
<p><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/27/83-what-are-the-differences-between-sax-and-dom-parser/#comments" title="Comment on 83. What are the differences between SAX and DOM parser."><font face="Times New Roman">5 comments</font></a><font face="Times New Roman"> <em>September 27th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/26/82-describe-the-differences-between-xml-and-html/"><font face="Times New Roman">82. Describe the differences between XML and HTML</font></a></span></h3>
<table border="1" cellPadding="0" style="border:3pt outset;">
<tr>
<td width="50%" vAlign="top" style="width:50%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><strong><font size="3" face="Times New Roman">XML</font></strong></p>
</td>
<td width="50%" vAlign="top" style="width:50%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><strong><font size="3" face="Times New Roman">HTML</font></strong></p>
</td>
</tr>
<tr>
<td width="50%" vAlign="top" style="width:50%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;"><font size="3" face="Times New Roman">User definable tags</font></p>
</td>
<td width="50%" vAlign="top" style="width:50%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;"><font size="3" face="Times New Roman">Defined set of tags designed for web display</font></p>
</td>
</tr>
<tr>
<td width="50%" vAlign="top" style="width:50%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;"><font size="3" face="Times New Roman">Content driven</font></p>
</td>
<td width="50%" vAlign="top" style="width:50%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;"><font size="3" face="Times New Roman">Format driven</font></p>
</td>
</tr>
<tr>
<td width="50%" vAlign="top" style="width:50%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;"><font size="3" face="Times New Roman">End tags required for well formed documents</font></p>
</td>
<td width="50%" vAlign="top" style="width:50%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;"><font size="3" face="Times New Roman">End tags not required</font></p>
</td>
</tr>
<tr>
<td width="50%" vAlign="top" style="width:50%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;"><font size="3" face="Times New Roman">Quotes required around attributes values</font></p>
</td>
<td width="50%" vAlign="top" style="width:50%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;"><font size="3" face="Times New Roman">Quotes not required</font></p>
</td>
</tr>
<tr>
<td width="50%" vAlign="top" style="width:50%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;"><font size="3" face="Times New Roman">Slash required in empty tags</font></p>
</td>
<td width="50%" vAlign="top" style="width:50%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;"><font size="3" face="Times New Roman">Slash not required</font></p>
</td>
</tr>
</table>
<p><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/26/82-describe-the-differences-between-xml-and-html/#respond" title="Comment on 82. Describe the differences between XML and HTML"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 26th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/81-how-you-will-make-available-any-message-resources-definations-file-to-the-struts-famework-environment/"><font face="Times New Roman">81. How you will make available any message resources definations file to the struts famework environment.</font></a></span></h3>
<p><span><font face="Times New Roman">Message Resources Definitions file are simple .properties files and these files contains the messages that can be used in the struts project. Message Resources Definitions files can be added to the struts-config.xml file through <strong>&lt;message-resources /&gt;</strong> tag.<br />
<strong>Example:</strong><br />
<span style="color:blue;">&lt;message-resources parameter=”MessageResources” /&gt;</span></font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/81-how-you-will-make-available-any-message-resources-definations-file-to-the-struts-famework-environment/#comments" title="Comment on 81. How you will make available any message resources definations fil "><font face="Times New Roman">1 comment</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/80-what-is-jakarta-struts-framework/"><font face="Times New Roman">80. What is Jakarta Struts FrameWork</font></a></span></h3>
<p><span><font face="Times New Roman">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.</font></span><span><a href="http://sharat.files.wordpress.com/2006/09/WindowsLiveWriter/80.WhatisJakartaStrutsFrameWork_85F5/strutsdiagram[8].jpg"><span style="text-decoration:none;"></span></a></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/80-what-is-jakarta-struts-framework/#respond" title="Comment on 80. What is Jakarta Struts FrameWork"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/79-what-are-the-core-classes-of-the-struts-frame-work/"><font face="Times New Roman">79. What are the core classes of the Struts Frame Work</font></a></span></h3>
<p><span><font face="Times New Roman">Core classes of Struts Framework are ActionForm, Action, ActionMapping, ActionForward, ActionServlet etc.</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/79-what-are-the-core-classes-of-the-struts-frame-work/#respond" title="Comment on 79. What are the core classes of the Struts Frame Work"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/78-what-are-the-components-of-struts/"><font face="Times New Roman">78. What are the components of Struts</font></a></span></h3>
<p><span><font face="Times New Roman">Struts is based on the MVC design pattern. Struts components can be categories into <strong>Model</strong>, <strong>View</strong> and <strong>Controller</strong>.</font></span><font face="Times New Roman"><strong><span style="color:red;">Model:</span></strong><strong><span> </span></strong><span>Components like business logic / business processes and data are the part of Model.</span></font><font face="Times New Roman"><strong><span style="color:red;">View:</span></strong><span> JSP, HTML etc. are part of View</span></font><font face="Times New Roman"><strong><span style="color:red;">Controller:</span></strong><span> Action Servlet of Struts is part of Controller components which works as front controller to handle all the requests.</span></font><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/78-what-are-the-components-of-struts/#respond" title="Comment on 78. What are the components of Struts"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/77-what-are-differences-between-beanmessage-and-beanwrite/"><font face="Times New Roman">77. What are differences between &lt;bean:message&gt; and &lt;bean:write&gt;</font></a></span></h3>
<p><font face="Times New Roman"><strong><span style="color:red;">&lt;bean:message&gt;</span></strong><span style="color:red;">:</span><span> This tag is used to output locale-specific text (from the properties files) from a MessageResources bundle. </span></font><font face="Times New Roman"><span style="color:blue;">&lt;bean:message key=”label.search.name”/&gt;</span><span></span></font><font face="Times New Roman"><strong><span style="color:red;">&lt;bean:write&gt;</span></strong><span style="color:red;">:</span><span> This tag is used to output property values from a bean. &lt;bean:write&gt; is a commonly used tag which enables the programmers to easily present the data.</span></font><font face="Times New Roman"><span style="color:blue;">&lt;bean:write name=”student” property=”age”/&gt;</span><span></span></font><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/77-what-are-differences-between-beanmessage-and-beanwrite/#comments" title="Comment on 77. What are differences between &lt;bean:message&gt; and &lt;bean:write&gt;"><font face="Times New Roman">3 comments</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/76-what-is-lookupdispatchaction/"><font face="Times New Roman">76. What is LookupDispatchAction</font></a></span></h3>
<p><span><font face="Times New Roman">An abstract Action that dispatches to the subclass mapped execute method. This is useful in cases where an HTML form has multiple submit buttons with the same name. The button name is specified by the parameter property of the corresponding ActionMapping. </font></span><span><font face="Times New Roman">(Ref. </font><a href="http://struts.apache.org/1.2.7/api/org/apache/struts/actions/LookupDispatchAction.html"><font face="Times New Roman">http://struts.apache.org/1.2.7/api/org/apache/struts/actions/LookupDispatchAction.html</font></a><font face="Times New Roman">).</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/76-what-is-lookupdispatchaction/#respond" title="Comment on 76. What is LookupDispatchAction"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/75-what-is-struts-flow/"><font face="Times New Roman">75. What is struts flow</font></a></span></h3>
<p><span><font face="Times New Roman">Struts Flow is a port of Cocoon’s Control Flow to Struts to allow complex workflow, like multi-form wizards, to be easily implemented using continuations-capable JavaScript. It provides the ability to describe the order of Web pages that have to be sent to the client, at any given point in time in an application. </font></span><span><font face="Times New Roman">The code is based on a proof-of-concept Dave Johnson put together to show how the Control Flow could be extracted from Cocoon. (Ref: </font><a href="http://struts.sourceforge.net/struts-flow/index.html"><font face="Times New Roman">http://struts.sourceforge.net/struts-flow/index.html</font></a><font face="Times New Roman"> )</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/75-what-is-struts-flow/#respond" title="Comment on 75. What is struts flow"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/74-how-to-handle-exceptions-in-struts/"><font face="Times New Roman">74. How to handle exceptions in struts</font></a></span></h3>
<p><span><font face="Times New Roman">In Struts you can handle the exceptions in two ways:<br />
<strong><span style="color:red;">a) Declarative Exception Handling:</span> </strong>You can either define global exception handling tags in your struts-config.xml or define the exception handling tags within &lt;action&gt;..&lt;/action&gt; tag.<br />
<strong>Example:</strong></font></span><font face="Times New Roman"><span style="color:blue;">&lt;exception   key=”database.error.duplicate”<br />
                    path=”/UserExists.jsp”<br />
                    type=”mybank.account.DuplicateUserException”/&gt; </span><span></span></font><font face="Times New Roman"><strong><span style="color:red;">b) Programmatic Exception Handling:</span></strong><span> Here you can use try{}catch{} block to handle the exception. </span></font><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/74-how-to-handle-exceptions-in-struts/#comments" title="Comment on 74. How to handle exceptions in struts"><font face="Times New Roman">2 comments</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/73-what-is-the-action-errors-and-action-message/"><font face="Times New Roman">73. What is the Action Errors and Action Message?</font></a></span></h3>
<p><font face="Times New Roman"><strong><span style="color:red;">ActionMessage:</span></strong><span style="color:red;"> </span></font><span><font face="Times New Roman">A class that encapsulates messages. Messages can be either global or they are specific to a particular bean property.<br />
Each individual message is described by an ActionMessage object, which contains a message key (to be looked up in an appropriate message resources database), and up to four placeholder arguments used for parametric substitution in the resulting message.</font></span><font face="Times New Roman"><span style="color:blue;">data.ok=Data saved successfully </span><span></span></font><font face="Times New Roman"><span style="color:blue;">ActionMessages messages = new ActionMessages();<br />
ActionMessage msg = new ActionMessage(”data.ok”);<br />
messages.add(”message1″, msg); </span><span></span></font><font face="Times New Roman"><span style="color:blue;">&lt;html:messages id=”msgId” message=”true” property=”message1″&gt;<br />
       &lt;bean:write name=”msgId”/&gt;&lt;br&gt;<br />
&lt;/html:messages&gt; </span><span></span></font><font face="Times New Roman"><span style="color:blue;">msg = new ActionMessage(”data.do”, “stop”);<br />
messages.add(”message2″, msg); </span><span></span></font><font face="Times New Roman"><span style="color:blue;">In property file data.do configured as below<br />
data.do=Please {0}</span><span> </span></font><font face="Times New Roman"><strong><span style="color:red;">ActionErrors:</span></strong><span> 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 (and, therefore, a particular input field on the corresponding form).</span></font><font face="Times New Roman"><span style="color:blue;">error.name.required=Please enter the name<br />
ActionErrors errors = new ActionErrors();<br />
errors.add(”name”,new ActionMessage(”error.name.required”));</span><span></span></font><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/73-what-is-the-action-errors-and-action-message/#comments" title="Comment on 73. What is the Action Errors and Action Message?"><font face="Times New Roman">2 comments</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/72-how-to-setup-struts-to-use-multiple-configuration-files/"><font face="Times New Roman">72. How to setup struts to use multiple configuration files.</font></a></span></h3>
<p><span><font face="Times New Roman">Struts can use multiple configuration files. Here is the configuration example:<br />
</font><span style="color:blue;"><font face="Times New Roman">&lt;servlet&gt;<br />
        &lt;servlet-name&gt;banking&lt;/servlet-name&gt;<br />
        &lt;servlet-class&gt;org.apache.struts.action.ActionServlet<br />
        &lt;/servlet-class&gt;<br />
        &lt;init-param&gt;<br />
        &lt;param-name&gt;config&lt;/param-name&gt;<br />
</font><font face="Times New Roman"><strong>        &lt;param-value&gt;/WEB-INF/struts-config.xml,<br />
                /WEB-INF/struts-authentication.xml,<br />
                /WEB-INF/struts-help.xml<br />
           &lt;/param-value&gt;<br />
</strong>    &lt;/init-param&gt;</font></span></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/72-how-to-setup-struts-to-use-multiple-configuration-files/#comments" title="Comment on 72. How to setup  struts to use multiple configuration files."><font face="Times New Roman">1 comment</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/71-what-we-will-define-in-struts-configxml-file/"><font face="Times New Roman">71. What we will define in struts-config.xml file.</font></a></span></h3>
<p><span><font face="Times New Roman">The main control file in the Struts framework is the struts-config.xml XML file, where action mappings are specified. </font></span><span><font face="Times New Roman">This file’s structure is described by the struts-config DTD file, which is defined at http://jakarta.apache.org/struts/. A copy of the DTD can be found on the /docs/dtds subdirectory of the framework’s installation root directory. The top-level element is struts-config. </font></span><span><font face="Times New Roman">Basically, it consists of the following elements:<br />
<strong><span style="color:red;">data-sources</span></strong>—A set of data-source elements, describing parameters needed to instantiate JDBC 2.0 Standard Extension DataSource objects </font></span><font face="Times New Roman"><span style="color:blue;">&lt;data-sources&gt;<br />
      &lt;data-source    autoCommit=”false”<br />
                              description=”Second Database Config”<br />
                              driverClass=”oracle.jdbc.driver.OracleDriver”<br />
                               key=”REFDB”<br />
                               maxCount=”4″ <br />
                               minCount=”2″<br />
                               password=”admin”<br />
                               url=”jdbc:oracle:thin:@localhost:1521/AUTHORDB”<br />
                               user=”admin”<br />
        /&gt;<br />
&lt;/data-sources&gt; </span><span></span></font><font face="Times New Roman"><strong><span style="color:red;">form-beans</span></strong><span>—A set of form-bean elements that describe the form beans that this application uses </span></font><font face="Times New Roman"><span style="color:blue;">&lt;form-beans&gt;<br />
         &lt;form-bean name=”searchForm”&gt;<br />
                            type=”com.abc.struts.SearchForm”<br />
        &lt;/form-bean&gt;<br />
&lt;/form-beans&gt; </span><span></span></font><font face="Times New Roman"><strong><span style="color:red;">global-forwards</span></strong><span>—A set of forward elements describing general available forward URIs </span></font><font face="Times New Roman"><span style="color:blue;">&lt;global-forwards&gt;<br />
          &lt;forward name=”search” path=”/search.jsp” /&gt;<br />
&lt;/global-forwards&gt; </span><span></span></font><span><br />
<font face="Times New Roman"><strong><span style="color:red;">action-mappings</span></strong>—A set of action elements describing a request-to-action mapping</font></span><font face="Times New Roman"><span style="color:blue;">&lt;action-mapping&gt;<br />
         &lt;action path=”/search”<br />
                     type=”com.abc.struts.SearchAction”<br />
                     name=”searchForm”<br />
                     scope=”request”<br />
                     validate=”true”<br />
                     input=”/search.jsp”&gt;<br />
          &lt;forward name=”results” path=”/results.jsp” /&gt;<br />
         &lt;/action&gt;<br />
&lt;/action-mapping&gt;</span><span></span></font><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/71-what-we-will-define-in-struts-configxml-file/#comments" title="Comment on 71. What we will define in struts-config.xml file."><font face="Times New Roman">1 comment</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/70-what-is-the-request-processor-in-struts-and-how-it-works/"><font face="Times New Roman">70. What is the request processor in struts and how it works</font></a></span></h3>
<p><font face="Times New Roman"><strong><span style="color:red;">The RequestProcessor</span></strong><span> Class is the actual place where the request processing takes place in a Struts controller environment.</span></font><span><font face="Times New Roman">When the request object first reaches the actionservlet class then it invokes the process method of the underlying RequestProcessor Class.</font></span><span><font face="Times New Roman">This process method then looks into the <strong><u>struts-config.xml</u></strong> file and tries to locate the name of the action that has come with the request.Once it identifies the action in the xml file it continues the rest of the steps needed for request processing.</font></span><span><font face="Times New Roman">processor has most of the following responsibilities: </font></span></p>
<ol>
<li class="MsoNormal"><span><font face="Times New Roman">Determine path, </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Handle Locale, </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Process content and encoding type, </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Process cache headers </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Pre Processing hook </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Pre-processing hook, </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Determine mapping, </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Determine roles, </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Process and validate actionForm, </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Return a response </font></span></li>
</ol>
<p><span><font face="Times New Roman">It is one instance per application module; it invokes proper Action instance </font></span><span><font face="Times New Roman">Of course processes all requests for a module.</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/70-what-is-the-request-processor-in-struts-and-how-it-works/#comments" title="Comment on 70. What is the request processor in struts and how it works"><font face="Times New Roman">5 comments</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/69-what-is-struts-actions-and-action-mappings/"><font face="Times New Roman">69. What is struts actions and action mappings.</font></a></span></h3>
<p><span><font face="Times New Roman">A Struts action is an instance of a subclass of an Action class, which implements a portion of a Web application and whose perform or execute method returns a forward.</font></span><span><font face="Times New Roman">An action can perform tasks such as validating a user name and password.</font></span><span><font face="Times New Roman">An action mapping is a configuration file entry that, in general, associates an action name with an action. An action mapping can contain a reference to a form bean that the action can use, and can additionally define a list of local forwards that is visible only to this action.</font></span><span><font face="Times New Roman">An action servlet is a servlet that is started by the servlet container of a Web server to process a request that invokes an action. The servlet receives a forward from the action and asks the servlet container to pass the request to the forward’s URL. </font></span><span><font face="Times New Roman">An action servlet must be an instance of an org.apache.struts.action.ActionServlet class or of a subclass of that class. An action servlet is the primary component of the controller.</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/69-what-is-struts-actions-and-action-mappings/#comments" title="Comment on 69. What is struts actions and action mappings."><font face="Times New Roman">2 comments</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/68-what-are-the-various-struts-tag-libraries/"><font face="Times New Roman">68. What are the various struts tag libraries.</font></a></span></h3>
<p><span><font face="Times New Roman">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:</font></span></p>
<ol>
<li class="MsoNormal"><font face="Times New Roman"><strong><span style="color:red;">Bean tag library</span></strong><span> &#8211; Tags for accessing JavaBeans and their properties. </span></font></li>
<li class="MsoNormal"><font face="Times New Roman"><strong><span style="color:red;">HTML tag library</span></strong><span> &#8211; Tags to output standard HTML, including forms, text boxes, checkboxes, radio buttons etc.. </span></font></li>
<li class="MsoNormal"><font face="Times New Roman"><strong><span style="color:red;">Logic tag library</span></strong><span> &#8211; Tags for generating conditional output, iteration capabilities and flow management </span></font></li>
<li class="MsoNormal"><font face="Times New Roman"><strong><span style="color:red;">Tiles or Template tag library </span></strong><span>- For the application using tiles </span></font></li>
<li class="MsoNormal"><font face="Times New Roman"><strong><span style="color:red;">Nested tag library</span></strong><span> &#8211; For using the nested beans in the application </span></font></li>
</ol>
<p><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/68-what-are-the-various-struts-tag-libraries/#respond" title="Comment on 68. What are the various struts tag libraries."><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/67-what-is-the-difference-between-perform-and-execute/"><font face="Times New Roman">67. What is the difference between perform() and execute()</font></a></span></h3>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">Perform method is the method which was deprecated in the Struts Version 1.1.  </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">In Struts 1.x, Action.perform() is the method called by the ActionServlet. This is typically where your business logic resides, or at least the flow control to your JavaBeans and EJBs that handle your business logic. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">As we already mentioned, to support declarative exception handling, the method signature changed in perform. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Now execute just throws Exception. Action.perform() is now deprecated; </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">however, the Struts v1.1 ActionServlet is smart enough to know whether or not it should call perform or execute in the Action, depending on which one is available. </font></span></li>
</ul>
<p><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/67-what-is-the-difference-between-perform-and-execute/#comments" title="Comment on 67. What is the difference between perform() and execute()"><font face="Times New Roman">6 comments</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/66how-to-enable-front-end-validation-based-on-the-xml-in-validationxml/"><font face="Times New Roman">66.How to enable front-end validation based on the xml in validation.xml</font></a></span></h3>
<p><span><font face="Times New Roman">The &lt;html:javascript&gt; tag to allow front-end validation based on the xml in validation.xml. </font></span><span><font face="Times New Roman">For  example the code: </font></span><font face="Times New Roman"><span style="color:blue;">&lt;html:javascript  formName=”logonForm”<br />
                         dynamicJavascript=”true”<br />
                         staticJavascript=”true”  /&gt;</span><span></span></font><span><font face="Times New Roman"> generates the client side java script for the form “logonForm” as defined in the  validation.xml file. </font></span><span><font face="Times New Roman">The <strong>&lt;html:javascript&gt;</strong> when added in the jsp file generates the client site validation script.</font></span><span><font face="Times New Roman">Following tag displays all the errors:<br />
<strong>&lt;html:errors/&gt;</strong></font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/66how-to-enable-front-end-validation-based-on-the-xml-in-validationxml/#respond" title="Comment on 66.How to enable front-end validation based on the xml in validation.xml"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/65-what-is-the-struts-validator-framework/"><font face="Times New Roman">65. What is the Struts Validator Framework</font></a></span></h3>
<p><span><font face="Times New Roman">Struts Framework provides the functionality to validate the form data. </font></span><span><font face="Times New Roman">It can be use to validate the data on the users browser as well as on the server side. </font></span><span><font face="Times New Roman">Struts Framework emits the java scripts and it can be used validate the form data on the client browser. </font></span><span><font face="Times New Roman">Server side validation of form can be accomplished by sub classing your From Bean with DynaValidatorForm class. </font></span><span><font face="Times New Roman">Following are the core components of this framework: </font></span></p>
<ol>
<li class="MsoNormal"><span><font face="Times New Roman">Validators </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Configuration Files </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Resource Files/Bundles </font></span></li>
<li class="MsoNormal"><span><a href="http://www.devarticles.com/"><font face="Times New Roman">JSP</font></a><font face="Times New Roman"> Custom Tags </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Validator Form Classes </font></span></li>
</ol>
<p><span><font face="Times New Roman">The Validator Framework uses two XML configuration files <strong><span style="color:red;">validator-rules.xml</span></strong> and <strong><span style="color:red;">validation.xml</span></strong>. The <strong>validator-rules.xml</strong> defines the standard validation routines, these are reusable and used in <strong>validation.xml</strong>. to define the form specific validations. The <strong>validation.xml</strong> defines the validations applied to a form bean.</font></span><span><font face="Times New Roman">The Validator framework was developed by David Winterfeldt as third-party add-on to Struts. Now the Validator framework is a part of Jakarta Commons project and it can be used with or without Struts. The Validator framework comes integrated with the Struts Framework and can be used without doing any extra settings.</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/65-what-is-the-struts-validator-framework/#respond" title="Comment on 65. What is the Struts Validator Framework"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/64-what-is-action-class/"><font face="Times New Roman">64. What is Action Class</font></a></span></h3>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">The Action is part of the controller. The purpose of Action Class is to translate the HttpServletRequest to the business logic. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">To use the Action, we need to  Subclass and overwrite the execute()  method. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">The ActionServlet (commad) passes the parameterized class to Action Form using the execute() method. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">There should be no database interactions in the action. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">The action should receive the request, call business objects (which then handle database, or interface with J2EE, etc) and then determine where to go next. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Even better, the business objects could be handed to the action at runtime (IoC style) thus removing any dependencies on the model.   </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">The return type of the execute method is ActionForward which is used by the Struts </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Framework to forward the request to the file as per the value of the returned ActionForward object. </font></span></li>
</ul>
<p><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/64-what-is-action-class/#respond" title="Comment on 64. What is Action Class"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/63-explain-actionfrom-class-and-its-life-cycle/"><font face="Times New Roman">63. Explain ActionFrom Class and its life cycle.</font></a></span></h3>
<p><span><font face="Times New Roman">An ActionForm is a java bean that extends org.apache.struts.action.ActionForm, ActionForm <strong><u>maintain the session state of web application</u></strong>.</font></span><span><font face="Times New Roman">All data submitted by the user are sent corresponding ActionForm  </font></span><span><font face="Times New Roman">ActionForm class is used to capture user-input data from an HTML form and transfer it to the Action Class. ActionForm plays the role of Transport Vehicle between the presentation Tire &amp; Business Tier.<br />
<strong><span style="color:red;">Life Cycle :</span></strong></font><strong><span style="color:red;"><br />
</span></strong><font face="Times New Roman">1. Request received by Controller<br />
2. Create or recycle ActionForm<br />
3. Call reset()<br />
4. store ActionForm in proper scope<br />
5. populate ActionForm from Request<br />
6. Validate the ActionForm<br />
7. If errors found then forward back to input attribute page(configured in Action mapping in struts-config.xml) with ActionForm in scope. If no errors found then call execute() with the ActionForm.</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/63-explain-actionfrom-class-and-its-life-cycle/#respond" title="Comment on 63. Explain ActionFrom Class and its life cycle."><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/62-what-are-new-features-of-struts-11/"><font face="Times New Roman">62. What are new features of Struts 1.1</font></a></span></h3>
<p><span><font face="Times New Roman">The new features added to Struts 1.1 are</font></span><span><font face="Times New Roman">1. RequestProcessor class<br />
2. Method perform() replaced by execute() in Struts base Action Class<br />
3. Changes to web.xml and struts-config.xml<br />
4.Declarative exception handling<br />
5.Dynamic ActionForms<br />
6.Plug-ins<br />
7.Multiple Application Modules<br />
8.Nested Tags<br />
9.The Struts Validator<br />
10.Change to the ORO package<br />
11.Change to Commons logging<br />
12.Removal of Admin actions<br />
13. Deprecation of the GenericDataSource.</font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/61-what-is-a-action-servlet/"><font face="Times New Roman">61. What is a Action Servlet</font></a></span></h3>
<p><font face="Times New Roman"><span style="color:red;">public class <strong>ActionServlet</strong> </span><span></span></font><font face="Times New Roman"><span style="color:red;">extends javax.servlet.http.HttpServlet</span><span></span></font></p>
<ol>
<li class="MsoNormal"><font face="Times New Roman"><strong><span>ActionServlet</span></strong><span> provides the “controller” in the Model-View-Controller (MVC) design pattern for web applications that is commonly known as “Model 2″. </span></font></li>
<li class="MsoNormal"><span><font face="Times New Roman">The class org.apache.struts.action.ActionServlet is the called the ActionServlet. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">In the the Jakarta Struts Framework this class plays the role of controller. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">All the requests to the server goes through the controller. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Controller is responsible for handling all the requests. </font></span></li>
</ol>
<p><strong><span style="font-size:9pt;color:#333333;"><font face="Verdana"> </font></span></strong><strong><span style="font-size:9pt;color:#333333;"><font face="Verdana"> </font></span></strong><strong><span style="font-size:9pt;color:#333333;"><font face="Verdana"> </font></span></strong></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/60-what-is-the-difference-between-type_scroll_insensitive-and-type_scroll_sensitive/"><font face="Times New Roman">60. What is the difference between TYPE_SCROLL_INSENSITIVE and TYPE_SCROLL_SENSITIVE</font></a></span></h3>
<p><span><font face="Times New Roman">You will get a scrollable ResultSet object if you specify one of these ResultSet constants.The difference between the two has to do with whether a result set reflects changes that are made to it while it is open and whether certain methods can be called to detect these changes. Generally speaking, a result set that is TYPE_SCROLL_INSENSITIVE does not reflect changes made while it is still open and one that is TYPE_SCROLL_SENSITIVE does. All three types of result sets will make changes visible if they are closed and then reopened: </font></span></p>
<pre style="margin:0 0.5in;"><span><font size="2"> </font></span></pre>
<p><span><font face="Times New Roman">Statement stmt =<br />
con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);<br />
ResultSet srs =<br />
stmt.executeQuery(”SELECT COF_NAME, PRICE FROM COFFEES”);<br />
srs.afterLast();<br />
while (srs.previous())<br />
{<br />
String name = srs.getString(”COF_NAME”);<br />
float price = srs.getFloat(”PRICE”);<br />
System.out.println(name + ” ” + price);<br />
}</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/60-what-is-the-difference-between-type_scroll_insensitive-and-type_scroll_sensitive/#comments" title="Comment on 60. What is the difference between TYPE_SCROLL_INSENSITIVE and TYPE_SCROLL_SENSITIVE"><font face="Times New Roman">2 comments</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/59-how-to-make-updates-to-updatable-result-sets/"><font face="Times New Roman">59. How to make updates to updatable result sets.</font></a></span></h3>
<p><span><font face="Times New Roman">Another new feature in the JDBC 2.0 API is the ability to update rows in a result set using methods in the Java programming language rather than having to send an SQL command. But before you can take advantage of this capability, you need to create a ResultSet object that is updatable. In order to do this, you supply the ResultSet constant <strong><span style="color:red;">CONCUR_UPDATABLE</span></strong> to the createStatement method.</font></span><span><font face="Times New Roman">E.g. </font></span><span><font face="Times New Roman">Connection con = DriverManager.getConnection(”jdbc:mySubprotocol:mySubName”); </font></span><span><font face="Times New Roman">Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); </font></span><span><font face="Times New Roman">ResultSet uprs = (”SELECT COF_NAME, PRICE FROM COFFEES”);</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/59-how-to-make-updates-to-updatable-result-sets/#respond" title="Comment on 59. How to make updates to updatable result sets."><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/58-how-to-retrieve-warnings/"><font face="Times New Roman">58. How to Retrieve Warnings</font></a></span></h3>
<p><span><font face="Times New Roman">SQLWarning objects are a subclass of SQLException that deal with database access warnings. Warnings do not stop the execution of an application, as exceptions do; they simply alert the user that something did not happen as planned. A warning can be reported on a Connection object, a Statement object (including PreparedStatement and CallableStatement objects), or a ResultSet object. Each of these classes has a getWarnings method, which you must invoke in order to see the first warning reported on the calling object</font></span><span><font face="Times New Roman">E.g. </font></span><span><font face="Times New Roman">SQLWarning warning = stmt.getWarnings(); </font></span><span><font face="Times New Roman">if (warning != null) { </font></span><span><font face="Times New Roman">while (warning != null) { </font></span><span><font face="Times New Roman">System.out.println(”Message: ” + warning.getMessage()); </font></span><span><font face="Times New Roman">System.out.println(”SQLState: ” + warning.getSQLState()); </font></span><span><font face="Times New Roman">System.out.print(”Vendor error code: “); </font></span><span><font face="Times New Roman">System.out.println(warning.getErrorCode()); </font></span><span><font face="Times New Roman">warning = warning.getNextWarning(); </font></span><span><font face="Times New Roman">} </font></span><span><font face="Times New Roman">}</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/58-how-to-retrieve-warnings/#respond" title="Comment on 58. How to Retrieve Warnings"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/56-how-can-you-load-the-driver/"><font face="Times New Roman">57. How can you load the driver.</font></a></span></h3>
<p><span><font face="Times New Roman">Driver can be loaded in 2 ways :</font></span><span><font face="Times New Roman">1. class.forName(”sun.jdbc.odbc.JdbcOdbc”); </font></span><span><font face="Times New Roman">2. Driver d=new Driver(”sun.jdbc.odbc.JdbcOdbc”); </font></span><span><font face="Times New Roman">   DriverManager.registerDriver(d);</font></span><span><font face="Times New Roman">vendar specific one: Driver d=new oracle.driver.oracleDriver();</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/56-how-can-you-load-the-driver/#respond" title="Comment on 57. How can you load the driver."><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/56-how-can-you-retrieve-data-from-the-resultset/"><font face="Times New Roman">56. How can you retrieve data from the ResultSet</font></a></span></h3>
<p><span><font face="Times New Roman">Example:</font></span><span><font face="Times New Roman">Statement stmt = conn.createStatement(); <br />
ResultSet rs = stmt.executeQuery(SELECT COF_NAME, PRICE FROM COFFEES”); <br />
while (rs .next() ) <br />
{ <br />
//Iam assuming there are 3 columns in the table. <br />
System.out.println ( rs.getString(1)); <br />
System.out.println(rs.getString(2)); <br />
System.out.println(rs.getString(3)); <br />
} <br />
//don’t forget to close the resultset, statement &amp; connection <br />
rs.close(); //First  <br />
stmt.close(); //Second <br />
con.close(); //Last  <br />
System.out.println(”You are done”); </font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/56-how-can-you-retrieve-data-from-the-resultset/#respond" title="Comment on 56. How can you retrieve data from the ResultSet"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/55-how-do-you-implement-connection-pooling/"><font face="Times New Roman">55. How do you implement Connection Pooling</font></a></span></h3>
<p><span><font face="Times New Roman">Connection Pooling can be implemented by the following way. </font></span></p>
<ol>
<li class="MsoNormal"><span><font face="Times New Roman"> A javax.sql.ConnectionPoolDataSource interface that serves as a resource manager connection factory for pooled java.sql.Connection objects. Each database vendors provide the implementation for that interface.  </font></span>
<ol>
<li class="MsoNormal"><span><font face="Times New Roman">For example, the oracle vendors implementation is as follows: oracle.jdbc.pool.oracleConnectionPoolDataSource Class.  </font></span></li>
</ol>
</li>
<li class="MsoNormal"><span><font face="Times New Roman">A javax.sql.PooledConnection interface encapsulates the physical connection for the database. Again, the vendor provides the implementation. </font></span></li>
</ol>
<p><font face="Times New Roman"><strong><u><span style="color:red;">Code for connecting the connection pooling</span></u></strong><span></span></font><span><font face="Times New Roman">InitalContext ic=new InitialContext(); </font></span><span><font face="Times New Roman">Hashtable ht=new Hashtable(); </font></span><span><font face="Times New Roman">ht.put(”Context.INITIAL_PROVIDER”,weblogic.jndi.InitialCla) </font></span><span><font face="Times New Roman">//u have to set weblogic properties first and the jndi name that u r defining in  </font></span><span><font face="Times New Roman">//weblogic while creating the connectionpool </font></span><span><font face="Times New Roman">ht.put(”Context.PROVIDER_URL”,t3://localhost:7001); </font></span><span><font face="Times New Roman">ht.put(”Context.SECURITY_PRINCIPAL”,username); </font></span><span><font face="Times New Roman">ht.put(”Context.SECURITY_CREDENTIALS”,passwordof weblogic); </font></span><span><font face="Times New Roman">DataSource ds=(DataSource)ic.lookup(”jndiname”); </font></span><span><font face="Times New Roman">Connection con=ds.getConnection(); </font></span><span><font face="Times New Roman">Statement stmt=con.createStatement();</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/55-how-do-you-implement-connection-pooling/#respond" title="Comment on 55. How do you implement Connection Pooling"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/54-how-to-call-a-stored-procedure-from-jdbc/"><font face="Times New Roman">54. How to call a stored procedure from JDBC</font></a></span></h3>
<p><span><font face="Times New Roman">Stored Procedures can be called in java by using the prepareCall() method which returns a CallableStatement object.</font></span><span><font face="Times New Roman">CallableStatement cs = con.prepareCall(”{call Procedure_name}”); </font></span><span><font face="Times New Roman">ResultSet rs = cs.executeQuery();</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/54-how-to-call-a-stored-procedure-from-jdbc/#respond" title="Comment on 54. How to call a stored procedure from JDBC"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/53-what-are-the-three-statements-in-jdbc-difference-between-them/"><font face="Times New Roman">53. What are the three statements in JDBC &amp; difference between them.</font></a></span></h3>
<p><span><font face="Times New Roman">1.<strong><span style="color:red;">Statement</span></strong> which is used to run simple sql statements like select and update <br />
2. <strong><span style="color:red;">PrepareStatment</span> </strong>is used to run Pre compiled sql.  <br />
3. <strong><span style="color:red;">CallableStatement</span></strong><span style="color:red;"> </span>is used to execute the stored procedures.</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/53-what-are-the-three-statements-in-jdbc-difference-between-them/#comments" title="Comment on 53. What are the three statements in JDBC &amp; difference between them."><font face="Times New Roman">1 comment</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/52-what-is-the-difference-between-resultset-and-rowset/"><font face="Times New Roman">52. What is the difference between ResultSet and RowSet</font></a></span></h3>
<p><font face="Times New Roman"><strong><span>A ResultSet</span></strong><span> maintains a connection to a database and because of that it can’t be serialized and also we cant pass the Resultset object from one class to other class across the network.</span></font><font face="Times New Roman"><strong><span>RowSet</span></strong><span> is a disconnected, serializable version of a JDBC ResultSet and also the RowSet extends the ResultSet interface so it has all the methods of ResultSet. The RowSet can be serialized because it doesn’t have a connection to any database and also it can be sent from one class to another across the network. </span></font><font face="Times New Roman"><strong><u><span style="color:red;">More on RowSet:</span></u></strong><span> </span></font><span><font face="Times New Roman">A RowSet can be * connected * or *disconnected* <br />
A *disconnected* rowset gets a connection to a data source in order to fill itself with data or to propagate changes in data back to the data source, but most of the time it does not have a connection open. While it is disconnected, it does not need a JDBC driver or the full JDBC API, so its footprint is very small. Thus a rowset is an ideal format for sending data over a network to a thin client.  <br />
A connected RowSet is like a wrapper around the ResultSet. <br />
Implementation: <br />
<strong><span style="color:red;">A CachedRowSet class</span></strong>—a disconnected rowset that caches its data in memory; not suitable for very large data sets, but an ideal way to provide thin Java clients, such as a Personal Digital Assistant (PDA) or Network Computer (NC), with tabular data  <br />
<strong><span style="color:red;">A JDBCRowSet class</span></strong>—a connected rowset that serves mainly as a thin wrapper around a ResultSet object to make a JDBC driver look like a JavaBeans component  <br />
<strong><span style="color:red;">A WebRowSet class</span></strong>—a connected rowset that uses the HTTP protocol internally to talk to a Java servlet that provides data access; used to make it possible for thin web clients to retrieve and possibly update a set of rows</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/52-what-is-the-difference-between-resultset-and-rowset/#comments" title="Comment on 52. What is the difference between ResultSet and RowSet"><font face="Times New Roman">2 comments</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/51-what-is-a-transaction/"><font face="Times New Roman">51. What is a transaction.</font></a></span></h3>
<p><span><font face="Times New Roman">To emulate a business transaction, a program may need to perform several steps. A financial program, for example, might transfer funds from a checking account to a savings account with the steps listed in the following pseudocode:</font></span></p>
<pre style="margin:0 0.5in;"><span style="color:blue;"><font size="2">begin transaction</font></span></pre>
<pre style="margin:0 0.5in;"><span style="color:blue;"><font size="2"><span>   </span>debit checking account</font></span></pre>
<pre style="margin:0 0.5in;"><span style="color:blue;"><font size="2"><span>   </span>credit savings account</font></span></pre>
<pre style="margin:0 0.5in;"><span style="color:blue;"><font size="2"><span>   </span>update history log</font></span></pre>
<pre style="margin:0 0.5in;"><font size="2"><span style="color:blue;">commit transaction</span><span></span></font></pre>
<pre style="margin:0 0.5in;"><span><font size="2"> </font></span></pre>
<p><span><font face="Times New Roman">Either all three of these steps must complete, or none of them at all. </font></span><span><font face="Times New Roman">Otherwise, data integrity is lost. Because the steps within a transaction are a unified whole, <strong><span style="color:red;">a <em>transaction</em> is often defined as an indivisible unit of work.</span></strong></font></span></p>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">A transaction can end in two ways: with a commit or a rollback. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">When a transaction commits, the data modifications made by its statements are saved. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">If a statement within a transaction fails, the transaction rolls back, undoing the effects of all statements in the transaction. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">In the pseudocode, for example, if a disk drive crashed during the </font></span><code><span style="font-size:10pt;font-family:'Courier New';">credit</span></code><span><font face="Times New Roman"> step, the transaction would roll back and undo the data modifications made by the </font></span><code><span style="font-size:10pt;font-family:'Courier New';">debit</span></code><span><font face="Times New Roman"> statement. Although the transaction failed, data integrity would be intact because the accounts still balance. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">In the preceding pseudocode, the </font></span><code><span style="font-size:10pt;font-family:'Courier New';">begin</span></code><span><font face="Times New Roman"> and </font></span><code><span style="font-size:10pt;font-family:'Courier New';">commit</span></code><span><font face="Times New Roman"> statements mark the boundaries of the transaction. </font></span></li>
</ul>
<p><span><font face="Times New Roman">Transaction mainly consist of <strong><span style="color:red;">ACID. A-Atomicity, C- Consistency, I &#8211; Integrity, D- Durability.</span></strong> If a process consist of acid successfully then we can say that the transaction is successful.</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/51-what-is-a-transaction/#respond" title="Comment on 51. What is a transaction."><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/50-what-is-the-purpose-of-setautocommit/"><font face="Times New Roman">50. What is the purpose of setAutoCommit()</font></a></span></h3>
<p><font face="Times New Roman"><strong><span style="color:red;">Autocommit behavior</span></strong><span>    The JDBC specification requires that, by default, a COMMIT is performed after each data modification statement. Currently, the server-side JDBC behavior is to commit. You can control this behavior using a statement such as the following:</span></font></p>
<pre><font size="2"><span style="font-family:Symbol;">·</span><span><span>  </span>conn.setAutoCommit( false ) ;</span></font></pre>
<p><span><font face="Times New Roman">where <strong>conn</strong> is the current connection object.</font></span><span style="font-family:Symbol;">·</span><span><font face="Times New Roman"><span>  </span><strong><span style="color:red;">Connection defaults</span></strong>    From server-side JDBC, only the first call to <strong>getConnection(</strong> <strong>“jdbc:default:connection”</strong> <strong>)</strong> creates a new connection with the default values. Subsequent calls return a wrapper of the current connection with all connection properties unchanged. If you set AutoCommit to OFF in your initial connection, any subsequent <strong>getConnection</strong> calls within the same Java code return a connection with AutoCommit set to OFF. </font></span><span><font face="Times New Roman">You may wish to ensure that closing a connection resets the connection properties to their default values, so that subsequent connections are obtained with standard JDBC values. The following type of code achieves this: </font></span></p>
<pre><span><font size="2">Connection conn = DriverManager.getConnection("");</font></span></pre>
<pre><span><font size="2">boolean oldAutoCommit = conn.getAutoCommit();</font></span></pre>
<pre><span><font size="2">try {</font></span></pre>
<pre><span><font size="2"><span>     </span>// do code here</font></span></pre>
<pre><span><font size="2">}</font></span></pre>
<pre><span><font size="2">finally {</font></span></pre>
<pre><span><font size="2"><span>    </span>conn.setAutoCommit( oldAutoCommit );</font></span></pre>
<pre><span><font size="2">}</font></span></pre>
<p><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/50-what-is-the-purpose-of-setautocommit/#respond" title="Comment on 50. What is the purpose of setAutoCommit()"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/49-what-is-the-difference-between-statement-and-prepared-statment/"><font face="Times New Roman">49. What is the difference between statement and prepared statment?</font></a></span></h3>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">Prepared Statement is Pre-compiled class , but Statement is not. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">So in PreparedStatement the execution will be faster. </font></span></li>
</ul>
<p><span><font face="Times New Roman">Actually when u submit a simple statement to the databse, at first the DBMS parses it and sends it back with the result, so again when u send the same statement again the DBMS server parses it and sends back the result so here a lot of time is wasted and because the statement is again parsed though it has been sent twice or thrice it consumes a lot of time and response will be slow.Prepared Statement is used when u want to execute a statement object many times. when u submit a PreparedStatement the DBMS server parses it and creates a execution plan. This e-plan can be used when u again send the same statement to the database.That is the DBMS server zest executes the compiled statement rather that executing it from first, hence we get an precompiled statement.And also the advanatge of using this PreparedStatement is it is used to send dynamic sql statements, which u can give values later than giving the values at the time of creation.</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/49-what-is-the-difference-between-statement-and-prepared-statment/#comments" title="Comment on 49. What is the difference between statement and prepared statment?"><font face="Times New Roman">4 comments</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/48-what-is-a-savepoint/"><font face="Times New Roman">48. What is a savepoint.</font></a></span></h3>
<p><span><font face="Times New Roman">Save point is a feature using which we can save  status of the transaction to a perticular extent.so that if anytime we have lost a transaction we can get it back using save point.</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/48-what-is-a-savepoint/#respond" title="Comment on 48. What is a savepoint."><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/47-what-is-the-difference-between-resultsetmetadata-and-databasemetadata/"><font face="Times New Roman">47. What is the difference between ResultSetMetaData and DatabaseMetaData</font></a></span></h3>
<h5><font face="Times New Roman"><strong><u><span style="color:red;">ResultSetMetaData</span></u></strong><span></span></font></h5>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">You can interrogate JDBC for detailed information about a query’s result set using a </font></span><tt><span style="font-size:10pt;font-family:'Courier New';">ResultSetMetaData</span></tt><span><font face="Times New Roman"> object. </font></span></li>
<li class="MsoNormal"><tt><span style="font-size:10pt;color:blue;font-family:'Courier New';">ResultSetMetaData</span></tt><span style="color:blue;"><font face="Times New Roman"> is a class that is used to find information about the </font></span><tt><span style="font-size:10pt;color:blue;font-family:'Courier New';">ResultSet</span></tt><span style="color:blue;"><font face="Times New Roman"> returned from a </font></span><tt><span style="font-size:10pt;color:blue;font-family:'Courier New';">executeQuery</span></tt><font face="Times New Roman"><span style="color:blue;"> call. </span><span></span></font></li>
<li class="MsoNormal"><span><font face="Times New Roman">It contains information about the number of columns, the types of data they contain, the names of the columns, and so on. </font></span></li>
<li class="MsoNormal"><span style="color:blue;"><font face="Times New Roman">Two of the most common methods in the </font></span><tt><span style="font-size:10pt;color:blue;font-family:'Courier New';">ResultSetMetaData</span></tt><span style="color:blue;"><font face="Times New Roman"> are </font></span><tt><span style="font-size:10pt;color:blue;font-family:'Courier New';">getColumnName</span></tt><span style="color:blue;"><font face="Times New Roman"> and </font></span><tt><span style="font-size:10pt;color:blue;font-family:'Courier New';">getColumnTypeName</span></tt><span style="color:blue;"><font face="Times New Roman">. These retrieve the name of a column, and the name of its associated data type, respectively, each in the form of a </font></span><tt><span style="font-size:10pt;color:blue;font-family:'Courier New';">String</span></tt><font face="Times New Roman"><span style="color:blue;">.</span><span> </span></font></li>
</ul>
<h5><font face="Times New Roman"><u><span>DatabaseMetaData</span></u><span></span></font></h5>
<p><tt><span style="font-size:10pt;font-family:'Courier New';">DatabaseMetaData</span></tt><span><font face="Times New Roman"> is a class that can be used to fetch information about the database you are using. Use it to answer questions such as: </font></span><span style="font-size:10pt;font-family:Symbol;">·<span style="font:7pt 'Times New Roman';">         </span></span><span><font face="Times New Roman">What kind of catalogs are in the database?</font></span><span style="font-size:10pt;font-family:Symbol;">·<span style="font:7pt 'Times New Roman';">         </span></span><span><font face="Times New Roman">What brand of database am I working with?</font></span><span style="font-size:10pt;font-family:Symbol;">·<span style="font:7pt 'Times New Roman';">         </span></span><span><font face="Times New Roman">What username am I?</font></span><span><font face="Times New Roman">ex: username = dbmd.getUserName();</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/47-what-is-the-difference-between-resultsetmetadata-and-databasemetadata/#respond" title="Comment on 47. What is the difference between ResultSetMetaData and DatabaseMetaData"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/46-what-do-you-mean-by-batch-update/"><font face="Times New Roman">46. What do you mean by batch update</font></a></span></h3>
<ol>
<li class="MsoNormal"><span><font face="Times New Roman">The JDBC drivers that support JDBC 2.0 and above support batch updates. </font></span></li>
<li class="MsoNormal"><font face="Times New Roman"><span style="color:blue;">With batch updates, instead of updating rows of a  table one at a time, you can direct JDBC to execute a group of updates at the same time. </span><span></span></font></li>
<li class="MsoNormal"><span><font face="Times New Roman">Statements that can be included in the same batch of updates are known as batchable statements. </font></span></li>
</ol>
<p><font face="Times New Roman"><strong><u><span style="color:red;">Homogeneous batch:</span></u></strong><span> If a statement has input parameters or host expressions, you can include that statement only in a batch that has other instances of the same statement. This type of batch is known as a homogeneous batch. </span></font><font face="Times New Roman"><strong><u><span style="color:red;">Heterogeneous batch:</span></u></strong><span> If a statement has no input parameters, you can include that statement in a batch only if the other statements in the batch have no input parameters or host expressions. This type of batch is known as a heterogeneous batch. </span></font><span><font face="Times New Roman">Two statements that can be included in the same batch are known as <em><strong><u>batch compatible.</u></strong></em> </font></span><span><font face="Times New Roman">Use the following </font></span><tt><span style="font-size:10pt;font-family:'Courier New';">Statement</span></tt><span><font face="Times New Roman"> methods for creating, executing, and removing a batch of SQL updates: </font></span></p>
<ul>
<li class="MsoNormal"><tt><span style="font-size:10pt;font-family:'Courier New';">addBatch</span></tt><span><font face="Times New Roman"> </font></span></li>
<li class="MsoNormal"><tt><span style="font-size:10pt;font-family:'Courier New';">executeBatch</span></tt><span><font face="Times New Roman"> </font></span></li>
<li class="MsoNormal"><tt><span style="font-size:10pt;font-family:'Courier New';">clearBatch</span></tt><span><font face="Times New Roman"> </font></span></li>
</ul>
<p><tt><span style="font-size:10pt;font-family:'Courier New';">example: the amendments to the all email ids of the employees.</span></tt><span></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/46-what-do-you-mean-by-batch-update/#respond" title="Comment on 46. What do you mean by batch update"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/09/44-what-is-the-difference-between-local-and-global-transaction/"><font face="Times New Roman">45. What is the difference between local and global transaction.</font></a></span></h3>
<p><span><font face="Times New Roman">A transaction is atomic unit of Work.The tasks which are made into the transaction act as a unit which can be executed successfully all,or if at least one task fails to its promise ,then the effect of all the tasks are to be rollbacked.Thus transaction is committed or rolled backed.</font></span><span><font face="Times New Roman">Transactions can be divided into two categories. </font></span><font face="Times New Roman"><span style="color:red;">1.<strong><u>Local Transactions</u></strong>:</span><span>These transactions are confined to objects which reside inside one particular JVM.Local transactions in java can be implemented using the JTA api. </span></font><font face="Times New Roman"><span style="color:red;">2.<strong><u>Global Transactions</u></strong>:</span><span>These transactions may encapsulate objects which are distributed on various JVM’s.Global transactions are implemented throught TWO-PHASE-COMMIT design implementation.</span></font><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/09/44-what-is-the-difference-between-local-and-global-transaction/#respond" title="Comment on 45.  What is the difference between local and global transaction."><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 9th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/08/44-what-the-new-features-in-jdbc-20/"><font face="Times New Roman">44. What the new features in JDBC 2.0</font></a></span></h3>
<p><span><font face="Times New Roman">The following are the JDBC 2.0 features. </font></span><span style="font-size:10pt;font-family:Symbol;">·<span style="font:7pt 'Times New Roman';">         </span></span><font face="Times New Roman"><span style="color:red;">Resultset enhancements</span><span></span></font><span style="font-size:10pt;font-family:'Courier New';">o<span style="font:7pt 'Times New Roman';">        </span></span><span><font face="Times New Roman">JDBC 2.0 supports scrollable resultset capability under three major headings, namely, forward-only, scroll-insensitive, and scroll-sensitive. </font></span><span style="font-size:10pt;font-family:'Courier New';">o<span style="font:7pt 'Times New Roman';">        </span></span><span><font face="Times New Roman">Each of these resultsets can in turn be Read_only or Updatable. Forward_only/Read_only is feature of JDBC 1.0. </font></span><span style="font-size:10pt;font-family:Symbol;">·<span style="font:7pt 'Times New Roman';">         </span></span><font face="Times New Roman"><span style="color:red;">Batch Updates</span><span></span></font><span style="font-size:10pt;font-family:'Courier New';">o<span style="font:7pt 'Times New Roman';">        </span></span><span><font face="Times New Roman">automatic batch update and explicit batch update.</font></span><span style="font-size:10pt;font-family:Symbol;">·<span style="font:7pt 'Times New Roman';">         </span></span><font face="Times New Roman"><span style="color:red;">Advanced Data Types</span><span></span></font><span style="font-size:10pt;font-family:'Courier New';">o<span style="font:7pt 'Times New Roman';">        </span></span><span><font face="Times New Roman">The advanced data types such as objects, object references, arrays, LOBS, SQL Data, and Struct are now supported by JDBC 2.0. </font></span><span style="font-size:10pt;font-family:Symbol;">·<span style="font:7pt 'Times New Roman';">         </span></span><font face="Times New Roman"><span style="color:red;">JNDI</span><span></span></font><span style="font-size:10pt;font-family:'Courier New';">o<span style="font:7pt 'Times New Roman';">        </span></span><span><font face="Times New Roman">The Java Naming and Directory Interface (JNDI) is an interface to obtain network resources in a vendor independent fashion. This is available as part of the JDBC2.0 Optional Package (JDBC 2.0 Standard Extension API) and implemented by means of javax.sql package. </font></span><span style="font-size:10pt;font-family:Symbol;">·<span style="font:7pt 'Times New Roman';">         </span></span><font face="Times New Roman"><span style="color:red;">Connection Pooling</span><span></span></font><span style="font-size:10pt;font-family:'Courier New';">o<span style="font:7pt 'Times New Roman';">        </span></span><span><font face="Times New Roman">Connection pooling is a method where multiple consumers share a limited set of connections instead of each having to create new connections. This also includes connection caching. This is implemented by means of javax.sql package. </font></span><span style="font-size:10pt;font-family:Symbol;">·<span style="font:7pt 'Times New Roman';">         </span></span><font face="Times New Roman"><span style="color:red;">Distributed Transactions</span><span></span></font><span style="font-size:10pt;font-family:'Courier New';">o<span style="font:7pt 'Times New Roman';">        </span></span><span><font face="Times New Roman">A distributed transaction is a combination of two or more related transactions that execute in a coordinated manner. </font></span><span style="font-size:10pt;font-family:'Courier New';">o<span style="font:7pt 'Times New Roman';">        </span></span><span><font face="Times New Roman">All the individual transactions might take place in the same database, but typically, the individual transactions are in different databases and often in different locations. </font></span><span style="font-size:10pt;font-family:'Courier New';">o<span style="font:7pt 'Times New Roman';">        </span></span><span><font face="Times New Roman">Distributed transactions are defined as a JDBC2.0 standard extensions API and are also implemented by means of javax.sql package. </font></span><span style="font-size:10pt;font-family:Symbol;">·<span style="font:7pt 'Times New Roman';">         </span></span><font face="Times New Roman"><span style="color:red;">Other Features</span><span></span></font><span style="font-size:10pt;font-family:'Courier New';">o<span style="font:7pt 'Times New Roman';">        </span></span><span><font face="Times New Roman">Other features include fetch size/row prefetching. Fetch size is part of JDBC2.0 and includes methods to define the number of rows retrieved by each database fetch.  </font></span><strong><span style="font-size:9pt;color:#333333;"><font face="Verdana"> </font></span></strong><strong><span style="font-size:9pt;color:#333333;"><font face="Verdana"> </font></span></strong><strong><span style="font-size:9pt;color:#333333;"><font face="Verdana"> </font></span></strong><strong><span style="font-size:9pt;color:#333333;"><font face="Verdana"> </font></span></strong></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/08/43-explain-jdbc-driver-types/"><font face="Times New Roman">43. Explain JDBC Driver Types</font></a></span></h3>
<p><span><font face="Times New Roman">JDBC drivers are divided into four types or levels. Each type defines a JDBC driver implementation with increasingly higher levels of platform independence, performance, and deployment administration. The four types are: </font></span></p>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">Type 1: JDBC-ODBC Bridge </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Type 2: Native-API/partly Java driver </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Type 3: Net-protocol/all-Java driver </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Type 4: Native-protocol/all-Java driver </font></span></li>
</ul>
<p><strong><span style="color:red;"><font face="Times New Roman">Type 1: JDBC-ODBC Bridge  </font></span></strong><span><br />
<font face="Times New Roman">The type 1 driver, JDBC-ODBC Bridge, translates all JDBC calls into ODBC (Open DataBase Connectivity) calls and sends them to the ODBC driver. As such, the ODBC driver, as well as, in many cases, the client database code, must be present on the client machine. Figure 1 shows a typical JDBC-ODBC Bridge environment. </font></span><strong><span style="color:blue;"><font face="Times New Roman">Pros </font><a href="http://sharat.files.wordpress.com/2006/09/WindowsLiveWriter/43.ExplainJDBCDriverTypes_ED9B/jdbc1[3]2.png"></a></span></strong><strong><span style="color:red;"><a href="http://sharat.files.wordpress.com/2006/09/WindowsLiveWriter/43.ExplainJDBCDriverTypes_ED9B/jdbc1[4].png"><span style="text-decoration:none;"></span></a></span></strong><span><br />
<font face="Times New Roman">The JDBC-ODBC Bridge allows access to almost any database, since the database’s ODBC drivers are already available. Type 1 drivers may be useful for those companies that have an ODBC driver already installed on client machines. </font></span><font face="Times New Roman"><strong><span style="color:blue;">Cons </span></strong><span></span></font></p>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">The performance is degraded since the JDBC call goes through the bridge to the ODBC driver, then to the native database connectivity interface. The result comes back through the reverse process. Considering the performance issue, type 1 drivers may not be suitable for large-scale applications. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">The ODBC driver and native connectivity interface must already be installed on the client machine. Thus any advantage of using Java applets in an intranet environment is lost, since the deployment problems of traditional applications remain. </font></span></li>
</ul>
<p><strong><span style="color:red;"><font face="Times New Roman">Type 2: Native-API/partly Java driver </font></span></strong><span style="color:red;"><br />
</span><span><font face="Times New Roman">JDBC driver type 2 — the native-API/partly Java driver — converts JDBC calls into database-specific calls for databases such as SQL Server, Informix, Oracle, or Sybase. The type 2 driver communicates directly with the database server; therefore it requires that some binary code be present on the client machine. </font></span><strong><span style="color:blue;"><font face="Times New Roman">Pros </font><a href="http://sharat.files.wordpress.com/2006/09/WindowsLiveWriter/43.ExplainJDBCDriverTypes_ED9B/jdbc2[4].png"><span style="text-decoration:none;"></span></a></span></strong><span><br />
<font face="Times New Roman">Type 2 drivers typically offer significantly better performance than the JDBC-ODBC Bridge. </font></span><strong><span style="color:blue;"></span></strong><span><br />
<font face="Times New Roman">The vendor database library needs to be loaded on each client machine. Consequently, type 2 drivers cannot be used for the Internet. Type 2 drivers show lower performance than type 3 and type 4 drivers. </font></span><strong><span style="color:red;"><font face="Times New Roman">Type 3: Net-protocol/all-Java driver  </font></span></strong><span style="color:red;"><br />
</span><span><font face="Times New Roman">JDBC driver type 3 — the net-protocol/all-Java driver — follows a three-tiered approach whereby the JDBC database requests are passed through the network to the middle-tier server. The middle-tier server then translates the request (directly or indirectly) to the database-specific native-connectivity interface to further the request to the database server. If the middle-tier server is written in Java, it can use a type 1 or type 2 JDBC driver to do this.</font></span><strong><span style="color:blue;"><font face="Times New Roman">Pros </font><a href="http://sharat.files.wordpress.com/2006/09/WindowsLiveWriter/43.ExplainJDBCDriverTypes_ED9B/jdbc3[4].png"><span style="text-decoration:none;"></span></a></span></strong><span style="color:blue;"><br />
</span><span><font face="Times New Roman">The net-protocol/all-Java driver is server-based, so there is no need for any vendor database library to be present on client machines. Further, there are many opportunities to optimize portability, performance, and scalability. Moreover, the net protocol can be designed to make the client JDBC driver very small and fast to load. Additionally, a type 3 driver typically provides support for features such as caching (connections, query results, and so on), load balancing, and advanced system administration such as logging and auditing. </font></span><font face="Times New Roman"><strong><span style="color:blue;">Cons</span></strong><strong><span> </span></strong></font><span><br />
<font face="Times New Roman">Type 3 drivers require database-specific coding to be done in the middle tier. Additionally, traversing the recordset may take longer, since the data comes through the backend server. </font></span><strong><span style="color:red;"><font face="Times New Roman">Type 4: Native-protocol/all-Java driver  </font></span></strong><span style="color:red;"><br />
</span><span><font face="Times New Roman">The native-protocol/all-Java driver (JDBC driver type 4) converts JDBC calls into the vendor-specific database management system (DBMS) protocol so that client applications can communicate directly with the database server. Level 4 drivers are completely implemented in Java to achieve platform independence and eliminate deployment administration issues.</font><a href="http://sharat.files.wordpress.com/2006/09/WindowsLiveWriter/43.ExplainJDBCDriverTypes_ED9B/jdbc4[4].png"><span style="text-decoration:none;"><font face="Times New Roman"> </font></span></a></span><font face="Times New Roman"><strong><span style="color:blue;">Pros</span></strong><strong><span> </span></strong></font><span><br />
<font face="Times New Roman">Since type 4 JDBC drivers don’t have to translate database requests to ODBC or a native connectivity interface or to pass the request on to another server, performance is typically quite good. Moreover, the native-protocol/all-Java driver boasts better performance than types 1 and 2. Also, there’s no need to install special software on the client or server. Further, these drivers can be downloaded dynamically. </font></span><font face="Times New Roman"><strong><span style="color:blue;">Cons</span></strong><strong><span> </span></strong></font><span><br />
<font face="Times New Roman">With type 4 drivers, the user needs a different driver for each database. </font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/08/43-explain-jdbc-driver-types/#comments" title="Comment on 43. Explain JDBC Driver Types"><font face="Times New Roman">1 comment</font></a><font face="Times New Roman"> <em>September 8th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/08/42-what-is-jdbc/"><font face="Times New Roman">42. What is JDBC</font></a></span></h3>
<ul>
<li class="MsoNormal"><font face="Times New Roman"><span style="color:blue;">JDBC (Java Database Connectivity) provides a standard interface for accessing a relational database from a Java application regardless of where the application is running and where the database is. </span><span></span></font></li>
<li class="MsoNormal"><span><font face="Times New Roman">It provides a way for Java applications to call SQL and PL/SQL. </font></span></li>
<li class="MsoNormal"><font face="Times New Roman"><span style="color:blue;">In other words it is a way to execute SQL statements and also call stored database procedures</span><span>. </span></font></li>
<li class="MsoNormal"><span><font face="Times New Roman">One important feature of JDBC is location independence. Java programs with database access can be written and deployed as an application or as a Web-based applet. </font></span></li>
<li class="MsoNormal"><font face="Times New Roman"><span style="color:blue;">The ease of development, robustness, and security of Java programs makes it a good choice for writing database applications in Java. </span><span></span></font></li>
<li class="MsoNormal"><span><font face="Times New Roman">JDBC consists of a high-level “thin” API and multiple low-level drivers for connecting to different databases. </font></span></li>
</ul>
<p><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/08/42-what-is-jdbc/#respond" title="Comment on 42. What is JDBC"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 8th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/08/41-explain-basic-steps-in-writing-a-java-program-using-jdbc/"><font face="Times New Roman">41. Explain Basic Steps in writing a Java program using JDBC</font></a></span></h3>
<p><span><font face="Times New Roman">Basically a Java program implementing JDBC performs the following functions: </font></span><span style="font-size:10pt;font-family:Symbol;">·<span style="font:7pt 'Times New Roman';">         </span></span><span><font face="Times New Roman">Load a JDBC driver.</font></span><span style="font-size:10pt;font-family:Symbol;">·<span style="font:7pt 'Times New Roman';">         </span></span><span><font face="Times New Roman">Establish a database connection.</font></span><span style="font-size:10pt;font-family:Symbol;">·<span style="font:7pt 'Times New Roman';">         </span></span><span><font face="Times New Roman">Optionally interrogate the database for capability subset.</font></span><span style="font-size:10pt;font-family:Symbol;">·<span style="font:7pt 'Times New Roman';">         </span></span><span><font face="Times New Roman">Optionally retrieve schema metadata information.</font></span><span style="font-size:10pt;font-family:Symbol;">·<span style="font:7pt 'Times New Roman';">         </span></span><span><font face="Times New Roman">Construct a SQL or callable statement object and send queries or database tasks.</font></span><span style="font-size:10pt;font-family:Symbol;">·<span style="font:7pt 'Times New Roman';">         </span></span><span><font face="Times New Roman">Execute the database tasks or process resultsets.</font></span><span style="font-size:10pt;font-family:Symbol;">·<span style="font:7pt 'Times New Roman';">         </span></span><span><font face="Times New Roman">Close the statement object and resultset.</font></span><span style="font-size:10pt;font-family:Symbol;">·<span style="font:7pt 'Times New Roman';">         </span></span><span><font face="Times New Roman">Close the connection.</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/08/41-explain-basic-steps-in-writing-a-java-program-using-jdbc/#respond" title="Comment on 41. Explain Basic Steps in writing a Java program using JDBC"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 8th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/08/40-what-is-the-difference-between-jspinclude-and-cimport/"><font face="Times New Roman">40. What is the difference between &lt;jsp:include&gt; and &lt;c:import&gt;</font></a></span></h3>
<ol>
<li class="MsoNormal"><font face="Times New Roman"><span style="color:red;">The &lt;c:import&gt; tag imports a resource specified by a URL and exposes it to the page, variable, or reader. </span><span></span></font></li>
<li class="MsoNormal"><span><font face="Times New Roman">The &lt;jsp:include&gt; standard action is a request-time action that will include the output of another JSP at the location of the tag within the calling JSP. </font></span></li>
<li class="MsoNormal"><font face="Times New Roman"><span style="color:blue;">&lt;c:import&gt; is similar to the &lt;jsp:include&gt; directive, but with more features. </span><span></span></font></li>
<li class="MsoNormal"><font face="Times New Roman"><span style="color:red;">The body of the import tag can have param tags for adding parameters to the URL.</span><span> </span></font></li>
<li class="MsoNormal"><font face="Times New Roman"><span style="color:blue;">we can overcome jsp:include limitations with the JSTL import tag.</span><span> </span></font></li>
<li class="MsoNormal"><font face="Times New Roman"><span style="color:red;">The import tag adds functionality to the jsp:include tag and eliminates the need to code these functions yourself.</span><span> </span></font></li>
<li class="MsoNormal"><font face="Times New Roman"><span style="color:blue;">The first additional function allows you to include content directly from other Web sites. So the following is now legal:<br />
&lt;c:import url=”</span><span><a href="http://www.example.com/somecontent.html">http://www.example.com/somecontent.html”/</a><span style="color:blue;">&gt; </span></span></font></li>
<li class="MsoNormal"><font face="Times New Roman"><span style="color:red;">You can even use ftp:<br />
&lt;c:import url=”</span><span><a href="ftp://ftp.example.com/somecontent.txt"><span style="color:red;">ftp://ftp.example.com/somecontent.txt”/</span></a><span style="color:red;">&gt; </span></span></font></li>
<li class="MsoNormal"><span><font face="Times New Roman">Rather than dump an imported URL straight to the page, you can store it in a variable. This is a common strategy in all the JSTL tags and increases the power of a JSP page:<br />
&lt;c:import url=”</font><a href="http://www.example.com/example.inc"><font face="Times New Roman">http://www.example.com/example.inc”</font></a><font face="Times New Roman"> var=”example”/&gt; Once this included page is stored in the example variable, you can use another taglib, such as the Jakarta String taglib, to replace all references to example.com with done.com. </font></span></li>
<li class="MsoNormal"><font face="Times New Roman"><span style="color:red;">Since you can store &lt;c:import&gt; in a variable I’m wondering if &lt;c:import&gt; might do some type of full read before printing to output, whereas I imagine &lt;jsp:include&gt; uses buffering. This could show a performance difference for very large files, with &lt;c:import&gt; requiring longer and taking more memory to get all the data in the import whereas &lt;jsp:include&gt; did things with buffers.</span><span> </span></font></li>
<li class="MsoNormal"><font face="Times New Roman"><strong><u><span style="color:blue;">Include Syntax:</span></u></strong><span> </span></font></li>
</ol>
<p><span><font face="Times New Roman">&lt;jsp:include page=”<em>…url..</em>” flush=”<em>true or false</em>“/&gt;</font></span><span><font face="Times New Roman">The tag can optionally contain a body: </font></span></p>
<pre style="margin:0 0.5in;"><span><font size="2">&lt;jsp:include page="<em>…url…</em>” flush=”<em>true or false</em>“&gt;</font></span></pre>
<pre style="margin:0 0.5in;"><span><font size="2"><span>  </span>&lt;jsp:param<span>  </span><em>….</em>/&gt;</font></span></pre>
<pre style="margin:0 0.5in;"><span><font size="2">&lt;/jsp:include&gt;</font></span></pre>
<p><font face="Times New Roman"><strong><span style="color:red;">          <u>Import Syntax:</u></span></strong><span></span></font><span><font face="Times New Roman">&lt;c:import url=”resource URL”<br />
          [var=”variable name”]<br />
          [scope=”page|request|session|application”]<br />
          [varReader=”variable name”]<br />
          [context=”context name”]<br />
          [charEncoding=”character encoding”]&gt;<br />
     JSP body content<br />
&lt;/c:import&gt;</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/08/40-what-is-the-difference-between-jspinclude-and-cimport/#comments" title="Comment on 40. What is the difference between &lt;jsp:include&gt; and &lt;c:import&gt;"><font face="Times New Roman">2 comments</font></a><font face="Times New Roman"> <em>September 8th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/07/39-what-is-the-differences-between-http-servlet-and-generic-servlet/"><font face="Times New Roman">39. What is the differences between Http Servlet and generic Servlet</font></a></span></h3>
<p><font face="Times New Roman"><strong><span style="color:red;">HttpServlet</span></strong><span> Provides an abstract class to be subclassed to create an HTTP servlet suitable for a Web site. A subclass of </span></font><code><span style="font-size:10pt;font-family:'Courier New';">HttpServlet</span></code><span><font face="Times New Roman"> must override at least one method, usually one of these: </font></span></p>
<ul>
<li class="MsoNormal"><code><span style="font-size:10pt;font-family:'Courier New';">doGet</span></code><span><font face="Times New Roman">, if the servlet supports HTTP GET requests </font></span></li>
<li class="MsoNormal"><code><span style="font-size:10pt;font-family:'Courier New';">doPost</span></code><span><font face="Times New Roman">, for HTTP POST requests </font></span></li>
<li class="MsoNormal"><code><span style="font-size:10pt;font-family:'Courier New';">doPut</span></code><span><font face="Times New Roman">, for HTTP PUT requests </font></span></li>
<li class="MsoNormal"><code><span style="font-size:10pt;font-family:'Courier New';">doDelete</span></code><span><font face="Times New Roman">, for HTTP DELETE requests </font></span></li>
<li class="MsoNormal"><code><span style="font-size:10pt;font-family:'Courier New';">init</span></code><span><font face="Times New Roman"> and </font></span><code><span style="font-size:10pt;font-family:'Courier New';">destroy</span></code><span><font face="Times New Roman">, to manage resources that are held for the life of the servlet </font></span></li>
<li class="MsoNormal"><code><span style="font-size:10pt;font-family:'Courier New';">getServletInfo</span></code><span><font face="Times New Roman">, which the servlet uses to provide information about itself </font></span></li>
</ul>
<p><span><font face="Times New Roman">There’s almost no reason to override the </font></span><code><span style="font-size:10pt;font-family:'Courier New';">service</span></code><span><font face="Times New Roman"> method. </font></span><code><span style="font-size:10pt;font-family:'Courier New';">service</span></code><span><font face="Times New Roman"> handles standard HTTP requests by dispatching them to the handler methods for each HTTP request type (the </font></span><code><span style="font-size:10pt;font-family:'Courier New';">do</span></code><font face="Times New Roman"><em><span>XXX</span></em><span> methods listed above). Likewise, there’s almost no reason to override the </span></font><code><span style="font-size:10pt;font-family:'Courier New';">doOptions</span></code><span><font face="Times New Roman"> and </font></span><code><span style="font-size:10pt;font-family:'Courier New';">doTrace</span></code><span><font face="Times New Roman"> methods. </font></span><font face="Times New Roman"><strong><span style="color:red;">GenericServlet</span></strong><span> defines a generic, protocol-independent servlet. To write an HTTP servlet for use on the Web, extend </span></font><code><span style="font-size:10pt;font-family:'Courier New';">HttpServlet</span></code><span><font face="Times New Roman"> instead. </font></span><code><span style="font-size:10pt;font-family:'Courier New';">GenericServlet</span></code><span><font face="Times New Roman"> implements the </font></span><code><span style="font-size:10pt;font-family:'Courier New';">Servlet</span></code><span><font face="Times New Roman"> and </font></span><code><span style="font-size:10pt;font-family:'Courier New';">ServletConfig</span></code><span><font face="Times New Roman"> interfaces. </font></span><code><span style="font-size:10pt;font-family:'Courier New';">GenericServlet</span></code><span><font face="Times New Roman"> may be directly extended by a servlet, although it’s more common to extend a protocol-specific subclass such as </font></span><code><span style="font-size:10pt;font-family:'Courier New';">HttpServlet</span></code><span><font face="Times New Roman">. </font></span><code><span style="font-size:10pt;font-family:'Courier New';">GenericServlet</span></code><span><font face="Times New Roman"> makes writing servlets easier. It provides simple versions of the lifecycle methods </font></span><code><span style="font-size:10pt;font-family:'Courier New';">init</span></code><span><font face="Times New Roman"> and </font></span><code><span style="font-size:10pt;font-family:'Courier New';">destroy</span></code><span><font face="Times New Roman"> and of the methods in the </font></span><code><span style="font-size:10pt;font-family:'Courier New';">ServletConfig</span></code><span><font face="Times New Roman"> interface. </font></span><code><span style="font-size:10pt;font-family:'Courier New';">GenericServlet</span></code><span><font face="Times New Roman"> also implements the </font></span><code><span style="font-size:10pt;font-family:'Courier New';">log</span></code><span><font face="Times New Roman"> method, declared in the </font></span><code><span style="font-size:10pt;font-family:'Courier New';">ServletContext</span></code><span><font face="Times New Roman"> interface. </font></span><span><font face="Times New Roman">To write a generic servlet, you need only override the abstract </font></span><code><span style="font-size:10pt;font-family:'Courier New';">service</span></code><span><font face="Times New Roman"> method.</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/07/39-what-is-the-differences-between-http-servlet-and-generic-servlet/#comments" title="Comment on 39. What is the differences between Http Servlet and generic Servlet"><font face="Times New Roman">5 comments</font></a><font face="Times New Roman"> <em>September 7th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/07/38-servlets-20-mins-drill-and-quiz/"><font face="Times New Roman">38. Servlets 20 mins drill and Quiz.</font></a></span></h3>
<p><font face="Times New Roman"><strong><span style="color:red;">What is the servlet? </span></strong><span></span></font><span><font face="Times New Roman">Servlet is a script, which resides and executes on server side, to create dynamic HTML. In servlet programming we will use java language. A servlet can handle multiple requests concurrently </font></span><font face="Times New Roman"><strong><span style="color:red;">What’s the difference between servlets and applets? </span></strong><span></span></font><span><font face="Times New Roman">Servlets executes on Servers. Applets executes on browser. Unlike applets, however, servlets have no graphical user interface. </font></span><font face="Times New Roman"><strong><span style="color:red;">What are the uses of Servlets? </span></strong><span></span></font><span><font face="Times New Roman">A servlet can handle multiple requests concurrently, and can synchronize requests. Servlets can forward requests to other servers and servlets. Thus servlets can be used to balance load among several servers. </font></span><font face="Times New Roman"><strong><span style="color:red;">When doGET() method will going to execute? </span></strong><span></span></font><span><font face="Times New Roman">When we specified method=’GET’ in HTML<br />
Example : &lt; form name=’SSS’ method=’GET’&gt; </font></span><font face="Times New Roman"><strong><span style="color:red;">When doPOST() method will going to execute? </span></strong><span></span></font><span><font face="Times New Roman">When we specified method=’POST’ in HTML<br />
&lt; form name=’SSS’ method=’POST’ &gt; </font></span><font face="Times New Roman"><strong><span style="color:red;">Which code line must be set before any of the lines that use the PrintWriter? </span></strong><span></span></font><span><font face="Times New Roman">setContentType() method must be set. </font></span><font face="Times New Roman"><strong><span style="color:red;">Which protocol will be used by browser and servlet to communicate ?</span></strong><span> </span></font><span><font face="Times New Roman">HTTP </font></span><font face="Times New Roman"><strong><span style="color:red;">How Can You invoke other web resources (or other servelt / jsp ) ?</span></strong><span> </span></font><span><font face="Times New Roman">Servelt can invoke other Web resources in two ways: indirect and direct.<br />
Indirect Way : Servlet will return the resultant HTML to the browser which will point to another Servlet (Web resource)<br />
Direct Way : We can call another Web resource (Servelt / Jsp) from Servelt program itself, by using RequestDispatcher object.<br />
You can get this object using getRequestDispatcher(”URL”) method. You can get this object from either a request or a Context.<br />
Example :<br />
RequestDispatcher dispatcher = request.getRequestDispatcher(”/jspsample.jsp”);<br />
if (dispatcher != null)<br />
dispatcher.forward(request, response);<br />
} </font></span><font face="Times New Roman"><strong><span style="color:red;">How Can you include other Resources in the Response? </span></strong><span></span></font><span><font face="Times New Roman">Using include method of a RequestDispatcher object.<br />
Included WebComponent (Servlet / Jsp) cannot set headers or call any method (for example, setCookie) that affects the headers of the response.<br />
Example : RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(”/banner”);<br />
            if (dispatcher != null)<br />
            dispatcher.include(request, response);<br />
} </font></span><font face="Times New Roman"><strong><span style="color:red;">Is there any way to generate PDF’S dynamically in servlets?</span></strong><span> </span></font><span><font face="Times New Roman">We need to use iText. A open source library for java. Please refer sourceforge site for sample servlet examples. </font></span><font face="Times New Roman"><strong><span style="color:red;">What is the difference between using getSession(true) and getSession(false) methods?</span></strong><span> </span></font><span><font face="Times New Roman">getSession(true) &#8211; This method will check whether already a session is existing for the user. If a session is existing, it will return that session object, Otherwise it will create new session object and return taht object.<br />
getSession(false) &#8211; This method will check existence of session. If session exists, then it returns the reference of that session object, if not, this methods will return null. </font></span><font face="Times New Roman"><strong><span style="color:red;">Can I invoke a JSP error page from a servlet?</span></strong><span> </span></font><span><font face="Times New Roman">Yes, you can invoke the JSP error page and pass the exception object to it from within a servlet. The trick is to create a request dispatcher for the JSP error page, and pass the exception object as a javax.servlet.jsp.jspException request attribute. However, note that you can do this from only within controller servlets. </font></span><span><font face="Times New Roman">If your servlet opens an OutputStream or PrintWriter, the JSP engine will throw the following translation error: </font></span><span><font face="Times New Roman">java.lang.IllegalStateException: Cannot forward as OutputStream or Writer has already been obtained </font></span><span><font face="Times New Roman">The following code snippet demonstrates the invocation of a JSP error page from within a controller servlet: </font></span><span><font face="Times New Roman">protected void sendErrorRedirect(HttpServletRequest request,<br />
HttpServletResponse response, String errorPageURL, Throwable e) throws<br />
ServletException, IOException {<br />
request.setAttribute (”javax.servlet.jsp.jspException”, e);<br />
getServletConfig().getServletContext().<br />
getRequestDispatcher(errorPageURL).forward(request, response);<br />
} </font></span><span><font face="Times New Roman">public void doPost(HttpServletRequest request, HttpServletResponse response)<br />
{<br />
try {<br />
// do something<br />
} catch (Exception ex) {<br />
try {<br />
sendErrorRedirect(request,response,”/jsp/MyErrorPage.jsp”,ex);<br />
} catch (Exception e) {<br />
e.printStackTrace();<br />
}<br />
}<br />
} </font></span><font face="Times New Roman"><strong><span style="color:red;">What is the Servlet Interface?</span></strong><span> </span></font><span><font face="Times New Roman">The central abstraction in the Servlet API is the Servlet interface. All servlets implement this interface, either directly or, more commonly, by extending a class that implements it such as HttpServlet.<br />
Servlets–&gt;Generic Servlet–&gt;HttpServlet–&gt;MyServlet.<br />
The Servlet interface declares, but does not implement, methods that manage the servlet and its communications with clients. Servlet writers provide some or all of these methods when developing a servlet. </font></span><font face="Times New Roman"><strong><span style="color:red;">If you want a servlet to take the same action for both GET and POST request, what should you do?</span></strong><span> </span></font><span><font face="Times New Roman">Simply have doGet call doPost, or vice versa. </font></span><font face="Times New Roman"><strong><span style="color:red;">When a servlet accepts a call from a client, it receives two objects. What are they?</span></strong><span> </span></font><span><font face="Times New Roman">ServeltRequest: which encapsulates the communication from the client to the server.<br />
ServletResponse: which encapsulates the communication from the servlet back to the client.<br />
ServletRequest and ServletResponse are interfaces defined by the javax.servlet package. </font></span><font face="Times New Roman"><strong><span style="color:red;">What information that the ServletResponse interface gives the servlet methods for replying to the client?</span></strong><span> </span></font><span><font face="Times New Roman">It Allows the servlet to set the content length and MIME type of the reply. Provides an output stream, ServletOutputStream and a Writer through which the servlet can send the reply data. </font></span><font face="Times New Roman"><strong><span style="color:red;">What information that the ServletRequest interface allows the servlet access to?</span></strong><span> </span></font><span><font face="Times New Roman">Information such as the names of the parameters passed in by the client, the protocol (scheme) being used by the client, and the names of the remote host that made the request and the server that received it. The input stream, ServletInputStream.Servlets use the input stream to get data from clients that use application protocols such as the HTTP POST and PUT methods. </font></span><strong><span style="color:red;"><font face="Times New Roman">What do you understand by servlet mapping?</font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">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. </font></span><strong><span style="color:red;"><font face="Times New Roman">What must be implemented by all Servlets?</font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">The Servlet Interface must be implemented by all servlets. </font></span><font face="Times New Roman"><strong><span style="color:red;">What are the uses of Servlets?</span></strong><span></span></font></p>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">Servlets are used to process the client request. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">A Servlet can handle multiple request concurrently and be used to develop high performance system </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">A Servlet can be used to load balance among serveral servers, as Servlet can easily forward request. </font></span></li>
</ul>
<p><strong><span style="color:red;"><font face="Times New Roman">What is Session ID?</font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">A session ID is an unique identification string usually a long, random and alpha-numeric string, that is transmitted between the client and the server. Session IDs are usually stored in the cookies, URLs (in case url rewriting) and hidden fields of Web pages. </font></span><strong><span style="color:red;"><font face="Times New Roman">What are the advantage of Cookies over URL rewriting?</font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">Sessions tracking using Cookies are more secure and fast. Session tracking using Cookies can also be used with other mechanism of Session Tracking like url rewriting.</font></span><span><font face="Times New Roman">Cookies are stored at client side so some clients may disable cookies so we may not sure that the cookies may work or not. <br />
In url  rewriting requites large data transfer from and to the server. So, it leads to network traffic and access may be become slow. </font></span><strong><span style="color:red;"><font face="Times New Roman">What is session hijacking?</font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">If you application is not very secure then it is possible to get the access of system after acquiring or generating the authentication information. Session hijacking refers to the act of taking control of a user session after successfully obtaining or generating an authentication session ID. It involves an attacker using captured, brute forced or reverse-engineered session IDs to get a control of a legitimate user’s Web application session while that session is still in progress. </font></span><strong><span style="color:red;"><font face="Times New Roman">What is Session Migration?</font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">Session Migration is a mechanism of moving the session from one server to another in case of server failure. Session Migration can be implemented by:<br />
a) Persisting the session into database<br />
b) Storing the session in-memory on multiple servers. </font></span><strong><span style="color:red;"><font face="Times New Roman">How to track a user session in Servlets?</font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">The interface HttpSession can be used to track the session in the Servlet. Following code can be used to create session object in the Servlet: </font></span><font face="Times New Roman"><strong><span>HttpSession session = req.getSession(true);</span></strong><span></span></font><strong><span style="color:red;"><font face="Times New Roman">How you can destroy the session in Servlet?</font></span></strong><strong><span style="color:red;"><br />
</span></strong><span><font face="Times New Roman">You can call invalidate() method on the session object to destroy the session. </font></span><span><font face="Times New Roman">e.g. <strong>session.invalidate();</strong></font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/07/38-servlets-20-mins-drill-and-quiz/#comments" title="Comment on 38. Servlets 20 mins drill and Quiz."><font face="Times New Roman">3 comments</font></a><font face="Times New Roman"> <em>September 7th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/07/37-what-is-servlet-lazy-loading/"><font face="Times New Roman">37. What is servlet lazy loading.</font></a></span></h3>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">A container doesnot initialize the servlets ass soon as it starts up, it initializes a servlet when it receives a request for that servlet first time. This is called lazy loading. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">The servlet specification defines the &lt;load-on-startup&gt; element, which can be specified in the deployment descriptor to make the servlet container load and initialize the servlet as soon as it starts up. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman"> The process of loading a servlet before any request comes in is called preloading or preinitializing a servlet. </font></span></li>
</ul>
<p><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/07/37-what-is-servlet-lazy-loading/#respond" title="Comment on 37. What is servlet lazy loading."><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 7th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/07/36-sir-do-you-know-about-jsp-application-object/"><font face="Times New Roman">36. Sir, do you know about jsp application object</font></a></span></h3>
<ul>
<li class="MsoNormal"><font face="Times New Roman"><span style="color:blue;">This implicit object represents the application to which the JSP belongs.</span><span> </span></font></li>
<li class="MsoNormal"><font face="Times New Roman"><span style="color:blue;"> JSPs are grouped into applications according to their URLs where the first directory name in a URL defines the application.</span><span> </span></font></li>
<li class="MsoNormal"><span style="color:blue;"><font face="Times New Roman">The </font></span><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">application</span></code><font face="Times New Roman"><span style="color:blue;"> object contains methods that provide information about the JSP Container, support for logging plus utility methods for example translating a relative URL to an absolute path.</span><span> </span></font></li>
</ul>
<h6><font face="Times New Roman"><span style="color:red;">Servlet Class</span><span></span></font></h6>
<p><code><span style="font-size:10pt;color:red;font-family:'Courier New';">javax.servlet.ServletContext</span></code><span><font face="Times New Roman"> </font></span><span><font face="Times New Roman">The following table summarises the most useful methods available to the </font></span><code><span style="font-size:10pt;font-family:'Courier New';">application </span></code><span><font face="Times New Roman">object.</font></span></p>
<table border="1" cellPadding="0" cellSpacing="0" style="border-collapse:collapse;border:medium none;">
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0;"><strong><font size="3"><font face="Times New Roman"><span style="color:red;">Method</span></font></font></strong></td>
<td style="background-color:transparent;border:#ece9d8;padding:0;"><strong><font size="3"><font face="Times New Roman"><span style="color:red;">Description</span></font></font></strong></td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">getAttribute(String objName)</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Returns the object named objName or returns null if the objName does not exist.</font></p>
</td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">getAttributeNames()</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Returns an Enumeration containing the attribute names available within the application.</font></p>
</td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">setAttribute(String objName, Object object)</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Stores the object with the given object name in this application.</font></p>
</td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">removeAttribute(String objName)</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Removes the objName object from the application.</font></p>
</td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">getMajorVersion()</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Returns the major version of the Servlet API for the JSP COntainer.</font></p>
</td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">getServerInfo()</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Returns information about the JSP Container, typically, name and product version.</font></p>
</td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">getResourceAsStream(Path)</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Translates the resource URL into an input stream for reading.</font></p>
</td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">log(Message)</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Writes a text string to the JSP Containers default log file.</font></p>
</td>
</tr>
</table>
<p><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/07/36-sir-do-you-know-about-jsp-application-object/#respond" title="Comment on 36. Sir, do you know about jsp application object"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 7th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/07/35-tell-me-something-about-jsp-session-object/"><font face="Times New Roman">35. Tell me something about jsp session object</font></a></span></h3>
<ul>
<li class="MsoNormal"><span style="color:blue;"><font face="Times New Roman">The </font></span><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">session</span></code><font face="Times New Roman"><span style="color:blue;"> implicit object is used to provide an association between the client and the server. </span><span></span></font></li>
<li class="MsoNormal"><font face="Times New Roman"><span style="color:blue;">This association, or session, persists over multiple connections and/or requests during a given time period. </span><span></span></font></li>
<li class="MsoNormal"><font face="Times New Roman"><span style="color:blue;">Sessions are used to maintain state and user identity across multiple page requests.</span><span> </span></font></li>
<li class="MsoNormal"><span style="color:blue;"><font face="Times New Roman">A session can be maintained either by using cookies or by URL rewriting. To expose whether the client supports cookies, session defines an </font></span><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">isCookieSupportDetermined()</span></code><span style="color:blue;"><font face="Times New Roman"> method and an </font></span><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">isUsingCookies()</span></code><font face="Times New Roman"><span style="color:blue;"> method.</span><span> </span></font></li>
</ul>
<h6><font face="Times New Roman"><span style="color:red;">Servlet Class</span><span></span></font></h6>
<p><code><span style="font-size:10pt;color:red;font-family:'Courier New';">HttpSession</span></code><span><font face="Times New Roman"> </font></span><span><font face="Times New Roman">The following table summarises the most useful methods available to the </font></span><code><span style="font-size:10pt;font-family:'Courier New';">session</span></code><span><font face="Times New Roman"> object. </font></span></p>
<table border="1" cellPadding="0" cellSpacing="0" style="border-collapse:collapse;border:medium none;">
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0;"><strong><font size="3"><font face="Times New Roman"><span style="color:red;">Method</span></font></font></strong></td>
<td style="background-color:transparent;border:#ece9d8;padding:0;"><strong><font size="3"><font face="Times New Roman"><span style="color:red;">Description</span></font></font></strong></td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">isNew()</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">A session is considered to be “new” if it has been created by the server, but the client has not yet acknowledged joining the session.</font></p>
</td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">invalidate()</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Discards the session, releasing any objects stored as attributes.</font></p>
</td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">getAttribute(String)</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Retrives the object associated with the named attribute.</font></p>
</td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">getAttributeNames()</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Retrives the names of all attributes currently associated with the session.</font></p>
</td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">setAttribute(String, object)</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Sets the object to the named attribute. attribute created if it doesn’t exist.</font></p>
</td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">removeAttribute(String)</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Removes the object bound with the specified name from this session.</font></p>
</td>
</tr>
</table>
<p><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/07/35-tell-me-something-about-jsp-session-object/#respond" title="Comment on 35. Tell me something about jsp session object"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 7th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/07/34-what-do-you-know-about-jsp-response-object/"><font face="Times New Roman">34. What do you know about jsp response object</font></a></span></h3>
<ul>
<li class="MsoNormal"><span style="color:blue;"><font face="Times New Roman">The </font></span><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">response</span></code><font face="Times New Roman"><span style="color:blue;"> object handles the output to the client. </span><span></span></font></li>
<li class="MsoNormal"><font face="Times New Roman"><span style="color:blue;">This object can be used for creating HTTP Headers, creating cookies, setting content type and redirecting workflow.</span><span> </span></font></li>
</ul>
<h6><font face="Times New Roman"><span style="color:red;">Servlet Class</span><span></span></font></h6>
<p><code><span style="font-size:10pt;color:red;font-family:'Courier New';">javax.servlet.http.HttpServletResponse</span></code><span><font face="Times New Roman"> </font></span><span><font face="Times New Roman">The following table summarises the most useful methods available to the </font></span><code><span style="font-size:10pt;font-family:'Courier New';">response</span></code><span><font face="Times New Roman"> object.</font></span></p>
<table border="1" cellPadding="0" cellSpacing="0" style="border-collapse:collapse;border:medium none;">
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0;"><strong><font size="3"><font face="Times New Roman"><span style="color:red;">Method</span></font></font></strong></td>
<td style="background-color:transparent;border:#ece9d8;padding:0;"><strong><font size="3"><font face="Times New Roman"><span style="color:red;">Description</span></font></font></strong></td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">setContentType()</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Sets the MIME type and character encoding for the page.</font></p>
</td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">addCookie(Cookie)</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Adds the specified cookie to the response. It can be called multiple times to set more than one cookie.</font></p>
</td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">containsHeader(name)</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Checks whether the response already includes this header. </font></p>
<p style="margin:0;"> </p>
</td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">setHeader(name, value)</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Creates an HTTP Header.</font></p>
</td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">sendRedirect(String)</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Sends a temporary redirect response to the client using the specified redirect location URL.</font></p>
</td>
</tr>
</table>
<p><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/07/34-what-do-you-know-about-jsp-response-object/#respond" title="Comment on 34. What do you know about jsp response object"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 7th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/07/33-explain-request-implicit-object/"><font face="Times New Roman">33. Explain request implicit object</font></a></span></h3>
<ol>
<li class="MsoNormal"><span style="color:blue;"><font face="Times New Roman">The </font></span><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">request</span></code><font face="Times New Roman"><span style="color:blue;"> object retrieves the values that the client browser passed to the server during an HTTP request such as headers, cookies or parameters associated with the request. </span><span></span></font></li>
<li class="MsoNormal"><span style="color:blue;"><font face="Times New Roman">Among the most common use of the </font></span><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">request</span></code><font face="Times New Roman"><span style="color:blue;"> object is to obtain parammeter or query string values.</span><span> </span></font></li>
</ol>
<p><span><font face="Times New Roman">Servlet Class</font></span><strong><span style="font-size:10pt;color:red;font-family:'Courier New';">javax.servlet.HttpServletRequest</span></strong><span><font face="Times New Roman"> </font></span><span><font face="Times New Roman">The following table summarises the most useful methods available to the </font></span><code><span style="font-size:10pt;font-family:'Courier New';">request</span></code><span><font face="Times New Roman"> object. </font></span></p>
<table border="1" cellPadding="0" cellSpacing="0" style="border:4.5pt outset;">
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;"><strong><font size="3"><font face="Times New Roman"><span style="color:red;">Method</span></font></font></strong></td>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;"><strong><font size="3"><font face="Times New Roman"><span style="color:red;">Description</span></font></font></strong></td>
</tr>
<tr>
<td noWrap="true" style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">getParameter(String name)</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Returns the value of a request parameter as a String, or null if the parameter does not exist.</font></p>
</td>
</tr>
<tr>
<td noWrap="true" style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">getParameterNames()</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Returns an Enumeration of String objects containing the names of the parameters contained in this request.</font></p>
</td>
</tr>
<tr>
<td noWrap="true" style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">getParameterValues(String name)</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist.</font></p>
</td>
</tr>
<tr>
<td noWrap="true" style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">getCookies()</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Gets the array of cookies found in this request. See below for more details on working with cookies.</font></p>
</td>
</tr>
<tr>
<td noWrap="true" style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">getQueryString()</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Gets any query string that is part of the HTTP request URI.</font></p>
</td>
</tr>
<tr>
<td noWrap="true" style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">getRequestURI()</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Gets the URI to the current JSP page.</font></p>
</td>
</tr>
<tr>
<td noWrap="true" style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">getHeaderNames()</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Returns an enumerator of all HTTP header names.</font></p>
</td>
</tr>
<tr>
<td noWrap="true" style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">getHeader(String hdr)</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Returns the value of an HTTP header such as “QUERY_STRING” or “URL”. The method </font><code><span style="font-size:10pt;font-family:'Courier New';">getHeaderNames()</span></code><font size="3" face="Times New Roman"> can be used to determine what headers are available. See example below.</font></p>
</td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">getAttribute(String)</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Retrives the object associated with the named attribute.</font></p>
</td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">getAttributeNames()</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Retrives the names of all attributes currently associated with the session.</font></p>
</td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">setAttribute(String, object)</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Sets the object to the named attribute. attribute created if it doesn’t exist.</font></p>
</td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;color:blue;font-family:'Courier New';">removeAttribute(String)</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Removes the object bound with the specified name from this session.</font></p>
</td>
</tr>
</table>
<p><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/07/33-explain-request-implicit-object/#respond" title="Comment on 33. Explain request implicit object"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 7th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/06/32-what-is-the-difference-between-servletcontext-and-servletconfig/"><font face="Times New Roman">32. What is the difference between ServletContext and ServletConfig</font></a></span></h3>
<p><font face="Times New Roman"><strong><u><span style="color:red;">public interface ServletContext</span></u></strong><span></span></font></p>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">Defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">There is one context per “web application” per Java Virtual Machine. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">The </font></span><code><span style="font-size:10pt;font-family:'Courier New';">ServletContext</span></code><span><font face="Times New Roman"> object is contained within the </font><a href="http://java.sun.com/javax/servlet/ServletConfig.html"><code><span style="font-size:10pt;font-family:'Courier New';">ServletConfig</span></code></a><font face="Times New Roman"> object, which the Web server provides the servlet when the servlet is initialized. </font></span></li>
</ul>
<p><font face="Times New Roman"><strong><u><span style="color:red;">public interface ServletConfig</span></u></strong><span></span></font></p>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">A servlet configuration object used by a servlet container used to pass information to a servlet during initialization. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">This object defines how a servlet is to be configured is passed to a servlet in its </font></span><code><span style="font-size:10pt;font-family:'Courier New';">init</span></code><span><font face="Times New Roman"> method. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Most servlet containers provide a way to configure a servlet at run-time (usually through flat file) and set up its initial parameters. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">The container, in turn, passes these parameters to the servlet via the </font></span><code><span style="font-size:10pt;font-family:'Courier New';">ServetConfig</span></code><span><font face="Times New Roman">. </font></span></li>
</ul>
<p><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/06/32-what-is-the-difference-between-servletcontext-and-servletconfig/#comments" title="Comment on 32. What is the difference between ServletContext and ServletConfig"><font face="Times New Roman">2 comments</font></a><font face="Times New Roman"> <em>September 6th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/06/31-explain-servlet-life-cycle/"><font face="Times New Roman">31. Explain Servlet Life Cycle.</font></a></span></h3>
<ol>
<li class="MsoNormal"><font face="Times New Roman"><span style="color:#0000a0;">Servlets are normal Java classes which are created when needed and destroyed when not needed. Since Servlets run within a Servlet Container, creation and destruction of Servlets is the duty of Servlet Container and not yours.</span><span> </span></font></li>
<li class="MsoNormal"><span style="color:#8000ff;"><font face="Times New Roman">Implementing the </font></span><code><span style="font-size:10pt;color:#8000ff;font-family:'Courier New';">init()</span></code><span style="color:#8000ff;"><font face="Times New Roman"> and </font></span><code><span style="font-size:10pt;color:#8000ff;font-family:'Courier New';">destory()</span></code><span style="color:#8000ff;"><font face="Times New Roman"> methods of </font></span><code><span style="font-size:10pt;color:#8000ff;font-family:'Courier New';">Servlet</span></code><font face="Times New Roman"><span style="color:#8000ff;"> interface allows you to be told by the Servlet Container that when it has created an instance of your Servlet and when it has destroyed that instance.</span><span> </span></font></li>
<li class="MsoNormal"><font face="Times New Roman"><span style="color:#ff0080;">An important point to remember is that your Servlet is not created and destroyed for every request it receives, rather it is created and kept in memory where requests are forwarded to it and your Servlet then generates response.</span><span> </span></font></li>
</ol>
<p><strong><u><span style="color:red;"><a href="http://sharat.files.wordpress.com/2006/09/WindowsLiveWriter/31.ExplainServletLifeCycle_FD1F/servletlifecycle[35].png"><span style="text-decoration:none;"></span></a><font face="Times New Roman">   </font></span></u></strong><span></span><font face="Times New Roman"><strong><u><span style="color:red;">Servlet Initialization (</span></u></strong><strong><u><span style="color:#004080;">init()</span></u></strong><strong><u><span style="color:red;">)</span></u></strong></font><span><br />
<font face="Times New Roman">So you have created your Servlet class by extending the </font></span><code><span style="font-size:10pt;font-family:'Courier New';">HttpServlet</span></code><span><font face="Times New Roman"> class and have placed it in </font></span><code><span style="font-size:10pt;font-family:'Courier New';">/WEB-INF/classes/</span></code><span><font face="Times New Roman"> directory of your application. </font></span><span><font face="Times New Roman">Now when will Servlet Container create an instance of your Servlet? there are two situations :</font></span></p>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">When you have specifically told the Servlet Container to preload your Servlet when the Servlet Container starts by setting the </font></span><code><span style="font-size:10pt;font-family:'Courier New';">load-on-startup</span></code><span><font face="Times New Roman"> tag to a non-zero value e.g. </font></span></li>
</ul>
<pre style="margin-left:0.5in;text-indent:-0.25in;"><span style="font-family:Symbol;"><font size="2">·</font><span style="font:7pt 'Times New Roman';">         </span></span><span><font size="2">&lt;web-app&gt;</font></span></pre>
<pre style="margin-left:0.5in;text-indent:-0.25in;"><span style="font-family:Symbol;"><font size="2">·</font><span style="font:7pt 'Times New Roman';">         </span></span><span><font size="2"> </font></span></pre>
<pre style="margin-left:0.5in;text-indent:-0.25in;"><span style="font-family:Symbol;"><font size="2">·</font><span style="font:7pt 'Times New Roman';">         </span></span><span><font size="2"><span>  </span>&lt;servlet&gt;</font></span></pre>
<pre style="margin-left:0.5in;text-indent:-0.25in;"><span style="font-family:Symbol;"><font size="2">·</font><span style="font:7pt 'Times New Roman';">         </span></span><span><font size="2"><span>    </span>&lt;servlet-name&gt;test&lt;/servlet-name&gt;</font></span></pre>
<pre style="margin-left:0.5in;text-indent:-0.25in;"><span style="font-family:Symbol;"><font size="2">·</font><span style="font:7pt 'Times New Roman';">         </span></span><span><font size="2"><span>    </span>&lt;servlet-class&gt;com.wordpress.sharat.servlets.TestServlet&lt;/servlet-class&gt;</font></span></pre>
<pre style="margin-left:0.5in;text-indent:-0.25in;"><span style="font-family:Symbol;"><font size="2">·</font><span style="font:7pt 'Times New Roman';">         </span></span><span><font size="2"><span>    </span>&lt;load-on-startup&gt;1&lt;/load-on-startup&gt;</font></span></pre>
<pre style="margin-left:0.5in;text-indent:-0.25in;"><span style="font-family:Symbol;"><font size="2">·</font><span style="font:7pt 'Times New Roman';">         </span></span><span><font size="2"><span>  </span>&lt;/servlet&gt;</font></span></pre>
<pre style="margin-left:0.5in;text-indent:-0.25in;"><span style="font-family:Symbol;"><font size="2">·</font><span style="font:7pt 'Times New Roman';">         </span></span><span><font size="2"> </font></span></pre>
<pre style="margin-left:0.5in;"><span><font size="2">&lt;/web-app&gt;</font></span></pre>
<p><span><font face="Times New Roman">So when the Servlet Container starts it will preload your Servlet in the memory. </font></span></p>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">If your Servlet is not already loaded, it’s instance will be created as soon as a request is received for it by the Servlet Container. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">During the loading of the Servlet into the memory, Servlet Container will call your Servlet’s </font></span><code><span style="font-size:10pt;font-family:'Courier New';">init()</span></code><span><font face="Times New Roman"> method. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Since </font></span><code><span style="font-size:10pt;font-family:'Courier New';">init()</span></code><span><font face="Times New Roman"> is going to be called only once you can put Servlet initialization code in it like getting hold of a database connection or whatever. </font></span></li>
</ul>
<p><font face="Times New Roman"><strong><u><span style="color:red;">Responding to Requests(</span></u></strong><strong><u><span style="color:#004080;">service()</span></u></strong><strong><u><span style="color:red;">)</span></u></strong><span></span></font></p>
<ol>
<li class="MsoNormal"><span><font face="Times New Roman">Once your Servlet is initialized and it’s </font></span><code><span style="font-size:10pt;font-family:'Courier New';">init()</span></code><span><font face="Times New Roman"> method called, any request that the Servlet Container receives will be forwarded to your Servlet’s </font></span><code><span style="font-size:10pt;font-family:'Courier New';">service()</span></code><span><font face="Times New Roman"> method. </font></span></li>
<li class="MsoNormal"><code><span style="font-size:10pt;font-family:'Courier New';">HttpServlet</span></code><span><font face="Times New Roman"> class breakes this </font></span><code><span style="font-size:10pt;font-family:'Courier New';">service()</span></code><span><font face="Times New Roman"> method into more useful </font></span><code><span style="font-size:10pt;font-family:'Courier New';">doGet(), doPost(), doDelete(), doOptions(), doPut()</span></code><span><font face="Times New Roman"> and </font></span><code><span style="font-size:10pt;font-family:'Courier New';">doTrace()</span></code><span><font face="Times New Roman"> methods depending on the type of HTTP request it receives. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">So in order to generate respose you should override the </font></span><code><span style="font-size:10pt;font-family:'Courier New';">doGet()</span></code><span><font face="Times New Roman"> or </font></span><code><span style="font-size:10pt;font-family:'Courier New';">doPost()</span></code><span><font face="Times New Roman"> method as required. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">At this moment all the requests will be forwarded to the appropriate </font></span><code><span style="font-size:10pt;font-family:'Courier New';">doGet()</span></code><span><font face="Times New Roman"> or </font></span><code><span style="font-size:10pt;font-family:'Courier New';">doPost()</span></code><span><font face="Times New Roman"> or whatever method as required. No new instance will be created for your Servlet. </font></span></li>
</ol>
<p><font face="Times New Roman"><strong><u><span style="color:red;">Servlet Destruction(</span></u></strong><strong><u><span style="color:#004080;">destory()</span></u></strong><strong><u><span style="color:red;">)</span></u></strong><span></span></font><span><font face="Times New Roman">When your application is stopped or Servlet Container shuts down, your Servlet’s </font></span><code><span style="font-size:10pt;font-family:'Courier New';">destroy()</span></code><span><font face="Times New Roman"> method will be called. This allows you to free any resources you may have got hold of in your Servlet’s </font></span><code><span style="font-size:10pt;font-family:'Courier New';">init()</span></code><span><font face="Times New Roman"> method.</font></span><font face="Times New Roman"><strong><u><span style="color:red;">Always Remember</span></u></strong><span></span></font></p>
<ul>
<li class="MsoNormal"><code><span style="font-size:10pt;font-family:'Courier New';">init()</span></code><span><font face="Times New Roman"> and </font></span><code><span style="font-size:10pt;font-family:'Courier New';">destroy()</span></code><span><font face="Times New Roman"> methods will be called <em>only once</em> during the life time of your Servlet </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman"> while </font></span><code><span style="font-size:10pt;font-family:'Courier New';">service()</span></code><span><font face="Times New Roman"> and it’s broken down methods ( </font></span><code><span style="font-size:10pt;font-family:'Courier New';">doGet(), doPost()</span></code><span><font face="Times New Roman"> etc ) will be called as many times as requests are received for them by the Servlet Container. </font></span></li>
</ul>
<p><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/06/31-explain-servlet-life-cycle/#comments" title="Comment on 31. Explain Servlet Life Cycle."><font face="Times New Roman">3 comments</font></a><font face="Times New Roman"> <em>September 6th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/06/30-explain-the-advantages-of-jstl/"><font face="Times New Roman">30. Explain the advantages of JSTL</font></a></span></h3>
<ol>
<li class="MsoNormal"><span><font face="Times New Roman">The Java Standard Tag Library (JSTL) is a standardized collection of custom tags. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">It has a number of tags for common tasks such as iterating through lists, interacting with databases, handling XML data, formatting data, and much more. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">The latest version of JSTL is 1.1. An implementation of JSTL can be downloaded from the Apache Jakarta Web site (</font><a href="http://jakarta.apache.org/"><font face="Times New Roman">http://jakarta.apache.org/</font></a><font face="Times New Roman">). </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">JSTL consist of four sets of tags and a library of functions, grouped according to their functionality: </font></span></li>
</ol>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">The JSTL core tag library contains tags that deal with flow control in JSP pages, iterating over collections, evaluating expressions, and importing resources. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Before using the core tag library, the following directive needs to be added to the JSP page: </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">&lt;%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %&gt; </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">The equivalent XML syntax for this directive is as follows: </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">&lt;jsp:directive.taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” /&gt; </font></span></li>
</ul>
<p><font face="Times New Roman"><strong><span style="color:blue;">The following table summarizes the JSTL core tags.</span></strong><span></span></font></p>
<table border="1" cellPadding="0">
<thead>
<td width="23%" vAlign="top" style="width:23%;background-color:transparent;border:#ece9d8;padding:0.75pt;"><strong><font size="3"><font face="Times New Roman"><span style="color:#ff0080;">Tag Name</span></font></font></strong></td>
<td width="77%" vAlign="top" style="width:77%;background-color:transparent;border:#ece9d8;padding:0.75pt;"><strong><font size="3"><font face="Times New Roman"><span style="color:#ff0080;">Description</span></font></font></strong></td>
</tr>
<tr>
<td width="23%" vAlign="top" style="width:23%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="table-para"><font size="3"><font face="Times New Roman"><span class="fixed"><span style="color:blue;">catch</span></span><span style="color:blue;"> </span></font></font></p>
</td>
<td width="77%" vAlign="top" style="width:77%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="table-para"><font size="3" face="Times New Roman">Catches any <span class="fixed"><span>Throwable</span></span> exception that occurs in the body of the tag </font></p>
</td>
</tr>
<tr>
<td width="23%" vAlign="top" style="width:23%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="table-para"><font size="3"><font face="Times New Roman"><span class="fixed"><span style="color:blue;">choose</span></span> </font></font></p>
</td>
<td width="77%" vAlign="top" style="width:77%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="table-para"><font size="3" face="Times New Roman">Provides conditional operation and allows for choosing between mutually exclusive options</font></p>
</td>
</tr>
<tr>
<td width="23%" vAlign="top" style="width:23%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="table-para"><font size="3"><font face="Times New Roman"><span class="fixed"><span style="color:red;">if</span></span> </font></font></p>
</td>
<td width="77%" vAlign="top" style="width:77%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="table-para"><font size="3" face="Times New Roman">Provides conditional operation</font></p>
</td>
</tr>
<tr>
<td width="23%" vAlign="top" style="width:23%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="table-para"><font size="3"><font face="Times New Roman"><span class="fixed"><span style="color:red;">import</span></span> </font></font></p>
</td>
<td width="77%" vAlign="top" style="width:77%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="table-para"><font size="3" face="Times New Roman">Imports a resource specified by a URL and exposes it to the page, variable, or reader</font></p>
</td>
</tr>
<tr>
<td width="23%" vAlign="top" style="width:23%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="table-para"><font size="3"><font face="Times New Roman"><span class="fixed"><span style="color:red;">forEach</span></span> </font></font></p>
</td>
<td width="77%" vAlign="top" style="width:77%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="table-para"><font size="3" face="Times New Roman">Iterates over collections</font></p>
</td>
</tr>
<tr>
<td width="23%" vAlign="top" style="width:23%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="table-para"><font size="3"><font face="Times New Roman"><span class="fixed"><span style="color:red;">forTokens</span></span> </font></font></p>
</td>
<td width="77%" vAlign="top" style="width:77%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="table-para"><font size="3" face="Times New Roman">Iterates over tokens</font></p>
</td>
</tr>
<tr>
<td width="23%" vAlign="top" style="width:23%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="table-para"><font size="3"><font face="Times New Roman"><span class="fixed"><span style="color:blue;">out</span></span> </font></font></p>
</td>
<td width="77%" vAlign="top" style="width:77%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="table-para"><font size="3" face="Times New Roman">Outputs expressions to the Web page</font></p>
</td>
</tr>
<tr>
<td width="23%" vAlign="top" style="width:23%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="table-para"><font size="3"><font face="Times New Roman"><span class="fixed"><span style="color:blue;">otherwise</span></span> </font></font></p>
</td>
<td width="77%" vAlign="top" style="width:77%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="table-para"><font size="3" face="Times New Roman">A conditional operation tag for choosing between mutually exclusive options. This tag is a subtag of the <span class="fixed"><span>choose</span></span> tag. </font></p>
</td>
</tr>
<tr>
<td width="23%" vAlign="top" style="width:23%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="table-para"><font size="3"><font face="Times New Roman"><span class="fixed"><span style="color:blue;">param</span></span> </font></font></p>
</td>
<td width="77%" vAlign="top" style="width:77%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="table-para"><font size="3" face="Times New Roman">Used along with the <span class="fixed"><span>import</span></span>, <span class="fixed"><span>redirect</span></span>, or <span class="fixed"><span>url</span></span> tags to add a parameter to a URL </font></p>
</td>
</tr>
<tr>
<td width="23%" vAlign="top" style="width:23%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="table-para"><font size="3"><font face="Times New Roman"><span class="fixed"><span style="color:red;">redirect</span></span> </font></font></p>
</td>
<td width="77%" vAlign="top" style="width:77%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="table-para"><font size="3" face="Times New Roman">Redirects to a new URL</font></p>
</td>
</tr>
<tr>
<td width="23%" vAlign="top" style="width:23%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="table-para"><font size="3"><font face="Times New Roman"><span class="fixed"><span style="color:red;">remove</span></span> </font></font></p>
</td>
<td width="77%" vAlign="top" style="width:77%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="table-para"><font size="3" face="Times New Roman">Removes a variable from a scope</font></p>
</td>
</tr>
<tr>
<td width="23%" vAlign="top" style="width:23%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="table-para"><font size="3"><font face="Times New Roman"><span class="fixed"><span style="color:red;">set</span></span> </font></font></p>
</td>
<td width="77%" vAlign="top" style="width:77%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="table-para"><font size="3" face="Times New Roman">Sets the value of an attribute</font></p>
</td>
</tr>
<tr>
<td width="23%" vAlign="top" style="width:23%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="table-para"><font size="3"><font face="Times New Roman"><span class="fixed"><span style="color:blue;">url</span></span> </font></font></p>
</td>
<td width="77%" vAlign="top" style="width:77%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="table-para"><font size="3" face="Times New Roman">Creates a URL</font></p>
</td>
</tr>
<tr>
<td width="23%" vAlign="top" style="width:23%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="table-para"><font size="3"><font face="Times New Roman"><span class="fixed"><span style="color:blue;">when</span></span> </font></font></p>
</td>
<td width="77%" vAlign="top" style="width:77%;background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="table-para"><font size="3" face="Times New Roman">A conditional operation tag that includes its body when an expression evaluates to <span class="fixed"><span>true</span></span>. This tag is a subtag of the <span class="fixed"><span>choose</span></span> tag. </font></p>
</td>
</tr>
</table>
<p><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/06/30-explain-the-advantages-of-jstl/#comments" title="Comment on 30. Explain the advantages of JSTL"><font face="Times New Roman">8 comments</font></a><font face="Times New Roman"> <em>September 6th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/06/29-what-are-the-advantages-of-jsp/"><font face="Times New Roman">29. What are the advantages of JSP</font></a></span></h3>
<ul>
<li class="MsoNormal"><font face="Times New Roman"><em><span>Separation of static from dynamic content.</span></em><span> </span></font></li>
<li class="MsoNormal"><font face="Times New Roman"><em><span>Write Once Run Anywhere.</span></em><span> </span></font></li>
<li class="MsoNormal"><font face="Times New Roman"><em><span>Dynamic content can be served in a variety of formats.</span></em><span> </span></font></li>
</ul>
<p><font face="Times New Roman"><em><span>            </span></em><span>example: browsers using HTML/DHTML, to handheld wireless devices like mobile phones and PDAs using WML, to other B2B applications using XML</span></font></p>
<ul>
<li class="MsoNormal"><font face="Times New Roman"><em><span>Recommended Web access layer for n-tier architecture.</span></em><span> </span></font></li>
<li class="MsoNormal"><font face="Times New Roman"><em><span>Completely leverages the Servlet API.</span></em><span> </span></font></li>
</ul>
<p><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/06/29-what-are-the-advantages-of-jsp/#respond" title="Comment on 29. What are the advantages of JSP"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 6th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/06/28-explain-jsp-basic-structure-items/"><font face="Times New Roman">28. Explain JSP Basic Structure Items.</font></a></span></h3>
<p><font face="Times New Roman"><strong><u><span style="color:red;">JSP Expression </span></u></strong><span></span></font></p>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">&lt;%= expression %&gt; </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Expression is evaluated and placed in output. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">XML equivalent is<br />
</font></span><code><span style="font-size:10pt;font-family:'Courier New';">&lt;jsp:expression&gt;</span></code><span style="font-size:10pt;font-family:'Courier New';"><br />
<code>expression</code><br />
<code>&lt;/jsp:expression&gt;</code></span><span><font face="Times New Roman">. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Predefined variables are </font></span><code><span style="font-size:10pt;font-family:'Courier New';">request</span></code><span><font face="Times New Roman">,</font></span><code><span style="font-size:10pt;font-family:'Courier New';">response</span></code><span><font face="Times New Roman">,</font></span><code><span style="font-size:10pt;font-family:'Courier New';">out</span></code><span><font face="Times New Roman">,</font></span><code><span style="font-size:10pt;font-family:'Courier New';">session</span></code><span><font face="Times New Roman">,</font></span><code><span style="font-size:10pt;font-family:'Courier New';">application</span></code><span><font face="Times New Roman">,</font></span><code><span style="font-size:10pt;font-family:'Courier New';">config</span></code><span><font face="Times New Roman">, and</font></span><code><span style="font-size:10pt;font-family:'Courier New';">pageContext</span></code><span><font face="Times New Roman"> (available in scriptlets also). </font></span></li>
</ul>
<p><font face="Times New Roman"><strong><u><span style="color:red;">JSP Scriptlet </span></u></strong><span></span></font></p>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">&lt;% code %&gt; </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Code is inserted in </font></span><code><span style="font-size:10pt;font-family:'Courier New';">service</span></code><span><font face="Times New Roman"> method. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">XML equivalent is<br />
</font></span><code><span style="font-size:10pt;font-family:'Courier New';">&lt;jsp:scriptlet&gt;</span></code><span style="font-size:10pt;font-family:'Courier New';"><br />
<code>code</code><br />
<code>&lt;/jsp:scriptlet&gt;</code></span><span><font face="Times New Roman">. </font></span></li>
</ul>
<p><font face="Times New Roman"><strong><u><span style="color:red;">JSP Declaration </span></u></strong><span></span></font></p>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">&lt;%! code %&gt; </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Code is inserted in body of servlet class, outside of </font></span><code><span style="font-size:10pt;font-family:'Courier New';">service</span></code><span><font face="Times New Roman"> method. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">XML equivalent is<br />
</font></span><code><span style="font-size:10pt;font-family:'Courier New';">&lt;jsp:declaration&gt;</span></code><span style="font-size:10pt;font-family:'Courier New';"><br />
<code>code</code><br />
<code>&lt;/jsp:declaration&gt;</code></span><span><font face="Times New Roman">. </font></span></li>
</ul>
<p><strong><u><span style="color:red;"><font face="Times New Roman">JSP </font></span></u></strong><code><strong><u><span style="font-size:10pt;color:red;font-family:'Courier New';">page</span></u></strong></code><font face="Times New Roman"><strong><u><span style="color:red;"> Directive </span></u></strong><span></span></font></p>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">&lt;%@ page att=”val” %&gt; </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Directions to the servlet engine about general setup. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">XML equivalent is<br />
</font></span><code><span style="font-size:10pt;font-family:'Courier New';">&lt;jsp:directive.page att="val"\&gt;</span></code><span><font face="Times New Roman">. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Legal attributes, with default values in bold, are: </font></span>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">import=”<em>package.class</em>” </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">contentType=”<em>MIME-Type</em>” </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">isThreadSafe=”<strong>true</strong>|false” </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">session=”<strong>true</strong>|false” </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">buffer=”<em>size</em>kb|none” </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">autoflush=”<strong>true</strong>|false” </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">extends=”<em>package.class</em>” </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">info=”<em>message</em>” </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">errorPage=”<em>url</em>” </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">isErrorPage=”true|<strong>false</strong>” </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">language=”java” </font></span></li>
</ul>
</li>
</ul>
<p><strong><u><span style="color:red;"><font face="Times New Roman">JSP </font></span></u></strong><code><strong><u><span style="font-size:10pt;color:red;font-family:'Courier New';">include</span></u></strong></code><font face="Times New Roman"><strong><u><span style="color:red;"> Directive </span></u></strong><span></span></font></p>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">&lt;%@ include file=”url” %&gt; </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">A file on the local system to be included when the JSP page is translated into a servlet. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">XML equivalent is<br />
</font></span><code><span style="font-size:10pt;font-family:'Courier New';">&lt;jsp:directive.include</span></code><span style="font-size:10pt;font-family:'Courier New';"><br />
<code>  file=”url”\&gt;</code></span><span><font face="Times New Roman">. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">The URL must be a relative one. Use the </font></span><code><span style="font-size:10pt;font-family:'Courier New';">jsp:include</span></code><span><font face="Times New Roman"> action to include a file at request time instead of translation time. </font></span></li>
</ul>
<p><font face="Times New Roman"><strong><u><span style="color:red;">JSP Comment </span></u></strong><span></span></font></p>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">&lt;%– comment –%&gt; </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Comment; ignored when JSP page is translated into servlet. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">If you want a comment in the resultant HTML, use regular HTML comment syntax of </font></span><code><span style="font-size:10pt;font-family:'Courier New';">&lt;-- comment --&gt;</span></code><span><font face="Times New Roman">. </font></span></li>
</ul>
<p><strong><u><span style="color:red;"><font face="Times New Roman">The </font></span></u></strong><code><strong><u><span style="font-size:10pt;color:red;font-family:'Courier New';">jsp:include</span></u></strong></code><font face="Times New Roman"><strong><u><span style="color:red;"> Action</span></u></strong><span> </span></font></p>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">&lt;jsp:include page=”<em>relative URL</em>” flush=”true”/&gt; </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Includes a file at the time the page is requested. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">If you want to include the file at the time the page is translated, use the </font></span><code><span style="font-size:10pt;font-family:'Courier New';">page</span></code><span><font face="Times New Roman"> directive with the</font></span><code><span style="font-size:10pt;font-family:'Courier New';">include</span></code><span><font face="Times New Roman"> attribute instead. Warning: on some servers, the included file must be an HTML file or JSP file, as determined by the server (usually based on the file extension). </font></span></li>
</ul>
<p><strong><u><span style="color:red;"><font face="Times New Roman">The </font></span></u></strong><code><strong><u><span style="font-size:10pt;color:red;font-family:'Courier New';">jsp:forward</span></u></strong></code><font face="Times New Roman"><strong><u><span style="color:red;"> Action </span></u></strong><span></span></font></p>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">&lt;jsp:forward page=”<em>relative URL</em>“/&gt; </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Forwards request to another page. </font></span></li>
</ul>
<p><strong><u><span style="color:red;"><font face="Times New Roman">The </font></span></u></strong><code><strong><u><span style="font-size:10pt;color:red;font-family:'Courier New';">jsp:plugin</span></u></strong></code><font face="Times New Roman"><strong><u><span style="color:red;"> Action</span></u></strong><span> </span></font></p>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">&lt;jsp:plugin attribute=”<em>value</em>“*&gt; … &lt;/jsp:plugin&gt; </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Generates </font></span><code><span style="font-size:10pt;font-family:'Courier New';">OBJECT</span></code><span><font face="Times New Roman"> or</font></span><code><span style="font-size:10pt;font-family:'Courier New';">EMBED</span></code><span><font face="Times New Roman"> tags, as appropriate to the browser type, asking that an applet be run using the Java Plugin. </font></span></li>
</ul>
<p><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/06/28-explain-jsp-basic-structure-items/#respond" title="Comment on 28. Explain JSP Basic Structure Items."><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 6th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/06/27-how-to-handle-cookies-in-jsp/"><font face="Times New Roman">27. How to handle cookies in JSP</font></a></span></h3>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">The </font></span><code><span style="font-size:10pt;font-family:'Courier New';">getCookies()</span></code><span><font face="Times New Roman"> method of the </font></span><code><span style="font-size:10pt;font-family:'Courier New';">request</span></code><span><font face="Times New Roman"> object returns an array of </font></span><code><span style="font-size:10pt;font-family:'Courier New';">Cookie</span></code><span><font face="Times New Roman"> objects. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Cookies are used to allow web browsers to hold small amounts of information or state data associated with a user’s web browsing. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Cookies are named and have a single value. They may have optional attributes, including a comment presented to the user, path and domain qualifiers for which hosts see the cookie, a maximum age, and a version. </font></span></li>
</ul>
<h6><span><font face="Times New Roman"> </font></span></h6>
<pre><strong><span style="color:blue;"><font size="2">Cookie(java.lang.String name, java.lang.String value)</font></span></strong></pre>
<p><code><span style="font-size:10pt;font-family:'Courier New';">Cookie</span></code><span><font face="Times New Roman"> objects have the following methods.</font></span><code><strong><span style="font-size:10pt;color:red;font-family:'Courier New';">getComment()</span></strong></code><strong><span style="color:#004000;"><br />
</span></strong><span><font face="Times New Roman">Returns the comment describing the purpose of this cookie, or null if no such comment has been defined.</font></span><strong><span style="font-size:10pt;color:red;font-family:'Courier New';">getMaxAge()</span></strong><span><br />
<font face="Times New Roman">Returns the maximum specified age of the cookie.</font></span><strong><span style="font-size:10pt;color:red;font-family:'Courier New';">getName()</span></strong><span><br />
<font face="Times New Roman">Returns the name of the cookie.</font></span><strong><span style="font-size:10pt;color:red;font-family:'Courier New';">getPath()</span></strong><span><br />
<font face="Times New Roman">Returns the prefix of all URLs for which this cookie is targetted.</font></span><strong><span style="font-size:10pt;color:red;font-family:'Courier New';">getValue()</span></strong><span><br />
<font face="Times New Roman">Returns the value of the cookie.</font></span><strong><span style="font-size:10pt;color:red;font-family:'Courier New';">setComment(String)</span></strong><span><br />
<font face="Times New Roman">If a web browser presents this cookie to a user, the cookie’s purpose will be described using this comment.</font></span><strong><span style="font-size:10pt;color:red;font-family:'Courier New';">setMaxAge(int)</span></strong><span><br />
<font face="Times New Roman">Sets the maximum age of the cookie. The cookie will expire after that many seconds have passed. Negative values indicate the default behaviour: the cookie is not stored persistently, and will be deleted when the user web browser exits. A zero value causes the cookie to be deleted</font></span><strong><span style="font-size:10pt;color:red;font-family:'Courier New';">setPath(String)</span></strong><span><br />
<font face="Times New Roman">This cookie should be presented only with requests beginning with this URL.</font></span><strong><span style="font-size:10pt;color:red;font-family:'Courier New';">setValue(String)</span></strong><span><br />
<font face="Times New Roman">Sets the value of the cookie. Values with various special characters (white space, brackets and parentheses, the equals sign, comma, double quote, slashes, question marks, the “at” sign, colon, and semicolon) should be avoided. Empty values may not behave the same way on all browsers.</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/06/27-how-to-handle-cookies-in-jsp/#respond" title="Comment on 27. How to handle cookies in JSP"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 6th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/06/26-explain-jsp-session-implicit-object/"><font face="Times New Roman">26. Explain JSP Session implicit Object.</font></a></span></h3>
<ol>
<li class="MsoNormal"><span><font face="Times New Roman">The </font></span><code><span style="font-size:10pt;font-family:'Courier New';">session</span></code><span><font face="Times New Roman"> implicit object is used to provide an association between the client and the server. This association, or session, persists over multiple connections and/or requests during a given time period. Sessions are used to maintain state and user identity across multiple page requests. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">A session can be maintained either by using cookies or by URL rewriting. To expose whether the client supports cookies, session defines an </font></span><code><span style="font-size:10pt;font-family:'Courier New';">isCookieSupportDetermined()</span></code><span><font face="Times New Roman"> method and an </font></span><code><span style="font-size:10pt;font-family:'Courier New';">isUsingCookies()</span></code><span><font face="Times New Roman"> method. </font></span></li>
</ol>
<h4><font face="Times New Roman"><u><span style="color:blue;">Servlet Class</span></u><span></span></font></h4>
<p><code><span style="font-size:10pt;font-family:'Courier New';">HttpSession</span></code><span><font face="Times New Roman"> </font></span><span><font face="Times New Roman">The following table summarises the most useful methods available to the </font></span><code><span style="font-size:10pt;font-family:'Courier New';">session</span></code><span><font face="Times New Roman"> object. </font></span><code><strong><u><span style="font-size:10pt;color:red;font-family:'Courier New';">isNew()</span></u></strong></code><strong><u><span style="color:red;"><br />
</span></u></strong><span><font face="Times New Roman">A session is considered to be “new” if it has been created by the server, but the client has not yet acknowledged joining the session. </font></span><code><strong><u><span style="font-size:10pt;color:red;font-family:'Courier New';">invalidate()</span></u></strong></code><strong><u><span><br />
</span></u></strong><span><font face="Times New Roman">Discards the session, releasing any objects stored as attributes. </font></span><code><strong><u><span style="font-size:10pt;color:red;font-family:'Courier New';">getAttribute(String)</span></u></strong></code><strong><u><span><br />
</span></u></strong><span><font face="Times New Roman">Retrives the object associated with the named attribute. </font></span><strong><u><span style="font-size:10pt;color:red;font-family:'Courier New';">getAttributeNames()</span></u></strong><span><br />
<font face="Times New Roman">Retrives the names of all attributes currently associated with the session. </font></span><code><strong><u><span style="font-size:10pt;color:red;font-family:'Courier New';">setAttribute(String, object)</span></u></strong></code><strong><u><span><br />
</span></u></strong><span><font face="Times New Roman">Sets the object to the named attribute. attribute created if it doesn’t exist. </font></span><strong><u><span style="font-size:10pt;color:red;font-family:'Courier New';">removeAttribute(String)</span></u></strong><span><br />
<font face="Times New Roman">Removes the object bound with the specified name from this session.</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/06/26-explain-jsp-session-implicit-object/#respond" title="Comment on 26. Explain JSP Session implicit Object."><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 6th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/06/25-what-is-session-tracking/"><font face="Times New Roman">25. What is Session Tracking.</font></a></span></h3>
<p><span><font face="Times New Roman">There are a number of problems that arise from the fact that HTTP is a “stateless” protocol. In particular, when you are doing on-line shopping, it is a real annoyance that the Web server can’t easily remember previous transactions. This makes applications like shopping carts very problematic: when you add an entry to your cart, how does the server know what’s already in your cart? Even if servers did retain contextual information, you’d still have problems with e-commerce. When you move from the page where you specify what you want to buy (hosted on the regular Web server) to the page that takes your credit card number and shipping address (hosted on the secure server that uses SSL), how does the server remember what you were buying? </font></span><font face="Times New Roman"><strong><span style="color:blue;">There are three typical solutions to this problem. </span></strong><span></span></font></p>
<ol>
<li class="MsoNormal"><font face="Times New Roman"><strong><u><span style="color:red;">Cookies.</span></u></strong><span> You can use HTTP cookies to store information about a shopping session, and each subsequent connection can look up the current session and then extract information about that session from some location on the server machine. This is an excellent alternative, and is the most widely used approach. However, even though servlets have a high-level and easy-to-use interface to cookies, there are still a number of relatively tedious details that need to be handled: </span></font>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">Extracting the cookie that stores the session identifier from the other cookies (there may be many, after all), </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Setting an appropriate expiration time for the cookie (sessions interrupted by 24 hours probably should be reset), and </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Associating information on the server with the session identifier (there may be far too much information to actually store it in the cookie, plus sensitive data like credit card numbers should <em>never</em> go in cookies). </font></span></li>
</ul>
</li>
<li class="MsoNormal"><font face="Times New Roman"><strong><span style="color:red;">URL Rewriting.</span></strong><span> You can append some extra data on the end of each URL that identifies the session, and the server can associate that session identifier with data it has stored about that session. This is also an excellent solution, and even has the advantage that it works with browsers that don’t support cookies or where the user has disabled cookies. However, it has most of the same problems as cookies, namely that the server-side program has a lot of straightforward but tedious processing to do. In addition, you have to be very careful that every URL returned to the user (even via indirect means like </span></font><code><span style="font-size:10pt;font-family:'Courier New';">Location</span></code><span><font face="Times New Roman"> fields in server redirects) has the extra information appended. And, if the user leaves the session and comes back via a bookmark or link, the session information can be lost. </font></span></li>
<li class="MsoNormal"><font face="Times New Roman"><strong><span style="color:red;">Hidden form fields.</span></strong><span> HTML forms have an entry that looks like the following: </span></font><code><span style="font-size:10pt;font-family:'Courier New';">&lt;INPUT TYPE="HIDDEN" NAME="session" VALUE="..."&gt;</span></code><span><font face="Times New Roman">. This means that, when the form is submitted, the specified name and value are included in the </font></span><code><span style="font-size:10pt;font-family:'Courier New';">GET</span></code><span><font face="Times New Roman"> or </font></span><code><span style="font-size:10pt;font-family:'Courier New';">POST</span></code><span><font face="Times New Roman"> data. This can be used to store information about the session. However, it has the major disadvantage that it only works if every page is dynamically generated, since the whole point is that each session has a unique identifier. </font></span></li>
</ol>
<p><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/06/25-what-is-session-tracking/#respond" title="Comment on 25. What is Session Tracking."><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 6th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/06/24-how-can-jsp-inheritance-be-achieved/"><font face="Times New Roman">24. How can JSP inheritance be achieved</font></a></span></h3>
<p><span><font face="Times New Roman">The JSP super class and sub-class need to satisfy certain requirements as defined in the sections below. First let us assume we are using the HTTP protocol. Then mapping of other protocols is an easy task. </font></span><font face="Times New Roman"><strong><u><span style="color:red;">Requirements for the Super Class</span></u></strong><strong><span> </span></strong></font></p>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">It must implement the HttpJspPage interface. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">It will obviously extend the HttpServlet class. It needs to implement the three main lifecycle methods of the servlet: </font></span>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">init </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">destroy </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">service </font></span></li>
</ul>
</li>
</ul>
<p><span><font face="Times New Roman">These implementations are ‘final’. In addition, the method getServletConfig must be implemented to return the ServletConfig object, passed onto the servlet during initialization. </font></span><span><font face="Times New Roman">This super class now necessitates the base class to actually implement the JSP version of these three function calls: jspInit, jspDestroy, and _jspService, by providing abstract declarations for the same </font></span></p>
<ul>
<li> 
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">public abstract void jspInit() </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">public abstract void jspDestroy() </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">The signature for the _jspService method depends on the kind of JSP Engine that has been deployed. For eg: if Weblogic is the JSP Engine, the signature is: </font></span></li>
</ul>
</li>
</ul>
<p><span><font face="Times New Roman">public abstract void _jspService(HttpServletRequest request, HttpServletResponse response); </font></span><span><font face="Times New Roman">The init method is called jspInit, the destroy method &#8211; jspDestroy, and the service method as _jspService. </font></span><font face="Times New Roman"><strong><u><span style="color:red;">Requirements for the sub class</span></u></strong><strong><span></span></strong></font><span><font face="Times New Roman">The sub class may or may not provide the jspInit and jspDestroy method, based on whether the parent class provides it. </font></span><strong><span><font face="Times New Roman">Code snippet for the JSP sub class </font></span></strong><span><font face="Times New Roman">&lt;%@ page extends=”JSPPageSuper”%&gt; </font></span><span><font face="Times New Roman">&lt;% </font></span><span><font face="Times New Roman">//Any server processing </font></span><span><font face="Times New Roman">%&gt; </font></span><span><font face="Times New Roman">&lt;HTML&gt; </font></span><span><font face="Times New Roman">&lt;/HTML&gt; </font></span><span><font face="Times New Roman">Note the first line where the page extends the JSPPageSuper servlet.</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/06/24-how-can-jsp-inheritance-be-achieved/#respond" title="Comment on 24. How can JSP inheritance be achieved"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 6th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/06/23-explain-about-jsp-standard-action/"><font face="Times New Roman">23. Explain about JSP Standard Action</font></a></span></h3>
<p><strong><span><font face="Times New Roman">* </font></span></strong><span><font face="Times New Roman">The use of JavaBeans within JSP pages is a system for accessing Java components through the standard interface of methods with set and get prefixes.<br />
<strong>* </strong>The general syntax for the useBean tag is as follows.<br />
&lt;jsp:useBean id=”myBean” type=”classtype” class=”com.domain.ClassName” scope=”session” /&gt;</font></span><span><font face="Times New Roman">   #  The it attribute id mandatory and can be an arbitrary string, but each can only appear once within a page (i.e. You cannot have two beans with the same id within a page).<br />
   #  The actual class must be stored under the WEB-INF\classes directory<br />
   #  The scope attribute can be page,request,session, or application.<br />
   #  The scope attribute is optional and if it is omitted the default is page scope.</font></span><font face="Times New Roman"><strong><span>Example:</span></strong><span></span></font><font face="Times New Roman"><strong><span># Java Bean Code</span></strong><span></span></font><span><font face="Times New Roman">package com.wordpress.sharat;<br />
public class TestJSPUseBean {<br />
   private String firstName=”sharat”;<br />
   private String lastName=”abcd”;<br />
   public void setFirstName(String firstName) {<br />
      this.firstName = firstName;<br />
   }<br />
   public void setLastName(String lastName) {<br />
      this.lastName = lastName;<br />
   }<br />
   public String getFirstName() {<br />
      return firstName;<br />
   }<br />
   <br />
   public String getLastName() {<br />
      return lastName;<br />
   }<br />
}</font></span><font face="Times New Roman"><strong><span># Usage:</span></strong><span></span></font><span><font face="Times New Roman">That JavaBean can be used within a JSP as follows<br />
&lt;jsp:useBean id=”testbean” type=”com.wordpress.sharat.TestJSPUseBean” class=”com.wordpress.sharat.TestJSPUseBean” scope=”session” /&gt;<br />
&lt;html&gt;<br />
&lt;%<br />
   out.print(testbean.getFirstName());<br />
%&gt;<br />
&lt;/html&gt;<br />
<strong># getProperty</strong></font></span><span><font face="Times New Roman">&lt;jsp:getProperty name=”testbean” property=”firstName”/&gt;</font></span><span><font face="Times New Roman">name and property attributes are mandatory.<br />
<strong># setProperty</strong></font></span><span><font face="Times New Roman">&lt;jsp:setProperty name=”testbean” property=”firstName” value=”Donkey”/&gt;</font></span><span><font face="Times New Roman"># This allows values of a JavaBean to be set at runtime.<br />
# Param Attribute:<br />
  It is a very common requirement to want to set a property according to a runtime query parameter.<br />
  for example with the URL query string<br />
           ?firstname=marcus&amp;lastname=green</font></span><span><font face="Times New Roman">  &lt;jsp:setProperty name=”testbean” property=”firstName” param=”firstname” /&gt;<br />
  &lt;jsp:setProperty name=”testbean” property=”lastName”  param=”lastname” /&gt;</font></span><span><font face="Times New Roman"># Default Params:<br />
  &lt;jsp:setProperty name=”person” property=”*”   /&gt;<br />
  then It will correctly map the firstName and lastName parameters to the “testbean” bean.</font></span><span><font face="Times New Roman"># type attribute in bean defication is optional. We can have a bean defication with type attribute and with out a “class” attribute.</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/06/23-explain-about-jsp-standard-action/#respond" title="Comment on 23. Explain about JSP Standard Action"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 6th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/05/22-discuss-the-scope-of-a-variable-declared-in-declaration-part-and-in-scriptlet-part/"><font face="Times New Roman">22. Discuss the scope of a variable declared in Declaration part and in scriptlet part.</font></a></span></h3>
<p><font face="Times New Roman"><strong><span style="font-size:10pt;">Variable declared inside declaration part</span></strong><span style="font-size:10pt;"> is treated as a global variable.that means after convertion jsp file into servlet </span><span></span></font></p>
<ul>
<li class="MsoNormal"><font face="Times New Roman"><span style="font-size:10pt;">that variable will be in outside of service method or </span><span></span></font></li>
<li class="MsoNormal"><font face="Times New Roman"><span style="font-size:10pt;">it will be declared as instance variable.</span><span> </span></font></li>
<li class="MsoNormal"><font face="Times New Roman"><span style="font-size:10pt;">the scope is available to complete jsp and to complete in the converted servlet class.</span><span> </span></font></li>
</ul>
<p><font face="Times New Roman"><strong><span style="font-size:10pt;">Variable declared inside a scriplet</span></strong><span style="font-size:10pt;"> that variable will be declared inside a service method and the scope is with in the service method. </span><span></span></font><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/05/22-discuss-the-scope-of-a-variable-declared-in-declaration-part-and-in-scriptlet-part/#respond" title="Comment on 22. Discuss the scope of a variable declared in Declaration part and in scriptlet part."><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 5th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/05/21-how-can-i-enable-session-tracking-for-jsp-pages-if-the-browser-has-disabled-cookies/"><font face="Times New Roman">21. How can I enable session tracking for JSP pages if the browser has disabled cookies?</font></a></span></h3>
<p><font face="Times New Roman"><span style="font-size:10pt;">Session tracking uses cookies by default to associate a session identifier with a unique user. If the browser does not support cookies, or if cookies are disabled, you can still enable session tracking using URL rewriting. </span><span>URL rewriting essentially includes the session ID within the link itself as a name/value pair. However, for this to be effective, you need to append the session ID for each and every link that is part of your servlet response.</span></font><span><font face="Times New Roman">Adding the session ID to a link is greatly simplified by means of of a couple of methods: response.encodeURL() associates a session ID with a given URL, and if you are using redirection, response.encodeRedirectURL() can be used by giving the redirected URL as input.</font></span><span><font face="Times New Roman">Both encodeURL() and encodeRedirectedURL() first determine whether cookies are supported by the browser; if so, the input URL is returned unchanged since the session ID will be persisted as a cookie.</font></span><span><font face="Times New Roman">Consider the following example, in which two JSP files, say hello1.jsp and hello2.jsp, interact with each other. Basically, we create a new session within hello1.jsp and place an object within this session. The user can then traverse to hello2.jsp by clicking on the link present within the page.Within hello2.jsp, we simply extract the object that was earlier placed in the session and display its contents. Notice that we invoke the encodeURL() within hello1.jsp on the link used to invoke hello2.jsp; if cookies are disabled, the session ID is automatically appended to the URL, allowing hello2.jsp to still retrieve the session object.</font></span><span><font face="Times New Roman">Try this example first with cookies enabled. Then disable cookie support, restart the brower, and try again. Each time you should see the maintenance of the session across pages.</font></span><span><font face="Times New Roman">Do note that to get this example to work with cookies disabled at the browser, your JSP engine has to support URL rewriting.</font></span><span><font face="Times New Roman">hello1.jsp</font></span></p>
<pre style="margin:0 0.5in;"><span><font size="2">&lt;%@ page session="true" %&gt; &lt;%<span>   </span>Integer num = new Integer(100);<span>   </span>session.putValue("num",num);<span>  </span>String url =response.encodeURL("hello2.jsp"); %&gt; &lt;a href='&lt;%=url%&gt;'&gt;hello2.jsp&lt;/a&gt;</font></span></pre>
<p><span><font face="Times New Roman">hello2.jsp</font></span></p>
<pre style="margin:0 0.5in;"><span><font size="2">&lt;%@ page session="true" %&gt; &lt;%<span>   </span>Integer i= (Integer )session.getValue("num");<span>   </span>out.println("Num value in session is "+i.intValue()); %&gt;</font></span></pre>
<p><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/05/21-how-can-i-enable-session-tracking-for-jsp-pages-if-the-browser-has-disabled-cookies/#respond" title="Comment on 21. How can I enable session tracking for JSP pages if the browser has disabled cookies? "><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>September 5th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/05/20-how-does-jsp-handle-run-time-exceptions/"><font face="Times New Roman">20. How does JSP handle run-time exceptions</font></a></span></h3>
<p><font face="Times New Roman"><span style="font-size:10pt;">You can use the <em>errorPage</em> attribute of the <em>page</em> directive to have uncaught run-time exceptions automatically forwarded to an error processing page. For example: </span><span></span></font></p>
<pre><font size="2"><strong><span>&lt;%@ page errorPage=”error.jsp” %&gt;</span></strong><span></span></font></pre>
<p><span><font face="Times New Roman">redirects the browser to the JSP page error.jsp if an uncaught exception is encountered during request processing.</font></span><span><font face="Times New Roman">Within error.jsp, if you indicate that it is an error-processing page, via the directive:</font></span></p>
<pre><font size="2"><strong><span>&lt;%@ page isErrorPage=”true” %&gt;</span></strong><span></span></font></pre>
<p><span><font face="Times New Roman">the Throwable object describing the exception may be accessed within the error page via the <em>exception</em> implicit object.</font></span><font face="Times New Roman"><strong><span>Note:</span></strong><span> You must always use a relative URL as the value for the <em>errorPage</em> attribute.</span></font><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/05/20-how-does-jsp-handle-run-time-exceptions/#comments" title="Comment on 20. How does JSP handle run-time exceptions"><font face="Times New Roman">1 comment</font></a><font face="Times New Roman"> <em>September 5th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/09/05/19-explain-the-life-cycle-of-a-jsp/"><font face="Times New Roman">19. Explain the life cycle of a JSP.</font></a></span></h3>
<p><span><font face="Times New Roman">A JSP page services requests as a servlet. Thus, the life cycle and many of the capabilities of JSP pages (in particular the dynamic aspects) are determined by Java Servlet technology.</font></span></p>
<ol>
<li class="MsoNormal"><span><font face="Times New Roman">JSP page translation </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">JSP page compilation </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">load class </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">create instance </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">call the jspInit method </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">call the _jspService method </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">call the jspDestroy method </font></span></li>
</ol>
<p><span><font face="Times New Roman">             When a request is mapped to a JSP page, the web container first checks whether the JSP page’s servlet is older than the JSP page. If the servlet is older, the web container translates the JSP page into a servlet class and compiles the class. During development, one of the advantages of JSP pages over servlets is that the build process is performed automatically.</font></span><span><font face="Times New Roman">            The translation involves bringing in any included files, skipping any jsp comments and leaving in any of the HTML comments. The source is then compiled using the same javac compiler that you would use to compile any other Java program. Of course the result of many of the attempts at compilation will be compilation errors that require modifications to the JSP source. If a JSP is configured with &lt;load-on-startup&gt; in the deployment descriptor, the container will attempt to compile when it starts rather than waiting for the first request.</font></span><span><font face="Times New Roman">             After the page has been translated and compiled, the JSP page’s servlet (for the most part) follows the servlet life cycle.</font></span></p>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">If an instance of the JSP page’s servlet does not exist, the container<br />
Loads the JSP page’s servlet class </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Instantiates an instance of the servlet class </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Initializes the servlet instance by calling the jspInit method </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">The container invokes the _jspService method, passing request and response objects. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">If the container needs to remove the JSP page’s servlet, it calls the jspDestroy method. </font></span></li>
</ul>
<p><font face="Times New Roman"><strong><span>Initialization:</span></strong><span> The jspInit method is called by the container once and only once for a servlet instance. As you might guess from its name it is used to do initial once only setup such as getting resources and initialising variables that are used in the JSP page. The jspInit method can be overridden within a JSP page with code like the following.</span></font><span><font face="Times New Roman">&lt;%!<br />
public void jspInit(){<br />
/*getServletConfig is inherited */<br />
ServletConfig config= getServletConfig();<br />
}<br />
%&gt;</font></span><strong><span><font face="Times New Roman">Execution:</font></span></strong><span><font face="Times New Roman"> The _jspService method is the equivalent of the service() method for a servlet. The _jspService method is called for each request, taking the request and response objects as parameters. The scriptlets, html elements and expressions are put into this method as part of translation. The _jspService method makes the implicit objects available to the JSP page.The _jspService method cannot be overridden within a JSP page. <br />
<strong>Cleanup:</strong> The jspDestroy method is guaranteed to to be the last method called by the container before an instance of a JSP page goes out of service. The jspDestroy method is typically used to clean up or release resources acquired by the jspInit method. The jspDestory method is a little like the finalize method in a plain old Java class, in that you cannot be certain exactly when it will run. Because of this be aware that it may not be the most efficient way of cleaning up precious resources.</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/09/05/19-explain-the-life-cycle-of-a-jsp/#comments" title="Comment on 19. Explain the life cycle of a JSP."><font face="Times New Roman">1 comment</font></a><font face="Times New Roman"> <em>September 5th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/08/29/18-how-to-overwrite-the-init-and-destroy-method-in-a-jsp-page/"><font face="Times New Roman">18. How to overwrite the init and destroy method in a jsp page</font></a></span></h3>
<p><font face="Times New Roman"><strong><span style="font-size:7.5pt;">You cannot override the _jspService() method within a JSP page. You can however, override the jspInit() and jspDestroy() methods within a JSP page. jspInit() can be useful for allocating resources like </span></strong><span class="klink"><span style="font-size:7.5pt;color:blue;">database</span></span><strong><span style="font-size:7.5pt;"> connections, network connections, and so forth for the JSP page. It is good programming practice to free any allocated resources within jspDestroy().The jspInit() and jspDestroy() methods are each executed just once during the lifecycle of a JSP page and are typically declared as JSP declarations:&lt;%!</span></strong></font><strong><span style="font-size:7.5pt;"><br />
<font face="Times New Roman"><strong>public void jspInit() {</strong><br />
<strong>. . .</strong><br />
<strong>}</strong><br />
<strong>%&gt;</strong><br />
<strong>&lt;%!</strong><br />
<strong>public void jspDestroy() {</strong><br />
<strong>. . .</strong><br />
<strong>}</strong><br />
<strong>%&gt;</strong></font></span></strong><strong><span style="font-size:7.5pt;"></span></strong><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/08/29/18-how-to-overwrite-the-init-and-destroy-method-in-a-jsp-page/#comments" title="Comment on 18.  How to overwrite the init and destroy method in a jsp page"><font face="Times New Roman">1 comment</font></a><font face="Times New Roman"> <em>August 29th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/08/29/17-what-are-servlet-and-jsp-lifecycle-methods/"><font face="Times New Roman">17. What are Servlet and JSP lifecycle methods.</font></a></span></h3>
<p><font face="Times New Roman"><strong><u><span>servlet life cycle methods:</span></u></strong><span></span></font><span><font face="Times New Roman">init();</font></span><span><font face="Times New Roman">service();</font></span><span><font face="Times New Roman">destroy();</font></span><font face="Times New Roman"><strong><u><span>jsp lifecycle methods:</span></u></strong><span></span></font><span><font face="Times New Roman">init();</font></span><span><font face="Times New Roman">_service();</font></span><span><font face="Times New Roman">destroy();</font></span><span><font face="Times New Roman">remember starting any method with _ means we cannot override this method.</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/08/29/17-what-are-servlet-and-jsp-lifecycle-methods/#comments" title="Comment on 17. What are Servlet and JSP lifecycle methods."><font face="Times New Roman">1 comment</font></a><font face="Times New Roman"> <em>August 29th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/08/29/16what-is-the-difference-between-sendredirectand-forwardin-jsp/"><font face="Times New Roman">16.What is the difference between sendRedirect()and forward()in JSP?</font></a></span></h3>
<p><span><font face="Times New Roman">In an HttpServletResponse class:</font></span><span><font face="Times New Roman">Response.sendRedirect () This function is used, when we want to redirect<br />
the client request to some other site (i.e out of our context) or<br />
when ever we want to redirect errors. If you are using sendRedirect (), then it will be visible to the client that means the URL which you have been redirected will be visible in the address bar. Redirect response to the client using the specified redirect location URL.</font></span><span><font face="Times New Roman">RequestDispatcher Interface:<br />
Forward() : This can be done in two ways by Request &amp; ServeletContext.<br />
Forwarding a request from a servlet to another resource (servlet, JSP<br />
file, or HTML file) on the server. Forward is done at server side, without the client’s knowledge.</font></span><span><font face="Times New Roman">When you invoke a forward request, the request is sent to another<br />
resource on the server, without the client being informed that a<br />
different resource is going to process the request. This process occurs<br />
completely with in the web container. When a sendRedirtect method is<br />
invoked, it causes the web container to return to the browser indicating<br />
that a new URL should be requested. Because the browser issues a<br />
completely new request any object that are stored as request attributes<br />
before the redirect occurs will be lost. This extra round trip a<br />
redirect is slower than forward.</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/08/29/16what-is-the-difference-between-sendredirectand-forwardin-jsp/#comments" title="Comment on 16.What is the difference between sendRedirect()and forward()in JSP?"><font face="Times New Roman">2 comments</font></a><font face="Times New Roman"> <em>August 29th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/08/29/15-what-is-the-difference-between-difference-between-doget-and-dopost/"><font face="Times New Roman">15. What is the difference between Difference between doGet() and doPost()</font></a></span></h3>
<table border="1" cellPadding="0" cellSpacing="0">
<tr>
<td width="295" vAlign="top" style="width:221.25pt;background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><strong><font size="3" face="Times New Roman">DoGet</font></strong></p>
</td>
<td width="295" vAlign="top" style="width:221.25pt;background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><strong><font size="3" face="Times New Roman">DoPost</font></strong></p>
</td>
</tr>
<tr>
<td width="295" vAlign="top" style="width:221.25pt;background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">In doGet Method the parameters are appended to the URL and sent along with header information</font></p>
</td>
<td width="295" vAlign="top" style="width:221.25pt;background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">In doPost, parameters are sent in separate line in the body</font></p>
</td>
</tr>
<tr>
<td width="295" vAlign="top" style="width:221.25pt;background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Maximum size of data that can be sent using doget is 240 bytes</font></p>
</td>
<td width="295" vAlign="top" style="width:221.25pt;background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">There is no maximum size for data</font></p>
</td>
</tr>
<tr>
<td width="295" vAlign="top" style="width:221.25pt;background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Parameters are not encrypted</font></p>
</td>
<td width="295" vAlign="top" style="width:221.25pt;background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Parameters are encrypted</font></p>
</td>
</tr>
<tr>
<td width="295" vAlign="top" style="width:221.25pt;background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">DoGet method generally is used to query or to get some information from the server</font></p>
</td>
<td width="295" vAlign="top" style="width:221.25pt;background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Dopost is generally used to update or post some information to the server</font></p>
</td>
</tr>
<tr>
<td width="295" vAlign="top" style="width:221.25pt;background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">DoGet is faster if we set the response content length since the same connection is used. Thus increasing the performance</font></p>
</td>
<td width="295" vAlign="top" style="width:221.25pt;background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">DoPost is slower compared to doGet since doPost does not write the content length</font></p>
</td>
</tr>
<tr>
<td width="295" vAlign="top" style="width:221.25pt;background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">DoGet should be idempotent. i.e. doget should be able to be repeated safely many times </font></p>
</td>
<td width="295" vAlign="top" style="width:221.25pt;background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">This method does not need to be idempotent. Operations requested through POST can have side effects for which the user can be held accountable, for example, updating stored data or buying items online. </font></p>
</td>
</tr>
<tr>
<td width="295" vAlign="top" style="width:221.25pt;background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">DoGet should be safe without any side effects for which user is held responsible</font></p>
</td>
<td width="295" vAlign="top" style="width:221.25pt;background-color:transparent;border:#ece9d8;padding:0;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">This method does not need to be either safe</font></p>
</td>
</tr>
</table>
<p><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/08/29/15-what-is-the-difference-between-difference-between-doget-and-dopost/#comments" title="Comment on 15. What is the difference between Difference between doGet() and doPost()"><font face="Times New Roman">6 comments</font></a><font face="Times New Roman"> <em>August 29th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/08/29/14-explain-jspservle-threading-model/"><font face="Times New Roman">14. Explain JSP/Servle threading model.</font></a></span></h3>
<p><span style="font-size:10pt;"><font face="Times New Roman">Every Servlet contains a doGet or doPost method.<br />
When we deploy a servlet the init method of the servlet is called. And after it has been deployed on each request of the servlet a separate thread is created to process the doGet/doPost method of the servlet.<br />
So all the instance variables of the servlet are not thread safe.<br />
We can override this behaviour by implementing SingleThreadedModel in the servlet which tells the servlet container to create a new Servlet instance for each client’s request instead of using a single Servlet Instance.</font></span><span><br />
<font face="Times New Roman">But single threaded model is deprecated now in Servlet 2.4 API. API doc says ” Note that <strong>SingleThreadModel does not solve all thread safety issues</strong>. For example, session attributes and static variables can still be accessed by multiple requests on multiple threads at the same time, even when SingleThreadModel servlets are used. It is recommended <strong>that a developer take other means to resolve those issues</strong> instead of implementing this interface, such as avoiding the usage of an instance variable or synchronizing the block of the code accessing those resources. “</font></span><span><font face="Times New Roman">this might be the reason why it has been deprecated.</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/08/29/14-explain-jspservle-threading-model/#respond" title="Comment on 14. Explain JSP/Servle threading model."><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>August 29th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/08/29/13-what-is-the-default-scope-of-jsp-tags/"><font face="Times New Roman">13. what is the default scope of jsp tags?</font></a></span></h3>
<p><span><font face="Times New Roman">Page <img src='http://s2.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> -</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/08/29/13-what-is-the-default-scope-of-jsp-tags/#comments" title="Comment on 13. what is the default scope of jsp tags?"><font face="Times New Roman">1 comment</font></a><font face="Times New Roman"> <em>August 29th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/08/29/12-what-are-the-default-objects-provided-by-jsp-container/"><font face="Times New Roman">12. What are the default objects provided by JSP container?</font></a></span></h3>
<p><span><font face="Times New Roman">Implicit objects let developers access container-provided services and resources. These objects are defined as <em>implicit</em> because you do not have to explicitly declare them. They are defined in every JSP page and used behind the scenes by the container whether you declare them or not — although you cannot <em>redeclare</em> them. Because implicit objects are declared automatically, we need only use the reference variable associated with a given object to begin calling methods on it.</font></span><span><font face="Times New Roman">The nine implicit objects, with a brief description of each one’s function, are:</font></span></p>
<ul>
<li class="MsoNormal"><code><span style="font-size:10pt;font-family:'Courier New';">Application</span></code><span><font face="Times New Roman"> is the broadest context state available. It allows the JSP page’s servlet and any Web components contained in the same application to share information. </font></span></li>
<li class="MsoNormal"><code><span style="font-size:10pt;font-family:'Courier New';">Config</span></code><span><font face="Times New Roman"> allows initialization data to be passed to a JSP page’s servlet. </font></span></li>
<li class="MsoNormal"><code><span style="font-size:10pt;font-family:'Courier New';">Exception</span></code><span><font face="Times New Roman"> houses exception data to be accessed only by designated JSP “error pages.” </font></span></li>
<li class="MsoNormal"><code><span style="font-size:10pt;font-family:'Courier New';">Out</span></code><span><font face="Times New Roman"> provides access to the servlet’s output stream. </font></span></li>
<li class="MsoNormal"><code><span style="font-size:10pt;font-family:'Courier New';">Page</span></code><span><font face="Times New Roman"> is the instance of the JSP page’s servlet processing the current request. Not typically used by JSP page authors. </font></span></li>
<li class="MsoNormal"><code><span style="font-size:10pt;font-family:'Courier New';">PageContext</span></code><span><font face="Times New Roman"> is the context for the JSP page itself. It provides a single API to manage the various scoped attributes. This API is used extensively when implementing JSP custom tag handlers. </font></span></li>
<li class="MsoNormal"><code><span style="font-size:10pt;font-family:'Courier New';">Request</span></code><span><font face="Times New Roman"> provides access to HTTP request data, as well as providing a context for associating request-specific data. </font></span></li>
<li class="MsoNormal"><code><span style="font-size:10pt;font-family:'Courier New';">Response</span></code><span><font face="Times New Roman"> enables direct access to the </font></span><code><span style="font-size:10pt;font-family:'Courier New';">HTTPServletResponse</span></code><span><font face="Times New Roman"> object and is rarely used by JSP authors. </font></span></li>
<li class="MsoNormal"><code><span style="font-size:10pt;font-family:'Courier New';">Session</span></code><span><font face="Times New Roman"> is perhaps the most commonly used of the state management contexts. The concept of a “session” is that of a single user interacting with a Web application over several requests. </font></span></li>
</ul>
<p><span><font face="Times New Roman">Although some implicit objects address a single function, several of them provide multiple categories of functionality. In the sections that follow, we’ll review implicit objects according to functional categories:</font></span></p>
<ul>
<li class="MsoNormal"><font face="Times New Roman"><strong><span>Session management</span></strong><span>: </span></font><code><span style="font-size:10pt;font-family:'Courier New';">application</span></code><span><font face="Times New Roman">, </font></span><code><span style="font-size:10pt;font-family:'Courier New';">session</span></code><span><font face="Times New Roman">, </font></span><code><span style="font-size:10pt;font-family:'Courier New';">request</span></code><span><font face="Times New Roman">, </font></span><code><span style="font-size:10pt;font-family:'Courier New';">pageContext</span></code><span><font face="Times New Roman"> </font></span></li>
<li class="MsoNormal"><font face="Times New Roman"><strong><span>Flow control</span></strong><span>: </span></font><code><span style="font-size:10pt;font-family:'Courier New';">application</span></code><span><font face="Times New Roman">, </font></span><code><span style="font-size:10pt;font-family:'Courier New';">config</span></code><span><font face="Times New Roman">, </font></span><code><span style="font-size:10pt;font-family:'Courier New';">pageContext</span></code><span><font face="Times New Roman">, </font></span><code><span style="font-size:10pt;font-family:'Courier New';">request</span></code><span><font face="Times New Roman">, </font></span><code><span style="font-size:10pt;font-family:'Courier New';">session</span></code><span><font face="Times New Roman"> </font></span></li>
<li class="MsoNormal"><font face="Times New Roman"><strong><span>Logging and exceptions</span></strong><span>: </span></font><code><span style="font-size:10pt;font-family:'Courier New';">application</span></code><span><font face="Times New Roman">, </font></span><code><span style="font-size:10pt;font-family:'Courier New';">config</span></code><span><font face="Times New Roman">, </font></span><code><span style="font-size:10pt;font-family:'Courier New';">exception</span></code><span><font face="Times New Roman">, </font></span><code><span style="font-size:10pt;font-family:'Courier New';">pageContext</span></code><span><font face="Times New Roman">, </font></span><code><span style="font-size:10pt;font-family:'Courier New';">request</span></code><span><font face="Times New Roman">, </font></span><code><span style="font-size:10pt;font-family:'Courier New';">session</span></code><span><font face="Times New Roman"> </font></span></li>
<li class="MsoNormal"><font face="Times New Roman"><strong><span>Input/output control</span></strong><span>: </span></font><code><span style="font-size:10pt;font-family:'Courier New';">request</span></code><span><font face="Times New Roman">, </font></span><code><span style="font-size:10pt;font-family:'Courier New';">response</span></code><span><font face="Times New Roman">, </font></span><code><span style="font-size:10pt;font-family:'Courier New';">out</span></code><span><font face="Times New Roman"> </font></span></li>
<li class="MsoNormal"><font face="Times New Roman"><strong><span>Initialization parameters</span></strong><span>: </span></font><code><span style="font-size:10pt;font-family:'Courier New';">config</span></code><span><font face="Times New Roman"> </font></span></li>
</ul>
<p><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/08/29/12-what-are-the-default-objects-provided-by-jsp-container/#respond" title="Comment on 12. What are the default objects provided by JSP container?"><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>August 29th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/08/29/11what-object-that-provides-one-point-access-to-page-request-response-session-out-exception-config-and-application/"><font face="Times New Roman">11.What object that provides one point access to page, request, response, session, out, exception, config and application</font></a></span></h3>
<p><span><font face="Times New Roman">A PageContext instance provides access to all the namespaces associated with a JSP page, provides access to several page attributes, as well as a layer above the implementation details. Implicit objects are added to the pageContext automatically.</font></span><span><font face="Times New Roman">The </font></span><code><span style="font-size:10pt;font-family:'Courier New';">PageContext </span></code><span><font face="Times New Roman">class is an abstract class, designed to be extended to provide implementation dependent implementations thereof, by conformant JSP engine runtime environments. A PageContext instance is obtained by a JSP implementation class by calling the JspFactory.getPageContext() method, and is released by calling JspFactory.releasePageContext().</font></span></p>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">a single API to manage the various scoped namespaces </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">a number of convenience API’s to access various public objects </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">a mechanism to obtain the JspWriter for output </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">a mechanism to manage session usage by the page </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">a mechanism to expose page directive attributes to the scripting environment </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">mechanisms to forward or include the current request to other active components in the application </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">a mechanism to handle errorpage exception processing </font></span></li>
</ul>
<p><span><font face="Times New Roman">The following methods provide <strong>convenient access</strong> to implicit objects: </font></span><code><span style="font-size:10pt;font-family:'Courier New';">getException()</span></code><span><font face="Times New Roman">, </font></span><code><span style="font-size:10pt;font-family:'Courier New';">getPage()</span></code><span><font face="Times New Roman"> </font></span><code><span style="font-size:10pt;font-family:'Courier New';">getRequest()</span></code><span><font face="Times New Roman">, </font></span><code><span style="font-size:10pt;font-family:'Courier New';">getResponse()</span></code><span><font face="Times New Roman">, </font></span><code><span style="font-size:10pt;font-family:'Courier New';">getSession()</span></code><span><font face="Times New Roman">, </font></span><code><span style="font-size:10pt;font-family:'Courier New';">getServletConfig()</span></code><span><font face="Times New Roman"> and </font></span><code><span style="font-size:10pt;font-family:'Courier New';">getServletContext()</span></code><span><font face="Times New Roman">.</font></span><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/08/29/11what-object-that-provides-one-point-access-to-page-request-response-session-out-exception-config-and-application/#respond" title="Comment on 11.What object that provides one point access to page, reque "><font face="Times New Roman">Add comment</font></a><font face="Times New Roman"> <em>August 29th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/08/28/10-what-is-the-difference-between-pagedirective-include-action-tag-include/"><font face="Times New Roman">10. what is the difference between , pagedirective include, action tag include ?</font></a></span></h3>
<p><span><font face="Times New Roman">A JSP page can include page fragments from other files to form the complete response. You can use this, for instance, to keep header, footer, and navigation bar content in separate files and include them in all other pages. There are two include mechanisms: the include <em>directive</em> and the include <em>action</em>. It’s not always obvious which one to use, though.</font></span><span><font face="Times New Roman">The include directive, </font></span><tt><span style="font-size:10pt;font-family:'Courier New';">&lt;%@ include file=”filename.inc” %&gt;</span></tt><span><font face="Times New Roman">, includes the <em>content</em> of the specified file during the translation phase–when the page is converted to a servlet. The main page and the included file are simply merged. This means that scripting variables declared in one file (using scripting elements or an action element like </font></span><tt><span style="font-size:10pt;font-family:'Courier New';">&lt;jsp:useBean&gt;</span></tt><span><font face="Times New Roman">) are visible in all files and must have unique names. Some containers detect changes in files included with the directive, but the specification doesn’t require it. Therefore, changes you make to the included file in a running system may not be reflected immediately; you may have to update the main JSP page, or remove the class file generated for the main page in order to see the change.</font></span><span><font face="Times New Roman">The include action, </font></span><tt><span style="font-size:10pt;font-family:'Courier New';">&lt;jsp:include page=”pagename.jsp” flush=”true” /&gt;</span></tt><span><font face="Times New Roman">, includes the <em>response</em> generated by executing the specified page (a JSP page or a servlet) during the request processing phase–when the page is requested by a user. As opposed to the include directive, the page name can be specified as a so-called <em>request-time attribute</em> value, so which page to include can be decided when the main page is requested. Since it’s the response generated by the page that is included, <em>not</em> the content of the page itself, scripting variables declared in one file are not available to the other files. To share an object between the pages you must instead place it in one of the following JSP scopes: request, session or application scope. If you change a page included with the include action, the change always takes effect immediately.</font></span><span><font face="Times New Roman">My rule of thumb for when to use the different mechanisms is this:</font></span></p>
<ul>
<li class="MsoNormal"><span><font face="Times New Roman">Use the include <em>directive</em> if the file changes rarely. It’s the fastest mechanism. If your container doesn’t automatically detect changes, you can force the changes to take effect by deleting the main page class file. </font></span></li>
<li class="MsoNormal"><span><font face="Times New Roman">Use the include <em>action</em> only for content that changes often, and if which page to include cannot be decided until the main page is requested. </font></span></li>
</ul>
<p><span><font face="Times New Roman"> </font></span><span><a href="http://sharat.wordpress.com/2006/08/28/10-what-is-the-difference-between-pagedirective-include-action-tag-include/#comments" title="Comment on 10. what is the difference between , pagedirective include, action tag include ?"><font face="Times New Roman">4 comments</font></a><font face="Times New Roman"> <em>August 28th, 2006</em> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/08/28/9-explain-jsp-import-and-jsp-extends/"><font face="Times New Roman">9. Explain JSP Import and JSP Extends.</font></a></span></h3>
<p><span><font face="Times New Roman">extends=”package.class”</font></span><span><font face="Times New Roman">The fully qualified name of the superclass of the Java class file this JSP file will be compiled to. Use this attribute cautiously, as it can limit the JSP container’s ability to provide a specialized superclass that improves the quality of the compiled file.</font></span><span><font face="Times New Roman">import=”{package.class | package.* }, …”</font></span><span><font face="Times New Roman"> A comma-separated list of Java packages that the JSP file should import. The packages (and their classes) are available to scriptlets, expressions, and declarations within the JSP file. If you want to import more than one package, you can specify a comma-separated list after </font></span><code><span style="font-size:10pt;font-family:'Courier New';">import</span></code><span><font face="Times New Roman"> or you can use </font></span><code><span style="font-size:10pt;font-family:'Courier New';">import</span></code><span><font face="Times New Roman"> more than once in a JSP file.</font></span><span><font face="Times New Roman"> </font></span><span><font face="Times New Roman"> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/08/28/8-what-is-the-diff-bn-declaring-members-in-the-jsp-and-declaraing-in-jspinit-method/"><font face="Times New Roman">8. What is the diff. b/n declaring members in the JSP and declaraing in jspinit() method ?</font></a></span></h3>
<p><span style="font-family:Arial;">Declaring a variable in JSP means it will have a scope of page while declaring it in jspInit() means its scope gets end with init() method only.</span><span></span><span><font face="Times New Roman"> </font></span><span><font face="Times New Roman"> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/08/28/1-how-do-i-prevent-the-output-of-my-jsp-or-servlet-pages-from-being-cached-by-the-browser/"><font face="Times New Roman">1. How do I prevent the output of my JSP or Servlet pages from being cached by the browser?</font></a></span></h3>
<p><span><font face="Times New Roman">Appropriate HTTP header attributes have to be set to prevent the dynamic content output by the JSP page from being cached by the browser. The following jsp code has to be included at the beginning of your JSP pages to prevent caching :</font></span><span><font face="Times New Roman">Execute the following scriptlet at the beginning of your JSP pages to prevent them from being cached at the browser:</font></span><span><font face="Times New Roman">&lt;%</font></span><span><font face="Times New Roman">response.setHeader(”Pragma”,”no-cache”); //HTTP 1.0<br />
</font></span><span style="font-size:10pt;font-family:Verdana;">response.setHeader(”Cache-Control”,”no-store”); //HTTP 1.1</span><span></span><span style="font-size:10pt;font-family:Verdana;"><br />
%&gt;</span><span></span><span><font face="Times New Roman"> </font></span><span><font face="Times New Roman"> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/08/28/5-can-a-single-jsp-page-be-considered-as-a-j2ee-applicationÂ Â /"><font face="Times New Roman">5. Can a single JSP page be considered as a J2EE application?  </font></a></span></h3>
<p><span><font face="Times New Roman">A J2EE application contains J2EE modules, which could be web applications, EJBs and application clients. It also contains meta-information about the application as well as shared libraries. You can also say that a J2EE application is a set of J2EE modules with some added glue that binds them together into a complete integrated application. The shape of a J2EE application is a single Java Archive file with the .ear filename extension.</font></span><span><font face="Times New Roman">If a single JSP is bundled according to J2EE structure, then we can consider that one as a J2EE application.</font></span><span><font face="Times New Roman"> </font></span><span><font face="Times New Roman"> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/08/27/3how-are-the-implicit-object-made-available-in-the-jsp-file-where-are-they-defined/"><font face="Times New Roman">3.How are the implicit object made available in the jsp file? Where are they defined?</font></a></span></h3>
<p><span><font face="Times New Roman">Implicit objects are a set of Java objects that the JSP Container makes available to developers in each page. These objects may be accessed as built-in variables via scripting elements and can also be accessed programmatically by JavaBeans and Servlets.</font></span><span><font face="Times New Roman">These objects will be automatically instantiated under specific variable names hence the name <em>implicit</em>.</font></span></p>
<table border="0" cellPadding="0" cellSpacing="0">
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;"><strong><font size="3"><font face="Times New Roman">Object</font></font></strong></td>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;"><strong><font size="3"><font face="Times New Roman">Class or Interface</font></font></strong></td>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;"><strong><font size="3"><font face="Times New Roman">Description</font></font></strong></td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;font-family:'Courier New';">page</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;font-family:'Courier New';">jsp.HttpJspPage</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Page’s servlet instance</font></p>
</td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;font-family:'Courier New';">config</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;font-family:'Courier New';">ServletConfig</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Servlet configuration information</font></p>
</td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;font-family:'Courier New';">pageContext</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;font-family:'Courier New';">jsp.pageContext</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Provides access to all the namespaces associated with a JSP page and access to several page attributes</font></p>
</td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;font-family:'Courier New';">request</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;font-family:'Courier New';">http.HttpServletRequest</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Data included with the HTTP Request</font></p>
</td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;font-family:'Courier New';">response</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;font-family:'Courier New';">http.HttpServletResponse</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">HTTP Response data, e.g. cookies</font></p>
</td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;font-family:'Courier New';">out</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;font-family:'Courier New';">jsp.JspWriter</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Output stream for page context</font></p>
</td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;font-family:'Courier New';">session</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;font-family:'Courier New';">http.HttpSession</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">User specific session data</font></p>
</td>
</tr>
<tr>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;font-family:'Courier New';">application</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><code><span style="font-size:10pt;font-family:'Courier New';">ServletContext</span></code></p>
</td>
<td style="background-color:transparent;border:#ece9d8;padding:0.75pt;">
<p style="margin:0;" class="MsoNormal"><font size="3" face="Times New Roman">Data shared by all application pages</font></p>
</td>
</tr>
</table>
<p><span><font face="Times New Roman"> </font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/08/27/2how-to-compile-the-jsp-pages-manually-in-all-applications-and-web-servers/"><font face="Times New Roman">2.How to compile the JSP pages manually in all applications and web servers</font></a></span></h3>
<p><span><font face="Times New Roman">Tomcat uses the /tomcat/bin/jspc.bat file to make java servlets out of<br />
 the JSPs. Run this program without parameters to view the help screen.<br />
 It’s pretty simple and easy to use. You can set up another bat file<br />
 to call it with parameters. Make sure to include the param “-p<br />
 org.apache.jsp” to create all servlets in the standard tomcat package<br />
 (package org.apache.jsp<br />
 <br />
 Once you have servlets, use the java compiler (javac) to compile the<br />
 whole directory of servlets. ie. javac *.java The classpath used by<br />
 the compiler must match the classpath set for Tomcat. You’ll also have<br />
 to include in the classpath any of your custom packages that the<br />
 servlets import.<br />
 <br />
 Be careful about file and directory permissions. The output of all<br />
 this has to be accessible to the user account that Tomcat runs under.</font></span></p>
<h3><span><a href="http://sharat.wordpress.com/2006/08/27/1-can-one-jspservlet-extend-another-servletjsp/"><font face="Times New Roman">1. Can One Jsp/Servlet extend another Servlet/Jsp</font></a></span></h3>
<p><span><font face="Times New Roman">Extending a JSP from another servlet or JSP can be a very tricky task. However it is not recommended by sun, because in doing so you restrict the capability of the JSP Engine.</font></span><span><font face="Times New Roman">The JSP super class and sub-class need to satisfy certain requirements.</font></span><strong><span><font face="Times New Roman">Requirements for the Super Class </font><a href="http://sharat.wordpress.com/2006/08/27/1-can-one-jspservlet-extend-another-servletjsp/#more-4"><font face="Times New Roman">(more…)</font></a></span></strong><strong><span style="font-size:9pt;color:#333333;"><font face="Verdana"> </font></span></strong><strong><span style="font-size:9pt;color:#333333;"><font face="Verdana"> </font></span></strong><strong><span style="font-size:10pt;font-family:Verdana;">What are the differences between HashMap and Hashtable?</span></strong><strong><span style="font-size:10pt;font-family:Verdana;"> </span></strong><span style="font-size:10pt;font-family:Verdana;">Both provide key-value access to data. The Hashtable is one of the original collection classes in Java. HashMap is part of the new Collections Framework, added with Java 2, v1.2. </span><span style="font-size:10pt;font-family:Verdana;">The key difference between the two is that access to the Hashtable is synchronized on the table while access to the HashMap isn&#8217;t. You can add it, but it isn&#8217;t there by default. </span><span style="font-size:10pt;font-family:Verdana;">Another difference is that iterator in the HashMap is fail-safe while the enumerator for the Hashtable isn&#8217;t. If you change the map while iterating, you&#8217;ll know. </span><span style="font-size:10pt;font-family:Verdana;">And, a third difference is that HashMap permits null values in it, while Hashtable doesn&#8217;t. </span></p>
<p style="margin:0;"><span style="font-size:10pt;font-family:Verdana;">For new code, I would tend to always use HashMap. </span></p>
<p style="margin:0;" class="MsoBodyText"><font size="2" face="Verdana">1)Synchronized means only one thread can modify a hash table at one point of time.Basically, it means that any thread before performing an update on a hashtable will have to acquire a lock on the object while others will wait for lock to be released.<br />
2)Fail-safe is relevant from the context of iterators.If an iterator has been created on a collection object and some other thread tries to modify the collection object &#8220;structurally&#8221;,a concurrent modification exception will be thrown.It is possible for other threads though to invoke &#8220;set&#8221; method since it doesnt modify the collection &#8220;structurally&#8221;.However, if prior to calling &#8220;set&#8221;, the collection has been modified structurally, &#8220;IllegalArgumentException&#8221; will be thrown.</font></p>
<p><font size="2"><font face="Verdana"> </font></font><strong><span style="font-size:9pt;color:#333333;"><font face="Verdana"> </font></span></strong><strong><span style="font-size:9pt;color:#333333;"><font face="Verdana"> </font></span></strong><strong><span style="font-size:9pt;color:#333333;"><font face="Verdana">how you create an object of a class without using new, please write the code?</font></span></strong><strong><span style="font-size:9pt;color:#333333;"><font face="Verdana"> </font></span></strong><span style="color:#333333;"><font size="2"><font face="Verdana">There are 3 ways to create an object of a class without using new.They are</p>
<p>1.Using Factory Method<br />
2.Using class.forName()<br />
3.Using Clone method</font></font></span><span style="color:#333333;"><font size="2"><font face="Verdana"> </font></font></span><strong><font size="2"><font face="Verdana">When should I use an interface instead of an abstract class?</font></font></strong><strong><font size="2"><font face="Verdana"> </font></font></strong><span style="font-size:10pt;font-family:Verdana;">There are two primary axes of &#8220;inheritance&#8221; in object-oriented languages like Java. &#8220;Implementation&#8221; inheritance is where the sub-class inherits the actual code implementation from the parent. &#8220;Interface&#8221; inheritance is where the &#8220;sub-class&#8221; adheres to the public interface of the &#8220;parent&#8221;.</span><span style="font-size:10pt;font-family:Verdana;">Alas, Java actually mixes the two notions together a bit&#8230; Java interfaces are nice and clean &#8212; when you &#8220;implement&#8221; an interface, you are stipulating that your class adheres to the &#8220;contract&#8221; of the interface that you specified. Java class inheritance isn&#8217;t so clean &#8212; when you sub-class in Java you are getting both the code inheritance but you are also stipulating that your sub-class adheres to the &#8220;contract&#8221; of the interface of the parent class.</span><span style="font-size:10pt;font-family:Verdana;">Abstract classes in Java are just like regular Java classes but with the added constraint that you cannot instantiate them directly. In terms of that added constraint, they are basically classes which don&#8217;t actually implement all of the code specified by their &#8220;contract&#8221;.</span><span style="font-size:10pt;font-family:Verdana;">So, it&#8217;s generally considered good OO practice to specify the &#8220;contract&#8221; which you want to adhere to via Java interfaces. Then use normal Java class inheritance primarily for code reuse purposes. Use abstract Java classes when you want to provide some standard base code but want/need to force the user&#8217;s of your class to complete the implementation (i.e., you create a skeleton implementation and the sub-classes must flesh it out).</span><span style="font-size:10pt;font-family:Verdana;">Abstract class is an incomplete implementation of some concept. This incomplete implementation may be different in different context. Derived class implements the abstract class in its context. </span><span style="font-size:10pt;font-family:Verdana;">Interface defines contract or standard. Implementation of the interface has to follow the contract or standard. Interfaces are more used to set these types of standards or contracts.</span><span style="font-size:10pt;font-family:Verdana;">e.g. In an application there are different editors. There are different toolbars, which can be used on these editors like navigation tool bar. In order to use this tool bar editor should implement that functionality. </span><span style="font-size:10pt;font-family:Verdana;">So there can be interfaces like &#8211; navigation &#8211; search &#8211; saveandmodify </span><span style="font-size:10pt;font-family:Verdana;">if an editor implements all three all toolbars will be enabled. if one then only one will be enable. and implementation of these interface differs from context to context. </span><span style="font-size:10pt;font-family:Verdana;">In the same example myAbstractEditor can be a class, which implements basic functionality of the editor. Where setting different controls, getting values from them can be abstract, which will depend on editor to editor. </span><span style="font-size:10pt;font-family:Verdana;"> </span></p>
<pre><font size="2">Use an abstract class when you want to inherit only from the parent</font></pre>
<pre><font size="2">Use an interface when you want to inherit from multiple sources</font></pre>
<pre><font size="2">Use both when you want a basic behavior from the parent and extra features from other sources</font></pre>
<p><span style="font-size:10pt;font-family:Verdana;"> </span><span style="font-size:10pt;font-family:Verdana;"> </span></p>
<p style="margin:0;" class="MsoBodyText"><span></span></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/rajsekhar.wordpress.com/1/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/rajsekhar.wordpress.com/1/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rajsekhar.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rajsekhar.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rajsekhar.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rajsekhar.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rajsekhar.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rajsekhar.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rajsekhar.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rajsekhar.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rajsekhar.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rajsekhar.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rajsekhar.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rajsekhar.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rajsekhar.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rajsekhar.wordpress.com/1/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rajsekhar.wordpress.com&amp;blog=1379224&amp;post=1&amp;subd=rajsekhar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rajsekhar.wordpress.com/2007/07/17/hello-world/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b4210a4197ff8ce52ff2a1dd9b8a05c9?s=96&#38;d=identicon" medium="image">
			<media:title type="html">rajsekhar</media:title>
		</media:content>
	</item>
	</channel>
</rss>
