Posts

Showing posts from January, 2012

Servlet Simple Example

Image
  Create the HTML file: index.html <html> <head> <title>The servlet example </title> </head> <body> <h1>A simple web application</h1> <form method="POST" action="WelcomeServlet"> <label for="name">Enter your name </label> <input type="text" id="name" name="name"/><br><br> <input type="submit" value="Submit Form"/> <input type="reset" value="Reset Form"/> </form> </body> </html> The welcome Servlet class package nkpack.servletexample ;   import java.io.IOException ; import java.io.PrintWriter ;   import javax.servlet.ServletConfig ; import javax.servlet.ServletException ; import javax.servlet.http.HttpServlet ; import javax.servlet.http.HttpServletRequest ; import javax.servlet.http.HttpServletResponse ;   public class...

Thread

Image
Introduction to Threads Multithreading refers to two or more tasks executing concurrently within a single program. A thread is an independent path of execution within a program. Many threads can run concurrently within a program. Every thread in Java is created and controlled by the java.lang.Thread class . A Java program can have many threads, and these threads can run concurrently, either asynchronously or synchronously. Multithreading has several advantages over Multiprocessing such as; Threads are lightweight compared to processes Threads share the same address space and therefore can share both data and code Context switching between threads is usually less expensive than between processes Cost of thread intercommunication is relatively low that that of process intercommunication Threads allow different tasks to be performed concurrently. The following figure shows the methods that are members of the Object and Thread Class. Thread Creation The...

Different ways to create an object in Java?

Image
There are four different ways to create objects in java: A. Using new keyword This is the most common way to create an object in java. Almost 99% of objects are created in this way.   MyObject object = new MyObject (); B. Using Class.forName() If we know the name of the class & if it has a public default constructor we can create an object in this way. MyObject object = ( MyObject ) Class . forName ( "subin.rnd.MyObject" ). newInstance (); C. Using clone() The clone() can be used to create a copy of an existing object. MyObject anotherObject = new MyObject (); MyObject object = anotherObject . clone (); D. Using object deserialization Object deserialization is nothing but creating an object from its serialized form. ObjectInputStream inStream = new ObjectInputStream ( anInputStream ); MyObject object = ( MyObject ) inStream . readObject ();

java.io

Image
  Create a StringWriter object The default constructor allows us to create a StringWriter with default string buffer size. import  java.io.StringWriter;      /**    * StringWriter class examples.    */    public   class  StringWriterExample {        public   static   void  main(String[] args){            // Create a stringWriter object.            StringWriter writer =  new  StringWriter();       }   }   Compare two files using java The compareTo method of File class helps us to create a temporary file. This method returns an integer. If the value of the integer is greater than zero, the file will be greater than the argument, ...

Swing AWT Interview Questions

1 Q What is JFC? A JFC stands for Java Foundation Classes. The Java Foundation Classes (JFC) are a set of Java class libraries provided as part of Java 2 Platform, Standard Edition (J2SE) to support building graphics user interface (GUI) and graphics functionality for client applications that will run on popular platforms such as Microsoft Windows, Linux, and Mac OSX. 2 Q What is AWT? A AWT stands for Abstract Window Toolkit. AWT enables programmers to develop Java applications with GUI components, such as windows, and buttons. The Java Virtual Machine (JVM) is responsible for translating the AWT calls into the appropriate calls to the host operating system. 3 Q What are the differences between Swing and AWT? A AWT is heavy-weight components, but Swing is light-weight components. AWT is OS dependent because it uses native components, But Swing components are OS independent. We can change the look and feel in Swing which is not possible in AWT. Swing takes le...

JDBC Interview questions

1. What is JDBC? JDBC technology is an API (included in both J2SE and J2EE releases) that provides cross-DBMS connectivity to a wide range of SQL databases and access to other tabular data sources, such as spreadsheets or flat files. With a JDBC technology-enabled driver, you can connect all corporate data even in a heterogeneous environment 2. What are stored procedures? A stored procedure is a set of statements/commands which reside in the database. The stored procedure is precompiled. Each Database has it’s own stored procedure language, 3. What is JDBC Driver ? The JDBC Driver provides vendor-specific implementations of the abstract classes provided by the JDBC API. This driver is used to connect to the database. 4. What are the steps required to execute a query in JDBC? First we need to create an instance of a JDBC driver or load JDBC drivers, then we need to register this driver with DriverManager class. Then we can open a connection. By using this connection , ...

JMS interview question

1. What is JMS? The Java Message Service (JMS) API is a messaging standard that allows application components based on the Java 2 Platform, Enterprise Edition (J2EE) to create, send, receive, and read messages. It enables distributed communication that is loosely coupled, reliable, and asynchronous 2. What type messaging is provided by JMS Both synchronous and asynchronous are provided by JMS. 3. What is messaging? Messaging is a mechanism by which data can be passed from one application to another application. 4. What are the advantages of JMS? One of the principal advantages of JMS messaging is that it’s asynchronous. Thus not all the pieces need to be up all the time for the application to function as a whole. 5. What is synchronous messaging? Synchronous messaging involves a client that waits for the server to respond to a message. So if one end is down the entire communication will fail. 6. What is asynchronous messaging? Asynchronous messaging involves a clien...

Core Java Interview Question

Question: How could Java classes direct program messages to the system console, but error messages, say to a file? Answer: The class System has a variable out that represents the standard output, and the variable err that represents the standard error device. By default, they both point at the system console. This how the standard output could be re-directed: Stream st = new Stream(new FileOutputStream("output.txt")); System.setErr(st); System.setOut(st);   Question: What's the difference between an interface and an abstract class? Answer: An abstract class may contain code in method bodies, which is not allowed in an interface. 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. Question: Why would you use a synchronized block vs. synchronized method? Answer: Synchronized blocks place locks for shorter periods than s...