Most Top 6 Struts 2 interview questions and answers for Experienced
1. Struts 2 Architecture Flow
-> 1. When the application server get started, the container loads the web.xml file.
-> 2. When the first request if made from the client browser to the server with a particular URL, the control first reaches web.xml file. It checks mapping for URL in web.xml and finds the Filter Dispatcher.
-> 3. The Filter Dispatcher determine whether to invoke an Action or not using the Action mapper and delegates the control to the Action Proxy.
-> 4. The ActionProxy takes help from Configuration Manager, which is initialized from the struts.xml. The the ActionProxy creates an ActionInvocation.
-> 5. The ActionInvocation finds which Action class to invoke for this request and discovers the intereptors associataed with the action mapping.
-> 6. ActionInvocation now invokes the intercept() method of the first interceptor in the stack. After execution of first interceptor Invoke() will check for the next interceptor.
-> 7. After the execution of all the interceptors the action class will be invoked. Finally a result string will be returned and the corresponding view will be rendered.
2. Difference Between Struts 1.x and Struts 2.x ?
3. What is Interceptors ?
Interceptor is an object that is invoked at the preprocessing and post processing of a request. In struts 2, interceptor is used to perform operations such as validation, exception handling, internationalization, displaying intermediate result etc.
4. What is Value Stack and OGNL?
Value Stack is the storage area where the application data is stored by Struts 2 for processing the client requests. The data is stored in ActionContext objects that use ThreadLocal to have values specifc to the particular request thread.
Object-Graph Navigation Language (OGNL) is a powerful Expression Language that is used to manipulate data stored on the ValueStack. As you can see in architecture diagram, both interceptors ans result pages can access data stored on ValueStack using OGNL.
5. Does Struts2 action and interceptors are thread safe?
Struts 2 Action classes are thread safe because an object is instantiated for every request to handle it.
Struts 2 interceptors are singleton classes and a new thread is created to handle the request, so it's not thread safe, we need to implement them carefully to avoid any issues with shared data.
6. What is Action Context and Action Invocation?
The ActionContext is a container of objects in which action is executed. The values stored in the ActionContext are unique per thread(i.e. ThreadLocal). So we don't need to make our action thread safe.
We can get the reference of ActionContext by calling the getContext() method of ActionContext class. It is a static factory method. For example:
ActionContext ctx = ActionContext.getContext();
The ActionInvocation represents the execution state of an action. It holds the action and interceptors objects.
The struts framework provides ActionInvocation interface to deal with ActionInvocation. It provides many methods, some of them can be used to get the instance of ValueStack, ActionProxy, ActionContext, Result etc.
===============================================================
1. The diffrence between java.lang.StringBuffer and java.lang.StringBuilder?
java.lang.StringBuffer: thread-safe, synchronized and not so faster.
java.lang.StringBuilder: faster, performs no synchronization.
2. How to handle uncaught Exception for a thread?
@FunctionalInterface
public static interface Thread.UncaughtExceptionHandler
3. The difference between [checked exceptions] and [unchecked exceptions]?
The class Exception
and any subclasses that are not also subclasses of RuntimeException
are checked exceptions. Checked exceptions need to be declared in a method or constructor's throws
clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.
Checked Exceptions should be used for expected, but unpreventable errors that are reasonable to recover from.
4. How to use non static innner class and static inner class?
non static: Out.In in = new Out().new In();
static: Out.StaticIn in = new Out.StaticIn();
5. Anonymous Inner Class?
Anonymous inner class must extends a super class or implements an interface.
ClassA a = new ClassA();
a.test(new Product(){
public double process() {
}
});
6. How to create dynamic proxy class and dynamic proxy instance?
java.lang.reflect.Proxy
interface InvocationHandler
static Class<?> getProxyClass(ClassLoader loader, Class<?>... interfaces)
static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)
InvocationHandler handler = new MyInvocationHandler(...);
Class proxyClass = Proxy.getProxyClass(Foo.class.getClassLoader(), new Class[] { Foo.class });
Constructor ctor = proxyClass.getConstructor(new Class[] { InvocationHandler.class });
Foo f = (Foo)ctor.newInstance(new Object[] { handler });
Foo f = (Foo)Proxy.newProxyInstance(Foo.class.getClassLoader(), new Class[] { Foo.class }, handler);
7. What's the difference between 3 types of class loaders?
public abstract class ClassLoader extends Object
loadClass(String name, boolean resolve)
findClass(String name)
Bootstrap ClassLoader: load core classes of Java. (java.exe -Xbootclasspath can load additional class). Not a subclass of java.lang.ClassLoader.
jre/lib/*.jar
jre/classes
Extension ClassLoader: jre/lib/ext/* or defined by java.ext.dirs System Property.
System ClassLoader: java -classpath or defined by java.class.path
java.lang.Object
java.lang.ClassLoader
java.security.SecureClassLoader
java.net.URLClassLoader
Parents of Extension ClassLoader and System ClassLoader.
8. How does interface Serializable works?
All subtypes of a serializable class are themselves serializable.
Classes that require special handling during the serialization and deserialization:
private void writeObject(java.io.ObjectOutputStream out) throws IOException
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException;
private void readObjectNoData() throws ObjectStreamException;
Designate an alternative object to be used when writing an object to the stream:
ANY-ACCESS-MODIFIER Object writeReplace() throws ObjectStreamException;
Designate a replacement when read from the stream:
ANY-ACCESS-MODIFIER Object readResolve() throws ObjectStreamException;
ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;
9. How does Buffer works?
java.nio.Buffer
capacity is the number of elements it contains.
limit is the index of the first element that should not be read or written.
position is the index of the next element to be read or written.
mark is the index to which its position will be reset when the reset method is invoked.
0 <= mark <= position <= limit <= capacity
clear() sets the limit to the capacity and the position to zero.
flip() sets the limit to the current position and then sets the position to zero.
rewind() leaves the limit unchanged and sets the position to zero.
Buffers are not safe for use by multiple concurrent threads.
b.flip().position(23).limit(42);
10. The difference between interfaces of runnable and callable<v>?
callable can have return value.
11. The difference between java.util.Collections and interface Map<K,V>?
Collections contains only items while Map contains key-value pairs.
12. The difference between interface Set<E> (java.util.HashSet<E>) and interface List<E> (java.util.ArrayList<E>)?
Unordered and No-duplicated for Set<E>, Ordered and duplicated for List<E>.
13. The difference between HashSet, TreeSet and EnumSet?
14. The difference between HashTable, HashMap, EnumMap and TreeMap?
Non thread safe, key and value can be null for HashMap.
Thread safe, key and value can't be null for HashTable.