A servlet is a Java-based software that extends a server’s capabilities. It’s a vital component of any server as they respond to container requests and for hosting web applications. To run the back-end of a website, aka, server-side, you’ll need to be familiar with servlets and how they operate.
As Java-based servers are vastly popular, learning about servlets is essential if you want to work with web servers.
If you were preparing for a developer interview, you’ve come to the right place. The following servlet interview questions will give you an idea of what to expect from the interview.
List of Top Servlet Interview Questions
- What do you mean by a Servlet?
Ans: Servlets are Java technologies, and the container that manages them is called the servlet engine. A servlet produces dynamic content and interacts with the client through Request and Response. They usually run when you click a link or perform any type of action on a website. Servlets are similar to applets, but the difference is they are processed on the server while an applet is processed on the client’s machine.
- What is a servlet container?
Ans: Servlet containers are part of a web server that offers network services. They depend on the MIME-based requests and responses. A servlet container handles servlets.
- Why do we use servlets?
Ans: Usually, we use servlets because they are platform-independent. Moreover, they are compiled into a platform-neutral byte code. You can load Java byte code dynamically into a Java-enabled web server.
- What do you mean by a filter?
Ans: Filters are reusable pieces of code that can transform the content of HTTP requests, response, and headers.
Ans: A web server handles HTTP requests from the client browser and responds with HTML response. It processes the HTTP language and runs on the HTTP protocol.
On the other hand, an application server provides additional features such as JMS Messaging support, Enterprise JavaBeans support, etc., along with the basic functionalities of a web server.
- What do you mean by a session?
Ans: Session means a period of time. In servlet, sessions are objects a servlet uses to track user interaction with the web applications across multiple HTTP requests.
- What do you mean by a servlet context?
Ans: Servlet context defines a set of methods a servlet employs to communicate with its servlet container. The servlet container creates this object to share the initial configuration or parameters to the whole application
You can get the ServletContext object by using the getServletContext() method. There is only one servlet context for an application. You can use ServletContext to share information in the whole application.
Note: This is among the most important servlet interview questions and answers.
- What does servlet-mapping mean?
Ans: Servlet mapping gives you the specific web container of which java servlet should be invoked for a particular URL. It would map the URL patterns to the servlet. When a client sends a request, the servlet container determines which application it would forward it to, and the system matches the context path to perform servlet mapping.
- How do you refresh automatically after entering new data into the database?
Ans: You can use Refresh in Server Push and Client-side to refresh automatically when entering new data into the database.
- Explain the life cycle of a servlet.
Ans: The life cycle of a servlet is divided into five steps. First, you load the servlet class then create the servlet instance. After that, you invoke the init() method, then the service method and finally the destroy() method. A servlet has three states: new, ready, and end.
The servlet would be in the new state when it is created. It goes into the ready state after you invoke the init() method. Finally, it goes into the end state when the destroy() method is invoked.
- What new features were added in Servlet 2.5?
Ans: Servlet 2.5 came with the following new features:
- J2SE 5.0 dependency
- Annotations support
- New web.xml
- Edge case clarifications
- Restrictions removal
- Which interface do all servlets implement (or should implement)?
Ans: All servlets must implement the servlet interface.
- When is a servlet loaded?
Ans: A servlet is loaded when:
- The first request is made.
- Server and autoloading startup.
- A single instance answers all the requests simultaneously, hence, saving memory.
- The Administrator loads manually.
- When is a servlet unloaded?
Ans: A servlet is unloaded when the server shuts down or the administrator unloads manually.
- What do you mean by MIME Type?
Ans: MIME stands for Multipurpose Internet Mail Extension. The MIME type is an HTTP header that gives information about what we’re sending to a browser. It helps the client in data rendering. Common MIME types are text (HTML), text (plain), images (jpeg), application (jar), etc.
To get the correct MIME type of a particular file, you can use the ServletContext getMimeType() method. It comes in handy while downloading a file through servlets from a server.
- What is the difference between the POST and GET methods?
Ans: The primary difference between the POST and GET methods is that the POST method carries the response parameters in the message body while the GET method carries the response parameters appended in the URL string.
- What do you mean by a web application?
Ans: A web application is a module that runs on the server to provide dynamic and static content to the client browser. The Apache web server supports PHP, and you can create a web application using the same.
Java supports web applications through JSPs, and servlets that run in a servlet container offer dynamic content to the client browser.
- What advantages does a servlet offer over CGI?
Ans: The CGI technology had many shortcomings. Servlets were introduced to overcome the same.
Servlets offer better performance than CGI in terms of utilising memory and processing time. They use the benefits of multithreading, where they create a new thread for every request, enhancing their speed greatly. In contrast, CGI creates a new Object for every request, which is relatively slower than the servlets’ process.
Servlets are system and platform-independent. You can run a servlet-based web application on any standard web container (Glassfish, Tomcat, and JBoss) and operating systems (Unix, Windows, Mac, etc.).
The learning curve for servlets is pretty small as you only need to handle the business logic for the application. Moreover, their container handles the servlet’s life cycle, so there’s no risk of memory leaks, garbage collection, and security.
- What do you know about the ServletConfig object?
Ans: You use the ServletConfig object to give configuration information to a specific servlet. Each servlet has a unique ServletConfig object and the servlet container instantiates it. You can give servlet init parameters through the WebInitParam annotation. To get the ServletConfig object of a servlet, you’d have to use the getServletConfig() method.
- What tasks does a servlet container perform usually?
Ans: A servlet container performs the following tasks:
- It facilitates communication between the servlets, JSPs and the web client. You don’t have to build a server socket to receive requests, parse them and generate responses because of the container. The container takes care of these tasks, allowing you to focus on the business logic.
- The servlet container handles the life cycle of servlets. It loads the servlets into memory, initialises them, invokes the necessary methods and destroys them. Servlet containers also simplify resource management by offering utilities such as JNDI.
- Servlet containers create new threads for every request and give servlets request and response objects. This way, you don’t have to initialise the servlets for every request, saving a lot of memory and time.
- Servlets compile JSPs and convert them to servlets to manage them like servlets, simplifying your task.
- What do you mean by the request dispatcher?
Ans: You use the RequestDispatcher interface to forward requests to another resource. This resource could be another servlet in the application, HTML or JSP. You can use the RequestDispatcher interface to add the content of another resource to your response. It is an excellent solution for inter-servlet communication.
You can define two methods in the RequestDispatcher interface:
- The void forward method. It forwards the request from one servlet to a different resource.
- The void include method. It adds the content of another resource to your response.
- What is the difference between ServletContext and ServletConfig?
Ans: The ServletContext is a unique object for the entire application, while ServletConfig is a unique object for a servlet. You can use the ServletContext to give init parameters across the complete application, which all servlets can utilise. On the other hand, you can use the ServletConfig to provide init parameters only to a specific servlet.
You can set attributes in ServletContext, which other servlets can use, but you can’t choose attributes in ServletConfig.
Note: This is among the most crucial servlet interview questions and answers. You should prepare it carefully.
- What do you know about servlet attributes?
Ans: Servlet attributes help you in inter-servlet communication. You can set, get and remove attributes in the application. Servlet attributes have three scopes, request scope, session scope, and application scope, namely. They are different from init parameters.
- How would you call a servlet from another servlet?
Ans: You can use the RequestDispatcher forward() to forward the request processing to a different servlet. If you want to add another servlet output to a response, you can use the RequestDispatcher include() method.
The above servlet interview questions and answers should have helped you understand what kind of questions an interview would ask you. We recommend bookmarking this page so you can come back to it later and revise the questions several times.
Try to remain calm and composed during the interview. It would help you listen closely to the questions and answer them accordingly. You can check out our Executive PG Programme in Software Development with Specialisation in Full Stack Development to learn more advanced concepts about Java and its related subjects.