Friday, July 30, 2010

About java finalize() method ?

Its a method defined in Object(Which is a base class of all the classes) class. Java dont have destructors instead it uses finalize() method. finalize() method not guarantee to run. For any given object finalize() method will be called only once by the garbage collector. Calling finalize() method can actually result in saving an object from deletion. If finalize() method contains the code of assigning a reference so that the object is not eligible for Garbage Collector. The second time the garbage collector will delete it. Because finalize() method run only one time for a given object.

Thursday, July 29, 2010

The list of interfaces we use in javax.servlet.http package ?

The javax.servlet.http package contains a number of classes and interfaces that describe and define the contracts between Servlet class running under a HTTP Potocol and the runtime environment provided for an instance of such a class by a conforming Servlet container.

The List of interfaces and classes in javax.servlet.http package are as follows.

HttpServletRequest
HttpServletResponse
HttpSession
HttpSessionListener
HttpServlet
Cookie

The list of interfaces we use in javax.servlet package ?

The javax.servlet package contains a number of classes and interfaces that describe and define the contracts between Servlet class and the runtime environment provided for an instance of such a class by a conforming Servlet container.

The List of interfaces and classes in javax.servlet package are as follows.

Servlet
ServletConfig
ServletRequest
ServletResponse
RequestDispatcher
Filter
FilterConfig
FilterChain
GenericFilter
ServletOutputStream
ServletInputStream
ServletException

Wednesday, July 28, 2010

What is a Java Thread and How to create them ?

Threads are lightweight processes that appear to run in parallel with your main program. Unlike a process a thread shares memory and data with the rest of the program. The word thread is a contraction of "thread of execution".An example of where threads can be useful is in printing. When you click on a print button you probably don't want the main program to stop responding until printing has finished. What would be nice is that the printing process started running "in the background" and allowed you to continue using the main portion of the program.

There are 2 ways to create a java thread.
1. By extending the class with the Thread class.
2. By implementing the Runnable interface.

In both the cases we have to implement run() method.

The best way to create the thread is to implement Runnable interface. Because, As you know java will not support multiple inheritance so, if we extend the class with the Thread class we cant extend with any other class. If we implement the class with Runnable interface, we can extend any other class

Tuesday, July 27, 2010

Calling Java Garbage Collector ?

We can request (not demand) the java garbage collector to run by using 2 ways.

1. Runtime rt = Runtime.getRuntime();
rt.gc();

2. System.gc();

Using Runtime object rt you can call more methods to know JVM memory. Examples,

rt.totalMemory(), Will return the total memory of JVM.
rt.freeMemory(), will return the free memory of JVM.

Which Objects eligible for Garbage Collection in Java ?

The Objects which are eligible for garbage collection are

1. StringBuffer sb=new stringBuffer("Hello");
System.out.println(sb); // "Hello" is NOT ELIGIBLE here for Garbage Collection.
sb=null; //"Hello" is ELIGIBLE here for Garbage Collection.


2. StringBuffer sb1=new stringBuffer("Hello");
StringBuffer sb2=new stringBuffer("goodbye");
s1=s2; //"Hello" is ELIGIBLE here for Garbage Collection.

3. Public static Date getDate(){
Date d2=new Date();
StringBuffer now=new StringBuffer(d2.toString());
System.out.println(now);
return d2;// Here d2 is NOT ELIGIBLE for Garbage Collection. But now is
ELIGIBLE for Garbage Collection
}

Wednesday, July 14, 2010

Advantages of struts2 over struts1.0

• Struts 1 supports separate Request Processors (lifecycles) for each module, but all the Actions in the module must share the same lifecycle, Struts 2 supports creating different lifecycles on a per Action basis via Interceptor Stacks. Custom stacks can be created and used with different Actions, as needed.Struts 1 integrates with JSTL, so it uses the JSTL EL, Struts 2 can use JSTL, but the framework also supports a more powerful and flexible expression language called "Object Graph Notation Language" (OGNL).(Object graph navigation language)( It is aimed to be used in J2EE applications with taglibs as expression language.)
• Struts 1 Actions have dependencies on the servlet API since the HttpServletRequest and HttpServletResponse is passed to the execute method when an Action is invoked. Struts 2 Actions are not coupled to a container. Most often the servlet contexts are represented as simple Maps.
• Struts 1 Actions are singletons and must be thread-safe since there will only be one instance of a class to handle all requests for that Action. Struts 2 Action objects are instantiated for each request, so there are no thread-safety issues.
• Struts 1 requires Action classes to extend an abstract base class. A common problem in Struts 1 is programming to abstract classes instead of interfaces. Struts 2 , we can use interfaces.
3. Interceptors
• These are request pre-processors or post processors
• Examples create-session, params, prepare, validation, exception and logger.
o Create-session, which creates HTTPSession.
o Prepare, to run prepare() method.
o Fileupload, to validate a file object
o Exception, handling exceptions.
o Roles, It checks the allowed roles and disallowed roles using intercept() method.
o Servlet-config sets the action properties, based on the different interface implemented.
o Validation, checks the action against all validation rules.