In this post, I'm sharing interview questions asked in 2nd round of Aristocrat for the position of Java Principal Architect.
You can also go through my other posts:
Question 1:
What is the purpose of using @ServletComponentScan?
Answer:
We add @ServletComponentScan to enable scanning for @WebFilter, @WebServlet and @WebListener.
It is used on the main SpringBootApplication.java class.
Embedded containers do not support @WebServlet, @WebFilter and @WebListener. That's why spring has introduced @ServletComponentScan annotation to support some dependent jars which use these 3 annotations.
To use @ServletComponentScan, we need to use spring boot with version 1.3.0 or above.
And we also need to add spring-boot-starter-parent and spring-boot-starter-web dependencies.
pom.xml file:
<parent>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-starter-parent</artifactId>
<version> 1.5.1.RELEASE </version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.1.RELEASE</version>
</dependency>
</dependencies>
Question 2:
How does Servlet work and what are the lifecycle methods?
Answer:
A servlet is a class that handles requests, processes them and reply back with a response.
e.g. we can use a servlet to collect input from user through an HTML form, , query records from a database and create web pages dynamically.
Servlets are under the control of another java application called servlet container. When an application running in a web server receives a request , the server hands the request to the servlet container - which in turn passes it to the target servlet.
Maven dependency for using servlet is given below:
<dependency>
<groupId> javax.servlet</groupId>
<artifactId> javax.servlet-api</artifactId>
<version> 3.1.0</version>
</dependency>
Lifecycle methods of servlet are described below:
init(): The init method is designed to be called only once. If an instance of servlet does not exist, the web container does the following:
- Loads the servlet class
- Create an instance of the servlet class
- Initializes it by calling the init() method.
The init() method must be completed successfully before the servlet can receive any requests.
The servlet container cannot place the servlet in service if the init() method either throws a ServletException or does not return within a time period defined by the web server.
public void init throws ServletException{
// code here
}
service(): This method is only called after the servlet's init() method has completed successfully.
The container calls the service method to handle the requests coming from the client, interprets the HTTP request type (GET, PUT, POST, DELETE etc.) and calls doGet(), doPut(), doPost() and doDelete() methods.
public void service(ServletRequest req, ServletResponse response) throws ServletException{
}
destroy(): It is called by the servlet container to take the servlet out of the service.
This method is only called after all the threads in the service have exited or a time period has passed.
After the container calls this method, it will not call the service method again on the servlet.
public void destroy(){
}
Question 3:
What application server have you used and what are the benefits of that?
Answer:
I have used Weblogic application server.
Weblogic server provides various functionalities:
- Weblogic server provides support for access protocols like HTTP, SOAP etc.
- It also provides data access and persistence from database server.
- It also supports SQL transactions for data integrity.
- It also provides security.
So means, when we use Weblogic server, we do not have to care about protocol, security, database transactions, data integrity etc. All these are handled by Weblogic server. We can focus on business logic.
That's all for this post.
Thanks for reading!!
No comments:
Post a Comment