Wednesday, November 28, 2012

Imp java Servlet qns

Question 1: In web.xml file   <load-on-startup>1</load-on-startup> is defined between <servlet></servlet> tag what does it means.

Ans: whenever we request for any servlet the servlet container will initialize the servlet and load it which is defined in our config file called web.xml by default it will not initialize when our context is loaded .defining like this <load-on-startup>1</load-on-startup> is also known as pre initialization of servlet means now the servlet for which we have define this tag has been initialized in starting when context is loaded before getting any request.

Question 2: How can we create deadlock condition on our servlet?

Ans: one simple way to call doPost() method inside doGet() and doGet()method inside doPost() it will create deadlock situation for a servlet.

Question 3: For initializing a servlet can we use constructor in place of init ().

Ans: No, we can not use constructor for initializing a servlet because for initialization we need an object of servletConfig using this object we get all the parameter which are defined in deployment descriptor for initializing a servlet and in servlet class we have only default constructor according to older version of java so if we want to pass a
Config object we don’t have parametrized constructor and apart from this servlet is loaded and initialized by container so ots a job of container to call the method according to servlet specification they have lifecycle method so init() method is called firstly.

More important Java doesn't allow interfaces to declare constructors. These kinds of servlet interview questions are quite popular on service based companies who just want to dig one level more.

Question 4: Why super.init (config) wiil be the first statement inside init(config) method.

Ans: This will be the first statement if we are overriding the init(config ) method by this way we will store the config object for future reference and we can use by getServletConfig ()  to get information about config object if will not do this config object will be lost and we have only one way to get config object because servlet pass config object only in init method . Without doing this if we call the servletConfig method will get NullPointerException.

Question5: Can we call destroy() method inside the init() method is yes what will happen?

Ans:Yes we can call like this but  if we have not override this method container will call the default method and nothing will happen.after calling this if any we have override the method then the code written inside is executed.

Question 6: How can we refresh servlet on client and server side automatically?

Ans: On client side we can use Meta http refresh and server side we can use server push.

Question 7: How can you get the information about one servlet context in another servlet?
Ans: In context object we can set the attribute which we want on another servlet and we can get that attribute using their name on another servlet.
Context.setAttribute (“name”,” value”)
Context.getAttribute (“name”)

Question 8: Why we need to implement Single Thread model in case of Servlet.

Ans: In J2EE we can implement our servlet on two different ways either by using:
1. Single Thread Model
2. Multithread Model
Depending upon our scenario, if we have implemented single thread means only one instance is going handle one request at a time no two thread will concurrently execute service method of servlet.
Example in banking account where sensitive data is handle mostly this scenario was used this interface is deprecated in Servlet API version 2.4.

As the name signifies multi thread means a servlet is capable to handle multiple requests at same time. This servlet interview question was quite popular few years back on entry level but now its loosing its shine.

Question 9: what is servlet collaboration?
Ans: communication between two servlet is called servlet collaboration which is achieved by 3 ways.
1. RequestDispatchers include () and forward() method .
2. Using sendRedirect()method of Response object.
3. Using servlet Context methods


Question 10: What is the difference between ServletConfig and ServletContext?

Ans: ServletConfig as the name implies provide the information about configuration of a servlet which is defined inside the web.xml file or we can say deployment descriptor.its a specific object for each servlet.

ServletContext is application specific object which is shared by all the servlet belongs to one application in one JVM .this is single object which represent our application and all the servlet access application specific data using this object.servlet also use their method to communicate with container.





Monday, November 19, 2012

How to develop a JDBC Application

The following are the steps to develop java appn to communicate with db server:

1. Register the JDBC driver.

2. Get the connection from db server

3. Create the Statement object to send the sql queries to db server.

4. Execute the query.

5. Close the connection.

For JDBC appn there are 2 different packages are ther

Java.Sql  ----- Interfaces
Driver, Connection, Statement, PreparedStatement, CallableStatement, ResultSet, ResultSetMetadata, DatabaseMetadata

Java.Sql ------Classes
DrverManager, Types,Date


Javax.sql------ Interfaces
DataSource,RowSet

When will be the class is loaded into JVM's memory

In the following 4 scenarios class is loaded into JVM's memory.

1. When we create an object
2. when we call JVM
3. When we access Static variables
4. when we acces static methods.


How to Organize the files in a project and how to deliver it to customer

To Organize the files in a java project:


To deliver the project to the customer, we use the following formats:

1. Jar(Java Archive) - For standalone appns

2. War (Web Archive) - For Webbased appns (ie Servlets & JSPs)

3. EAR ( Enterprize Archive) - For Enterprize appns  ( ie EJBs & Spring)

To create JAR or WAR files:

Class files path> jar -cvf  filename. Extension   ( The externsion may be jar or war as per our requirement)

To Extract JAR or WAR files:

Class files path> jar -xvf  filename. Extension   ( The externsion may be jar or war as per our requirement)

Difference between Interface and abstract class

Interface should contain all methods are "abstract".
Abstarct class may or may not contain abstract methods, concrete methods

Some Super class and sub class concepts(Polymorphism)

I f


 Some Valid obect creation....

Cone c1= new Cone();
Ctwo c2= new Ctwo();
Cone c= new Ctwo();
Object O= new Cone();
Object o= new Ctwo();


This is somewhat called as polymorphism

But from Cone object we cant access the CTwo class methods . To access the CTwo class methods,
we have to typecaste  COne object into CTwo object

Ctwo c2= (CTwo)c1;