Welcome to Java Interview!!
We have tried to consolidate some good quality of Tutorial / Questions on java, jsp, servlets, ejb etc. We are updating this section regularly and if you also want to contribute in this section you are welcome.
Struts Interview Question
- Q: What is MVC?
- Q: Core classes of Struts Framework?
- Q: What is ActionServlet?
- Q: What is ActionServlet?
- Q: What is role of ActionServlet??
- Q: What is the ActionForm?
- Q: What are the different methods of ActionForm?
- Q: What is ActionMapping?
- Q: What is the use of saveToken() and isTokenValid()?
- Q: What is the difference between servlet & Filter?
- Q: Difference between default namespace "" and root namespace "/" ?
- Q: Difference between applicatiopn server and web server?
Q: What is MVC?
Ans: MVC(Model View Controller) is a design pattern it decouples data access logic from business logic. Model : The model contains the core of the application's functionality. The model encapsulates the state of the application. Sometimes the only functionality it contains is state. It knows nothing about the view or controller. View: The view provides the presentation of the model. It is the look of the application. The view can access the model getters, but it has no knowledge of the setters. In addition, it knows nothing about the controller. The view should be notified when changes to the model occur. Controller:The controller reacts to the user input. It creates and sets the model.
Q: Core classes of Struts Framework?
Ans: JavaBeans components for managing application state and behavior. Event-driven development . Views
Q: What is ActionServlet?
Ans:
Q: What is ActionServlet?
Ans: It is a component that handles client requests and determines which Action will process each received request.
Q: What is role of ActionServlet??
Ans: ActionServlet performs following roles. Process user requests.Pull data from the model and pass it to the appropriate view.Select the proper view to respond to the user request.Delegates most of business work to Action classes.Initializes and cleaned all the resources.
Q: What is the ActionForm?
Ans: ActionForm is javabean which represents the form inputs containing the request parameters from the View referencing the Action bean.
Q: What are the different methods of ActionForm?
Ans: Methods are :
validate() & reset().
Q: What is ActionMapping?
Ans: Action mapping contains all the deployment information for a particular Action bean. This class is to determine where the results of the Action will be sent once its processing is complete.
Q: What is the use of saveToken() and isTokenValid()?
Ans: These method are used to avoid duplicate form submission. The Action class has a method called saveToken() whose logic is as follows:
HttpSession session = request.getSession();
String token = generateToken(request);
if (token != null)
{ session.setAttribute(Globals.TRANSACTION_TOKEN_KEY, token); }
The method generates a random token using session id and some other parameters and stores it in the session with "TRANSACTION_TOKEN_KEY" key. The Action class that renders the form invokes the saveToken() method to create a session attribute This token should be used as a hidden form field in JSP with above key.
When the client submits the form, the hidden field is also submitted. In the Action that handles the form submission the token in the form submission is compared with the token in the session by using the isTokenValid() method. The method compares the two tokens and returns a true if both are same. reset=”true” should be passed in the isTokenValid() method to clear the token from session after comparison.
If the two tokens are equal, the form was submitted for the first time. However, if the two tokens do not match or if there is no token in the session, then it is a duplicate submission and handle it in proper manner. In this way duplicate submission can be avoided by using these methods.
Q: What is the difference between servlet & Filter?
Ans: From Java EE Doc
A filter is an object that can transform the header and content (or both) of a request or response. Filters differ from web components in that filters usually do not themselves create a response. Instead, a filter provides functionality that can be “attached” to any kind of web resource. Consequently, a filter should not have any dependencies on a web resource for which it is acting as a filter; this way, it can be composed with more than one type of web resource.
The main tasks that a filter can perform are as follows:
Query the request and act accordingly.
Block the request-and-response pair from passing any further.
Modify the request headers and data. You do this by providing a customized version of the request.
Modify the response headers and data. You do this by providing a customized version of the response.
Interact with external resources.
Q: Difference between default namespace "" and root namespace "/" ?
Ans: Default Namespace: The default namespace is "" - an empty string. The default namespace is used as a "catch-all" namespace. If an action configuration is not found in a specified namespace, the default namespace is also be searched. Root Namespace: A root namespace ("/") is also supported. The root is the namespace when a request directly under the context path is received. As with other namespaces, it will fall back to the default ("") namespace if a local action is not found.
Q: Difference between applicatiopn server and web server?
Core Java Question
Q: Why wait and Notify is in Object Class and not in Thread
Ans: In Multi Threaded model Java uses locks to implement exclusive access to object & lock is related to object and not Thread. These methods work on the locks of the object i.e. the reason why these methods are in Object class and not in Thread.
Q: What is single thread model.
Ans: With Single Thread Model Servlet will serve one request at a time. In other words different reference will be creatred for different request.
Collection
There are two "groups" of interfaces used: Collection's and Map's. Note*: Always remember there is a class named Collections and Interface named Collection. The Iterable interface (java.lang.Iterable) is one of the root interfaces of the Java collection classes. The Collection interface extends Iterable, so all subtypes of Collection also implement the Iterable interface.
A class that implements the Iterable can be used with the new for-loop introduced in java 5.
Struts Hello World Applcation
In this tutorial we will learn how to create Hello world Application in Struts2.