tag:blogger.com,1999:blog-74314382152699483992024-03-13T01:09:04.460-07:00"JAVA" knowledge bankUnknownnoreply@blogger.comBlogger13125tag:blogger.com,1999:blog-7431438215269948399.post-62792341025573397592011-01-23T11:42:00.000-08:002011-01-23T11:42:27.352-08:00FAQS on threads concept on JAVA<div dir="ltr" style="text-align: left;" trbidi="on"><div style="color: black; font-family: Georgia,"Times New Roman",serif;"></div><div class="MsoNormal" style="color: black; font-family: Georgia,"Times New Roman",serif; line-height: normal; margin-bottom: 0.0001pt;"><b><span style="font-size: small;">Question: <a href="http://www.blogger.com/post-edit.g?blogID=7431438215269948399&postID=6279234102557339759" name="whatthreaded">What is threaded programming and when is it used?</a> </span></b></div><div class="MsoNormal" style="color: black; font-family: Georgia,"Times New Roman",serif; line-height: normal; margin-left: 36pt;"><br />
</div><div class="MsoNormal" style="color: black; font-family: Georgia,"Times New Roman",serif; line-height: normal; margin-left: 36pt; text-align: justify;"><span style="font-size: small;"> Answer: Threaded programming is normally used when a program is required to do more than one task at the same time. Threading is often used in applications with graphical user interfaces; a new thread may be created to do some processor-intensive work while the main thread keeps the interface responsive to human interaction. </span></div><div></div><div class="MsoNormal" style="color: black; font-family: Georgia,"Times New Roman",serif; line-height: normal; margin-left: 36pt; text-align: justify;"><span style="font-size: small;">The Java programming language has threaded programming facilities built in, so it is relatively easy to create threaded programs. However, multi-threaded programs introduce a degree of complexity that is not justified for most simple command line applications. </span></div><div class="MsoNormal" style="color: black; font-family: Georgia,"Times New Roman",serif; line-height: normal; margin-left: 36pt; text-align: justify;"><br />
</div><div class="MsoNormal" style="color: black; font-family: Georgia,"Times New Roman",serif; line-height: normal; margin-bottom: 0.0001pt;"><b><span style="font-size: small;">Question: What is a thread? </span></b></div><div class="MsoNormal" style="color: black; font-family: Georgia,"Times New Roman",serif; line-height: normal; margin-left: 36pt;"><br />
</div><div class="MsoNormal" style="color: black; font-family: Georgia,"Times New Roman",serif; line-height: normal; margin-left: 36pt; text-align: justify;"><span style="font-size: small;">Answer: In Java the Thread class represents a single independent path of execution in a Java Virtual Machine. When you run a Java program it implicitly starts a single thread of execution. The Thread class enables programmers to create additional threads and set them running. A number of threads may run in parallel, but only one is actively executed at a given moment. </span></div><div></div><div class="MsoNormal" style="color: black; font-family: Georgia,"Times New Roman",serif; line-height: normal; margin-left: 36pt; text-align: justify;"><span style="font-size: small;">The Java runtime system uses fairly complex thread scheduling mechanisms to coordinate the execution of threads, but this does not require privileged knowledge or detail level intervention by programmers. Programmers can manage the high level creation, initiation and distribution of tasks amongst threads through simple Application Programming Interface (API) methods. </span></div><div style="text-align: justify;"></div><div class="MsoNormal" style="color: black; font-family: Georgia,"Times New Roman",serif; line-height: normal; margin-left: 36pt; text-align: justify;"><span style="font-size: small;">The example below shows the simplest approach to thread creation and task execution; construct a new Thread with a Runnable argument and start it.</span></div><div class="MsoNormal" style="color: black; font-family: Georgia,"Times New Roman",serif; line-height: normal; margin-left: 36pt; text-align: justify;"><br />
</div><b> </b><br />
<div class="MsoNormal" style="color: black; font-family: Georgia,"Times New Roman",serif; line-height: normal; margin-bottom: 0.0001pt;"><b><span style="font-size: small;">Question: How do Java threads make the environment asynchronous? </span></b></div><div class="MsoNormal" style="color: black; font-family: Georgia,"Times New Roman",serif; line-height: normal; margin-left: 36pt;"><br />
</div><div class="MsoNormal" style="color: black; font-family: Georgia,"Times New Roman",serif; line-height: normal; margin-left: 36pt; text-align: justify;"><span style="font-size: small;">Answer: The thread mechanism in Java begins with the main entry point thread the runtime environment creates to start a Java program. When you use that initial thread create secondary threads, each one runs independently of the other. The Java virtual machine manages the execution of the threads so they behave as if they all run at the same time, in fact each thread briefly takes turns at execution. </span></div><div></div><div class="MsoNormal" style="color: black; font-family: Georgia,"Times New Roman",serif; line-height: normal; margin-left: 36pt; text-align: justify;"><span style="font-size: small;">In its simplest form there may be no communication or synchronization between multiple threads in a Java program and they each run to completion independently of each other. In this respect Java threads are fundamentally asynchronous, there is no master clock that governs when threads will run and when they synchronize variables to “catch-up” with each other. </span></div><div style="text-align: justify;"><span style="color: black; font-size: small;"><span style="font-family: Georgia,"Times New Roman",serif;"> </span><span style="font-family: Georgia,"Times New Roman",serif; line-height: 115%;">It is often necessary and more useful if threads <b>do</b> check ready states before progressing, synchronize read and write access to shared variables and call-back to each other when their work is done. This is where the </span><span style="font-family: Georgia,"Times New Roman",serif; line-height: 115%;">synchronized</span><span style="font-family: Georgia,"Times New Roman",serif; line-height: 115%;"> keyword and the various </span><span style="font-family: Georgia,"Times New Roman",serif; line-height: 115%;">sleep()</span><span style="font-family: Georgia,"Times New Roman",serif; line-height: 115%;">, </span><span style="font-family: Georgia,"Times New Roman",serif; line-height: 115%;">wait()</span><span style="font-family: Georgia,"Times New Roman",serif; line-height: 115%;"> and </span><span style="font-family: Georgia,"Times New Roman",serif; line-height: 115%;">notify()</span><span style="font-family: Georgia,"Times New Roman",serif; line-height: 115%;"> methods are used to more closely schedule the interaction between asynchronous threads.</span></span></div></div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7431438215269948399.post-70960722517394144922011-01-22T23:54:00.000-08:002011-01-22T23:54:37.594-08:00Example for thread in java<div dir="ltr" style="text-align: left;" trbidi="on"><div style="color: black; font-family: Georgia,"Times New Roman",serif; text-align: justify;"><span style="font-size: small;"> This example illustrates how to create a thread and how to implement the thread. In this example we will see that the program prints numbers from 1 to 10 line by line after 5 seconds which has been declared in the </span> <span style="font-size: small;">sleep</span><span style="font-size: small;"> </span><span style="font-size: small;"> function of the thread class. </span> <span style="font-size: small;">Sleep</span><span style="font-size: small;"> </span><span style="font-size: small;"> function contains the sleeping time in millisecond and in this program </span> <span style="font-size: small;">sleep</span><span style="font-size: small;"> </span><span style="font-size: small;"> function has contained 5000 millisecond mean 5 second time. There is </span> <span style="font-size: small;">sleep</span><span style="font-size: small;"> </span><span style="font-size: small;"> function must caught by the </span> <span style="font-size: small;">InterruptedException</span><span style="font-size: small;">. </span><span style="font-size: small;"> So, this program used the </span> <span style="font-size: small;">InterruptedException</span><span style="font-size: small;"> </span><span style="font-size: small;"> which tells something the user if thread is failed or interrupted.</span> </div><span style="color: black; font-size: small;"><span style="font-family: Georgia,"Times New Roman",serif;"> </span></span><div style="color: black; font-family: Georgia,"Times New Roman",serif; text-align: justify;"><br />
</div><div style="color: black; font-family: Georgia,"Times New Roman",serif;"><span style="font-size: small;"><b>Here is the code of the program : </b></span> </div><span style="color: black; font-size: small;"><span style="font-family: Georgia,"Times New Roman",serif;"> </span><code style="font-family: Georgia,"Times New Roman",serif;"> <b> </b></code></span><br />
<span style="color: black; font-size: small;"><code style="font-family: Georgia,"Times New Roman",serif;"><b>public class </b>Threads{<br />
<b>public static </b><b>void </b>main(String args[]){<br />
Thread th = <b>new </b>Thread();<br />
System.out.println("Numbers are printing line by line after 5 seconds : ");<br />
<b>try</b>{<br />
<b>for</b>(<b>int </b>i = 1;i <= 10;i++)<br />
{<br />
System.out.println(i);<br />
th.sleep(5000);<br />
}<br />
}<br />
<b>catch</b>(InterruptedException e){<br />
System.out.println("Thread interrupted!");<br />
e.printStackTrace();<br />
}<br />
}<br />
}</code></span><span style="color: black;"> </span></div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7431438215269948399.post-4296907023793797842011-01-19T15:35:00.000-08:002011-01-19T15:35:26.102-08:00What is the difference between thread amd process?<div style="color: black; font-family: Georgia,"Times New Roman",serif; text-align: justify;"><span style="font-size: small;"><b>Process:</b></span></div><span style="color: black; font-size: small;"><span style="font-family: Georgia,"Times New Roman",serif;"> </span></span><div style="color: black; font-family: Georgia,"Times New Roman",serif; text-align: justify;"><span style="font-size: small;"> A <b>process</b> is an instance of a computer program that is executed sequentially. It is a collection of instructions which are executed simultaneously at the run time. Thus several processes may be associated with the same program. For example, to check the <b>spelling </b>is a single process in the <b>Word Processor</b> program and you can also use other processes like <b>printing, formatting, drawing, etc</b>. associated with this program.</span></div><div style="color: black; font-family: Georgia,"Times New Roman",serif; text-align: justify;"><br />
</div><span style="color: black; font-size: small;"><span style="font-family: Georgia,"Times New Roman",serif;"> </span></span><div style="color: black; font-family: Georgia,"Times New Roman",serif; text-align: justify;"><span style="font-size: small;"><b>Thread:</b></span></div><span style="color: black; font-size: small;"><span style="font-family: Georgia,"Times New Roman",serif;"> </span></span><div class="MsoNormal" style="color: black; font-family: Georgia,"Times New Roman",serif; text-align: justify;"><span style="font-size: small;"> A thread is a <b>lightweight </b>process which exist within a program and executed to perform a special task. </span><span style="font-size: small;">Several threads of execution may be associated with a single process. Thus a process that has only one thread is referred to as a <b>single-threaded </b>process, while a process with multiple threads is referred to as a <b>multi-threaded</b> process.</span></div><div class="MsoNormal" style="color: black; font-family: Georgia,"Times New Roman",serif; text-align: justify;"><br />
</div><span style="color: black; font-size: small;"><span style="font-family: Georgia,"Times New Roman",serif;"> </span></span><div class="MsoNormal" style="color: black; font-family: Georgia,"Times New Roman",serif; text-align: justify;"><span style="font-size: small;"> In Java Programming language, thread is a sequential path of code execution within a program. Each thread has its own local variables, program counter and lifetime. In single threaded runtime environment, operations are executes sequentially i.e. next operation can execute only when the previous one is complete. It exists in a common memory space and can share both data and code of a program.</span><span style="font-size: small;"> Threading concept is very important in Java</span><span style="font-size: small;"> through which we can increase the speed of any application. You can see diagram shown below in which a thread is executed along with its several operations with in a single process. </span></div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7431438215269948399.post-45524195054768367692011-01-18T14:21:00.000-08:002011-01-18T14:22:42.299-08:00Explain what is thread? and it's life cycle in java<div style="color: black; font-family: Georgia,"Times New Roman",serif; text-align: justify;"><br />
</div><div style="color: black; font-family: Georgia,"Times New Roman",serif; text-align: justify;"><span style="font-size: small;"> A <i>thread</i> is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently. </span></div><div style="color: black; font-family: Georgia,"Times New Roman",serif; text-align: justify;"><span style="font-size: small;"> Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon. When code running in some thread creates a new <code>Thread</code> object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon. </span></div><div style="color: black; font-family: Georgia,"Times New Roman",serif; text-align: justify;"><br />
</div><div style="color: black; font-family: Georgia,"Times New Roman",serif; text-align: justify;"><span style="font-size: small;">When you are programming with threads, understanding the life cycle of thread is very valuable. While a thread is alive, it is in one of several states. By invoking start() method, it doesn’t mean that the thread has access to CPU and start executing straight away. Several factors determine how it will proceed.<br />
<br />
<b> Different states of a thread are :</b></span></div><span style="color: black; font-size: small;"><span style="font-family: Georgia,"Times New Roman",serif;"> </span></span><br />
<div style="color: black; font-family: Georgia,"Times New Roman",serif; text-align: justify;"><span style="font-size: small;"><img border="0" height="284" src="http://www.roseindia.net/java/thread/life-c1.gif" width="396" /></span></div><span style="color: black; font-size: small;"><span style="font-family: Georgia,"Times New Roman",serif;"> </span></span><br />
<ol style="color: black; font-family: Georgia,"Times New Roman",serif; text-align: justify;"><li><span style="font-size: small;"><b>New state –</b> After the creations of Thread instance the thread is in this state but before the start() method invocation. At this point, the thread is considered not alive.<br />
</span></li>
<li><span style="font-size: small;"><b>Runnable (Ready-to-run) state – </b> A thread start its life from Runnable state. A thread first enters runnable state after the invoking of start() method but a thread can return to this state after either running, waiting, sleeping or coming back from blocked state also. On this state a thread is waiting for a turn on the processor.<br />
</span></li>
<li><span style="font-size: small;"><b>Running state – </b> A thread is in running state that means the thread is currently executing. There are several ways to enter in Runnable state but there is only one way to enter in Running state: the scheduler select a thread from runnable pool.<br />
</span></li>
<li><span style="font-size: small;"><b>Dead state –</b> A thread can be considered dead when its run() method completes. If any thread comes on this state that means it cannot ever run again. </span></li>
<li><span style="font-size: small;"><b>Blocked - </b>A thread can enter in this state because of waiting the resources that are hold by another thread.</span></li>
</ol>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7431438215269948399.post-52629937386277959712011-01-15T08:40:00.000-08:002011-01-15T08:41:19.884-08:00Difference between an interface and an abstract class?<div style="color: black;"><span style="font-family: Georgia,"Times New Roman",serif; font-size: small;"> An abstract class may contain code in method bodies, which is not allowed in an <span style="font-family: inherit; font-weight: inherit ! important; position: static;"><span class="kLink" style="font-weight: inherit ! important; position: static;">interface</span></span>.</span></div><div style="color: black; text-align: justify;"><span style="font-family: Georgia,"Times New Roman",serif; font-size: small;"> With abstract classes, you have to inherit your class from it and Java does not allow multiple inheritance. On the other hand, you can implement multiple interfaces in your class.</span></div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7431438215269948399.post-22625336106427120732011-01-12T19:47:00.000-08:002011-01-13T23:57:05.967-08:00sloution to ANT build error "error during FTP transfer: j ava.net.ConnectException: Connection refused: connect"<div style="font-family: Georgia,"Times New Roman",serif;"><span style="font-size: small;">This is my personal experience, when I am tying to access the Unix server through FTP, I got the below error:</span></div><div style="font-family: Georgia,"Times New Roman",serif;"></div>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />
<br />
<div class="MsoNormal">C:\ant_scripting\apache-ant-1.8.2\build.xml:20: Could not create type ftp due to java.lang.NoClassDefFoundError: org/apache/commons/net/ftp/FTPClientConfig at org.apache.tools.ant.taskdefs.optional.net.FTP$LanguageCode.getValidLanguageCodes(FTP.java:2693)</div><div class="MsoNormal"> at org.apache.tools.ant.taskdefs.optional.net.FTP$LanguageCode.<clinit>(FTP.java:2689)</clinit></div><div class="MsoNormal"> at org.apache.tools.ant.taskdefs.optional.net.FTP.<init>(FTP.java:135)</init></div><div class="MsoNormal"> at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)</div><div class="MsoNormal"><br />
</div><div class="MsoNormal"> at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)</div><div class="MsoNormal"> at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)</div><div class="MsoNormal"> at java.lang.reflect.Constructor.newInstance(Constructor.java:274)</div><div class="MsoNormal"> at org.apache.tools.ant.AntTypeDefinition.innerCreateAndSet(AntTypeDefinition.java:328)</div><div class="MsoNormal"> at org.apache.tools.ant.AntTypeDefinition.createAndSet(AntTypeDefinition.java:274)</div><div class="MsoNormal"> at org.apache.tools.ant.AntTypeDefinition.icreate(AntTypeDefinition.java:219)</div><div class="MsoNormal"> at org.apache.tools.ant.AntTypeDefinition.create(AntTypeDefinition.java:206)</div><div class="MsoNormal"> at org.apache.tools.ant.ComponentHelper.createComponent(ComponentHelper.java:286)</div><div class="MsoNormal"> at org.apache.tools.ant.ComponentHelper.createComponent(ComponentHelper.java:264)</div><div class="MsoNormal"> at org.apache.tools.ant.UnknownElement.makeObject(UnknownElement.java:417)</div><div class="MsoNormal"> at org.apache.tools.ant.UnknownElement.maybeConfigure(UnknownElement.java:163)</div><div class="MsoNormal"> at org.apache.tools.ant.Task.perform(Task.java:347)</div><div class="MsoNormal"> at org.apache.tools.ant.Target.execute(Target.java:390)</div><div class="MsoNormal"> at org.apache.tools.ant.Target.performTasks(Target.java:411)</div><div class="MsoNormal"> at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)</div><div class="MsoNormal"> at org.apache.tools.ant.Project.executeTarget(Project.java:1368)</div><div class="MsoNormal"> at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)</div><div class="MsoNormal"> at org.apache.tools.ant.Project.executeTargets(Project.java:1251)</div><div class="MsoNormal"> at org.apache.tools.ant.Main.runBuild(Main.java:809)</div><div class="MsoNormal"> at org.apache.tools.ant.Main.startAnt(Main.java:217)</div><div class="MsoNormal"> at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)</div><div class="MsoNormal"> at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)</div><div class="MsoNormal"><br />
</div><div class="MsoNormal">~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</div><div class="MsoNormal" style="font-family: Georgia,"Times New Roman",serif;"><span style="font-size: small;">solution which I have find to avoid this error was given below:</span></div><div class="MsoNormal" style="font-family: Georgia,"Times New Roman",serif;"><span style="font-size: small;"><br />
</span></div><div class="MsoNormal" style="font-family: Georgia,"Times New Roman",serif;"><span style="font-size: small;">1. To run FTP command by using ant script, you must have to down load the <span style="line-height: 115%;"> commons-net-1.4.1.jar file.</span></span></div><div class="MsoNormal" style="font-family: Georgia,"Times New Roman",serif;"><span style="font-size: small;"><span style="line-height: 115%;"> </span><a href="http://commons.apache.org/net/download_net.cgi">click here</a> to down load the file, please make sure to download the jar file according to your JAVA version,</span></div><div class="MsoNormal" style="font-family: Georgia,"Times New Roman",serif;"><span style="font-size: small;">Find more details on the Apache link.</span></div><div class="MsoNormal" style="font-family: Georgia,"Times New Roman",serif;"><span style="font-size: small;">2. please make sure to put the jar file in the class path. and run the script, oops you won't get any error now.</span></div><div class="MsoNormal" style="font-family: Georgia,"Times New Roman",serif;"><span style="font-size: small;"><br />
</span></div><div class="MsoNormal" style="font-family: Georgia,"Times New Roman",serif;"><span style="font-size: small;">please put your comment in case of any issues. </span></div><div class="MsoNormal"><br />
</div><div class="MsoNormal"><br />
</div><div class="MsoNormal"><br />
</div><div class="MsoNormal"><br />
</div>Unknownnoreply@blogger.com1tag:blogger.com,1999:blog-7431438215269948399.post-75628295612558796612010-11-10T10:05:00.000-08:002011-01-14T00:04:19.818-08:00String, StringBuffer & stringBuilder in JAVA<div style="text-align: justify;"><span class="Apple-style-span" style="font-family: Verdana,sans-serif;"><span class="Apple-style-span" style="font-size: small;"><b>1] Differnces between the above three is:</b></span></span></div><div style="text-align: justify;"><span class="Apple-style-span" style="font-family: Verdana,sans-serif;"> String is immutable</span></div><div style="text-align: justify;"><span class="Apple-style-span" style="font-family: Verdana,sans-serif;"><span class="Apple-style-span" style="font-size: small;"> StringBuffer & StringBuilders are mutable.</span></span></div><div style="text-align: justify;"><span class="Apple-style-span" style="font-family: Verdana,sans-serif;"><span class="Apple-style-span" style="font-size: small;"><br />
</span></span></div><div style="text-align: justify;"><span class="Apple-style-span" style="font-family: Verdana,sans-serif;"><b>2] Differnces between the StringBuffer& stringBuiler:</b></span></div><div style="text-align: justify;"><span class="Apple-style-span" style="font-family: Verdana,sans-serif;"> StringBuffer is synchronized.</span></div><div style="text-align: justify;"><span class="Apple-style-span" style="font-family: Verdana,sans-serif;"><span class="Apple-style-span" style="font-size: small;"> StringBuilder is unsynchronized.</span></span></div><div style="text-align: justify;"><span class="Apple-style-span" style="font-family: Verdana,sans-serif;"><span class="Apple-style-span" style="font-size: small;"><br />
</span></span></div><div style="text-align: justify;"><span class="Apple-style-span" style="font-family: Verdana,sans-serif;"><b>3] On which scenario we have to use the above three?</b></span></div><div style="text-align: justify;"><span class="Apple-style-span" style="font-family: Verdana,sans-serif;"> </span><span class="Apple-style-span" style="font-family: Verdana,sans-serif;"><b>String:</b></span><span class="Apple-style-span" style="font-family: Verdana,sans-serif;"> If the text is not going to change use a string Class because a String object is immutable.</span></div><div style="text-align: justify;"><span class="Apple-style-span" style="font-family: Verdana,sans-serif;"> </span><span class="Apple-style-span" style="font-family: Verdana,sans-serif;"><b>StringBuffer:</b></span><span class="Apple-style-span" style="font-family: Verdana,sans-serif;"> If the text can changes, and will be accessed from multiple threads, use a StringBuffer because StringBuffer is synchronous.</span></div><div style="text-align: justify;"><span class="Apple-style-span" style="font-family: Verdana,sans-serif;"> </span><span class="Apple-style-span" style="font-family: Verdana,sans-serif;"><b>StringBuilder :</b></span><span class="Apple-style-span" style="font-family: Verdana,sans-serif;"> If the text can change and will only be accessed from a single thread, then use StringBuilder because StringBuilder is unsynchronized.</span></div>Unknownnoreply@blogger.com1tag:blogger.com,1999:blog-7431438215269948399.post-54146982515684168482010-07-04T03:32:00.000-07:002011-01-14T00:04:03.607-08:00Setting the class path to JAVA on Windows XP and 2000<span style="font-weight: bold;"><span class="Apple-style-span" style="color: #990000;">Setting the class path to JAVA on Windows XP &2000</span></span><br />
<div style="color: black;"><br />
</div><div style="color: black; text-align: justify;"><span class="Apple-style-span">• Open the System Properties window (by right-clicking on My Computer and selecting Properties, or through the Control Panel - Start -> Control Panel -> System).</span></div><div style="color: black; text-align: justify;"><span class="Apple-style-span">• On the Advanced tab, click the Environment Variables button.</span></div><div style="color: black; text-align: justify;"><span class="Apple-style-span">• In user variables, select the new tab and give the variable name as JAVA_HOME and value as path of the java folder (Example: C:\Java)</span></div><div style="color: black; text-align: justify;"><span class="Apple-style-span">• In System variables we have to set the CLASSPATH; if it is already exists then add the java path up o lib folder (Example C:\Java\lib)</span></div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7431438215269948399.post-29941464800852840982008-02-24T22:15:00.001-08:002011-01-13T23:30:35.455-08:00STRUTS FAQS<div style="color: black; text-align: justify;"><b><span class="Apple-style-span">Struts</span></b></div><div style="color: black; text-align: justify;"><span class="Apple-style-span"><br />
</span></div><div style="color: black; text-align: justify;"><span class="Apple-style-span">Struts is a open source framework which make building of the web applications </span><span class="Apple-style-span">easier based on the java Servlet and JavaServer pages technologies.</span></div><div style="color: black; text-align: justify;"><span class="Apple-style-span"><br />
</span></div><div style="color: black; text-align: justify;"><span class="Apple-style-span">Struts framework was created by Craig R. McClanahan and donated to the Apache Software Foundation in 2000. The Project now has several committers, and many developers are contributing to overall to the framework.</span></div><div style="color: black; text-align: justify;"><span class="Apple-style-span"><br />
</span></div><div style="color: black; text-align: justify;"><span class="Apple-style-span">Developing web application using struts frame work is fairly complex, but it </span><span class="Apple-style-span">eases things after it is setup. It encourages software development following </span><span class="Apple-style-span">the MVC design pattern. Many web applications are JSP-only or Servlets-only.</span><a class="cssButton" href="javascript:void(0)" id="draftButton" onclick="if (this.className.indexOf("ubtn-disabled") == -1) {var e = document['postingForm'].saveDraft;(e.length) ? e[0].click() : e.click(); if (window.event) window.event.cancelBubble = true; return false;}" target=""></a><br />
<div class="cssButtonOuter"><div class="cssButtonMiddle"><div class="cssButtonInner"><a class="cssButton" href="javascript:void(0)" id="draftButton" onclick="if (this.className.indexOf("ubtn-disabled") == -1) {var e = document['postingForm'].saveDraft;(e.length) ? e[0].click() : e.click(); if (window.event) window.event.cancelBubble = true; return false;}" target=""> </a></div></div></div></div><div style="color: black; text-align: justify;"><span class="Apple-style-span">With JSP and Servlets, Java code is embedded in the HTML code and the Java code calls println methods to generate the HTML code respectively. Both approaches have their advantages and drawbacks; Struts gathers their strengths to get the best of their association.</span></div><div style="color: black; text-align: justify;"><span class="Apple-style-span"><br />
</span></div><div style="color: black; text-align: justify;"><span class="Apple-style-span">Struts is based on Model-View-Controller (MVC) design paradigm, it is an implementation of JSP Model 2 Architecture. For more of Model-View-Controller </span><span class="Apple-style-span">(MVC) click here.</span></div><div style="color: black; text-align: justify;"><span class="Apple-style-span"><br />
</span></div><div style="color: black; text-align: justify;"><span class="Apple-style-span">Consists of 8 Top-Level Packagesand approx 250 Classes and Interfaces.</span></div><div style="color: black; text-align: justify;"><span class="Apple-style-span"><br />
</span></div><div style="color: black; text-align: justify;"><span class="Apple-style-span">Struts is a set of cooperating classes, servlets, and JSP tags that make up a reusable MVC 2 design. This definition implies that Struts is a framework, rather than a library, but Struts also contains an extensive tag library and utility classes that work independently of the framework.</span></div><div style="color: black; text-align: justify;"><span class="Apple-style-span"><br />
</span></div><div style="color: black; text-align: justify;"><span class="Apple-style-span">The overview of struts</span></div><div style="color: black; text-align: justify;"><span class="Apple-style-span"><br />
</span></div><div style="color: black; text-align: justify;"><span class="Apple-style-span">Client browser</span></div><div style="color: black; text-align: justify;"><span class="Apple-style-span">An HTTP request from the client browser creates an event. The Web container will respond with an HTTP response.</span></div><div style="color: black; text-align: justify;"><span class="Apple-style-span"><br />
</span></div><div style="color: black; text-align: justify;"><span class="Apple-style-span">Controller</span></div><div style="color: black; text-align: justify;"><span class="Apple-style-span">The controller is responsible for intercepting and translating user input into actions to</span></div><div style="color: black; text-align: justify;"><span class="Apple-style-span">be performed by the model. The controller is responsible for selecting the next view based on user input and the outcome of model operations.The Controller receives the request from the browser, and makes the decision where to send the request. With Struts, the Controller is a command design pattern implemented as a servlet. The struts-config.xml file configures the Controller.</span></div><div style="color: black; text-align: justify;"><span class="Apple-style-span"><br />
</span></div><div style="color: black; text-align: justify;"><span class="Apple-style-span"><br />
</span></div><div style="color: black; text-align: justify;"><span class="Apple-style-span">Business logic</span></div><div style="color: black; text-align: justify;"><span class="Apple-style-span">The business logic updates the state of the model and helps control the flow of the application. With Struts this is done with an Action class as a thin wrapper to the actual business logic.</span></div><div style="color: black; text-align: justify;"><span class="Apple-style-span"><br />
</span></div><div style="color: black; text-align: justify;"><span class="Apple-style-span">Model</span></div><div style="color: black; text-align: justify;"><span class="Apple-style-span">A model represents an application’s data and contains the logic for accessing and manipulating that data. Any data that is part of the persistent state of the application should reside in the model objects. The business objects update the application state. ActionForm bean represents the Model state at a session or request level, and not at a persistent level. Model services are accessed by the controller for either querying or effecting a change in the model state. The model notifies the view when a state change occurs in the model.The JSP file reads information from the ActionForm bean using JSP tags.</span></div><div style="color: black; text-align: justify;"><span class="Apple-style-span"><br />
</span></div><div style="color: black; text-align: justify;"><span class="Apple-style-span">View</span></div><div style="color: black; text-align: justify;"><span class="Apple-style-span">The view is responsible for rendering the state of the model. The presentation semantics are encapsulated within the view, therefore model data can be adapted for several different kinds of clients.The view modifies itself when a change in the model is communicated to the view. A view forwards user input to the controller.The view is simply a JSP file. There is no flow logic, no business logic, and no model information -- just tags. Tags are one of the things that make Struts unique compared to other frameworks like Velocity.</span></div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7431438215269948399.post-47077833735095960272008-02-24T22:14:00.002-08:002011-01-14T00:03:46.556-08:00SERVLET FAQS<div style="text-align: justify;">Q: What's the difference between applets and servlets? </div><div style="text-align: justify;"><br />
</div><div style="text-align: justify;">A: There are many fundamental differences between Applet and Servlet classes, the Java API documentation for the two types will show you they have little in common. </div><div style="text-align: justify;"><br />
</div><div style="text-align: justify;">Applets are essentially graphical user interface (GUI) applications that run on the client side in a network environment, typically embedded in an HTML page. Applets are normally based on Abstract Windowing Toolkit components to maintain backward-compatibility with the widest range of browsers' Java implementations. The application classes are downloaded to the client and run in a Java Virtual Machine provided by the browser, in a restrictive security environment called a "sandbox". </div><div style="text-align: justify;"><br />
</div><div style="text-align: justify;">Servlets are used to dynamically generate HTTP responses and return HTML content to Web browsers on the server side. Servlets are often used to validate and process HTML form submissions and control a series of user interactions in what is known as a Web application. Servlets can be used to control all aspects of the request and response exchange between a Web browser and the server, called a servlet container.</div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7431438215269948399.post-71461223955883302582008-02-24T22:14:00.001-08:002008-02-24T22:14:43.795-08:00JDBC FAQS:Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7431438215269948399.post-9534305218302985912008-02-24T22:13:00.000-08:002011-01-13T23:29:54.427-08:00JSP FAQS:<div style="text-align: justify;">What are the implicit objects? - Implicit objects are objects that are created by the web container and contain information related to a particular request, page, or application. They are: request, response, pageContext, session, application, out, config, page, exception.</div><div style="text-align: justify;"><br />
</div><div style="text-align: justify;">Is JSP technology extensible? - Yes. JSP technology is extensible through the development of custom actions, or tags, which are encapsulated in tag libraries. </div><div style="text-align: justify;"><br />
</div><div style="text-align: justify;">How can I implement a thread-safe JSP page? What are the advantages and Disadvantages of using it? - You can make your JSPs thread-safe by having them implement the SingleThreadModel interface. This is done by adding the directive within your JSP page. With this, instead of a single instance of the servlet generated for your JSP page loaded in memory, you will have N instances of the servlet loaded and initialized, with the service method of each instance effectively synchronized. You can typically control the number of instances (N) that are instantiated for all servlets implementing SingleThreadModel through the admin screen for your JSP engine. More importantly, avoid using the tag for variables. If you do use this tag, then you should set isThreadSafe to true, as mentioned above. Otherwise, all requests to that page will access those variables, causing a nasty race condition. SingleThreadModel is not recommended for normal use. There are many pitfalls, including the example above of not being able to use . You should try really hard to make them thread-safe the old fashioned way: by making them thread-safe </div><div style="text-align: justify;"><br />
</div><div style="text-align: justify;">How does JSP handle run-time exceptions? - You can use the errorPage attribute of the page directive to have uncaught run-time exceptions automatically forwarded to an error processing page. For example: </div><div style="text-align: justify;">redirects the browser to the JSP page error.jsp if an uncaught exception is encountered during request processing. Within error.jsp, if you indicate that it is an error-processing page, via the directive: Throwable object describing the exception may be accessed within the error page via the exception implicit object. Note: You must always use a relative URL as the value for the errorPage attribute. </div><div style="text-align: justify;"><br />
</div><div style="text-align: justify;">How do I prevent the output of my JSP or Servlet pages from being cached by the browser? - You will need to set the appropriate HTTP header attributes to prevent the dynamic content output by the JSP page from being cached by the browser. Just execute the following scriptlet at the beginning of your JSP pages to prevent them from being cached at the browser. You need both the statements to take care of some of the older browser versions. </div><div style="text-align: justify;"><br />
</div><div style="text-align: justify;">How do I use comments within a JSP page? - You can use “JSP-style” comments to selectively block out code while debugging or simply to comment your scriptlets. JSP comments are not visible at the client. For example: </div><div style="text-align: justify;"><br />
</div><div style="text-align: justify;">--%></div><div style="text-align: justify;">You can also use HTML-style comments anywhere within your JSP page. These comments are visible at the client. For example:</div><div style="text-align: justify;"><br />
</div><div style="text-align: justify;">Of course, you can also use comments supported by your JSP scripting language within your scriptlets. For example, assuming Java is the scripting language, you can have:</div><div style="text-align: justify;"><br />
</div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7431438215269948399.post-56051690822575086342008-02-24T22:11:00.000-08:002011-01-13T23:29:22.048-08:00CORE JAVA FAQS.............Java is an object-oriented programming language developed by Sun Microsystems. Java language was designed to be small, simple, and portable across platforms and operating systems, both at the source and at the binary level, which means that Java programs (applets and applications) can run on any machine that has the Java virtual machine installed.<br />
<br />
Java is Platform independent, Platform independence means, the ability of a program to move easily from one computer system to another-is one of the most significant advantages that Java has over other programming languages, particularly if your software needs to run on many different platforms.<br />
<br />
The Java language was developed at Sun Microsystems in 1991 as part of a research project to develop software for consumer electronics devices. Java's rapidly growing popularity is due to the Web. But Java's inherent power does not come from the fact that it is a Web programming language. The talented software engineers at Sun, in bringing Java to the Web, have elegantly solved a much broader and more significant problem-how to develop network-capable windowing software that will run on almost any 32-bit computer and operating system.<br />
<br />
A software developer writes programs in the Java language that use predefined software packages of the Java API. The developer compiles his or her programs using the Java compiler. This results in what is known as compiled bytecode. Bytecode is in a form that can be executed on the Java virtual machine, the core of the Java runtime system. You can think of the virtual machine as a microprocessor that is implemented in software and runs using the capabilities provided by your operating system and computer hardware. Since the Java virtual machine is not a real microprocessor, the Java bytecode is interpreted, rather than executed directly in the native machine instructions of the host computer.<br />
<br />
<br />
<br />
<br />
<br />
Java is exceptionally well suited to distributed networking applications because of its built-in networking support and the runtime system's capability to dynamically load Java bytecode across the network. Java also provides the capability to dynamically utilize new content and protocol handling software. The HotJava browser, written in Java, is an excellent example of Java's distributed networking capabilities.<br />
<br />
The Java API provides full support of multithreaded programming. Multithreaded programs can be developed in a single, consistent manner, independent of the vagaries of the host operating system interface.<br />
<br />
Java classes and objects directly support the object-oriented concepts of encapsulation, inheritance, messages and methods, and data hiding. Java interfaces provide support for multiple inheritance and polymorphism. The Java language retains all the benefits of object-oriented programming without the performance impacts associated with pure object languages, such as Smalltalk.<br />
<br />
The Java API provides extensive support of windowing and graphical user interface development without the complexities associated with maintaining multiple window class libraries. Several visual programming tools have been developed for Java.Unknownnoreply@blogger.com0