1. read a line of input at a time in Java?

The standard input stream is represented by the InputStream object System.in. For reading a whole line at a time BufferedReader is chained to an InputStreamReader that in turn is chained to System.in. For example: This program read line from a file “students” and displays it on screen.

import java.io.*;
import java.util.*;
public class stud
{
public static void main (String args[ ]) throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(newFileInputStream(“students”)));
String s= “”;
while ((s= br.readLine())!=null) System.out.println(s);
}
catch (IOException e)
{
System.out.println(e);
}
}

2. Explain different Authentication Options available in Servets?

Authentication options available in Servlets:

There are four different options for authentication in servlets

1. Basic Authentication- In this the server uses the username and password provided by the client to authenticate the user. The server gives several attempts (up to 3 attempts) after the three wrong attempts it gives the error page (usually an HTTP 401 Unauthorized error).
2. Form-based authentication- In this the login form is made by the programmer by using HTML. This is more helpful than the basic authentication because we can create more login interface other than username and password for the authentication.
3. Digest Authentication- It is similar to basic authentication but in this the passwords are encrypted using Hash formula. This makes digest more secured.
4. Client certificate Authentication- It requires that each client accessing the resource has a certificate that it send to authenticate itself. This requires SSL protocol.

3. Explain Difference between Tomcat and Weblogic server?

Tomcat is a web server which can run servlets and JSP whereas Weblogic is an application server which can run EJBs also. Tomcat is owned by Apache Software Foundation and weblogic is owned by Oracle. Weblogic fully supports the Java Enterprise Edition like EJB but Tomcat does not support EJB. Transaction management can be done in Weblogic but it can't be done in Tomcat. Weblogic supports both Http and Ftp but Tomcat only supports Http. Oracle WebLogic Server Process Edition also include Business Process Management and Data Mapping functionality. WebLogic supports security policies managed by security administrators. The Oracle WebLogic Server Security Model includes:

-application business logic separated from security code
-complete scope of security coverage for all Java EE and non-Java EE components

4. How does the native language C or C++ understand data types in JAVA?

Java native Interface (JNI) is a programming framework that allows Java code to interact with code written in another language, typically C or C++. JNI enables one to write native methods to handle situations when an application cannot be written entirely in the Java programming language.

It is also used to modify an existing application written in another programming language to be accessible to Java applications. Many of the standard library classes depend on JNI to provide functionality to the developer and the user. The Java Development Kit (JDK) provides tools and library routines that help the Java programmer interface with native code. It is important that the data types that are passed between Java and native code have the same properties. Like jboolean data type of native language is same as Boolean data type of Java.

5. What is a StringBuffer class and how does it differ from String Class?

In contrast to the String class which implements immutable character strings, the StringBuffer class implements mutable character strings. Not only can the character string in a string buffer gets changed, but the buffer's capacity can also change dynamically. The capacity of a String Buffer is the maximum number of characters that a string buffer can accommodate, before its size is automatically augmented.

Both String and StringBuffer class are thread-safe.

The syntax for initializing String class object is: String str1=”Hello”;

The syntax for initializing StringBuffer class object is:
StringBuffer sbr1= new StringBuffer (“hello”);
StringBuffer sbr2= new StringBuffer (10);
StringBuffer sbr3= new StringBuffer();

6. Explain widening conversion and Narrowing conversion?

For a primitive data types, a value narrower data type can be converted to a value of a broader data type without loss of information. This is called Widening conversion. For example an int is directly converted to a double without first having to convert it to a long and a float.

Converting from a broader data type to a narrower data type is called narrowing conversion, which can result in loss of information. For example conversion between char and byte and short are narrowing conversions. For example byte takes 8 bits, short takes 16 bits, int takes 32 bits, long takes 64 bits. Conversion of short to int or int to long is widening conversion and conversion of int to short is a narrowing conversion.

7. How to use beans in JSP?

Java Beans are reusable components. They are used to separate Business Logic from Presentation Logic. Internally a bean is just an instance of a class. JSP’s provide three basic tags for working with Beans.

* <jsp : useBean id="bean name" class="bean class" scope= "page | request | session | application " />

bean name = the name that refers to the bean.

Bean class = name of the java class that defines the bean.

* <jsp:setProperty name = “id” property = “someProperty” value = “someValue” /

id = the name of the bean as specified in the useBean tag.

property = name of the property to be passed to the bean.

value = value of that particular property .

* <jsp:getProperty name = “id” property = “someProperty” />

8. Can you explain in brief life cycle for stateless and stateful beans?

Stateless Session Bean Lifecycle:
The stateless session bean does not become passive, so its life cycle has two stages:
1. Does Not Exist: In this stage bean has not been instantiated.
2. Method Ready pool: In the Method Ready Pool stage bean has instances in the memory of the EJB container and it is ready to serve clients. On the startup of the EJB container some instances of the bean are created and placed in the pool. EJB container creates the new instance of the Bean and then sets the session context (setSessioncontext()) and it calls the ejbCreate() method to place the bean in the Method Ready Pool stage. Container calls the ejbRemove() method to move the bean into Does Not Exist state.

Stateful Session Bean Lifecycle:
There are three stages in the lifecycle of Stateful Session Bean. These are:
1. Does Not Exist: In this the bean does not have any instance.
2. Method Ready Pool: In the Method Ready Pool stage bean has instance in the memory of the EJB container and it is ready to serve client. One instance of the Stateful Session Bean serves only one client.
3. Passive: In the Passive state the bean is passivated to conserve the resource. The passivate method is called before the instance enters the "passive" state. The instance should release any resources that it can re-acquire later in the ejbActivate() method. After the passivate method completes, the instance must be in a state that allows the container to use the Java Serialization protocol to externalize and store away the instance's state. ejbRemove or Timeout moves the bean into Does Not Exist stage.

9. When should we use an event adapter class?

A listener's class has to implement Listener interface which means all the methods of the interface must be defined in the class. This will become a tedious work when we have many methods in interface and we want to implement only one method. For this we have to define all the methods in a class. So to avoid this type of situation we use Event adapter class. The event listener interfaces that contain more than one method have a corresponding event adapter class that implements the interface and defines each method in the interface with an empty method body. Instead of implementing an event listener interface, you can extend the corresponding event adapter class and define only those methods in which you are interested.

The WindowAdapter class is an example of an event adapter class. It implements WindowListener, and defines its seven methods with empty method bodies.

10. Tell me how to send data from my Java program to a CGI program?

There are two methods of sending data from Java program to a CGI Program:

a.) The first method is that we can use CGI scripting on both client and server by using Java.

The client-side part covers using GET and POST from applets to talk to CGI programs. The server-side part covers implementing CGI programs in Java that handle GET and POST (regardless of whether the client uses HTML forms or applets), and also includes a URL decoder and CGI form parser in Java.

b.) The second method is by using Servlets and JSP.

Servlets are Java technology's answer to CGI programming. They are programs that run on a Web server and build Web pages. Java servlets are more efficient, easier to use, more powerful, more portable, and cheaper than traditional CGI and than many alternative CGI-like technologies.

Java Server Pages (JSP) is a technology that lets you mix regular, static HTML with dynamically-generated HTML. Many Web pages that are built by CGI programs are mostly static, with the dynamic part limited to a few small locations.

Download Interview PDF