Sunday, March 17, 2024

Top 22 Apache Tomcat Interview Questions Answers for Java JEE Developers

The Apache Tomcat is one of the most popular web server used to deploy Java web application comprising HTML, JavaScript, Servlet and JSP. If you have ever worked in a Java web application you might have found yourself using Tomcat everywhere, from test environment to production. It is the defacto standard for deploying Java Web applications without EJB because using application server like WebSphere or WebLogic is both costly and superfluous for such web application. As a Java developer, working in Java and Spring based web applications, you should know some basic and advanced detail of Tomcat server to efficiently support your Java web app and troubleshoot any issue arising on request processing. 

 You should know where to look in case of any issue and you should also be able to trace how a particular request is processed in tomcat with respect to your application setup. Sometime, you will also found that Apache is fronted Tomcat and Apache web server is used to serve static files and dynamic requests are forwarded to Tomcat for further processing. 

The knowledge of both Apache and Tomcat will go a long way for Java JEE developer to secure a job and do well on Java interviews for both development and support positions. In my 20 years of career as a Java developer, I have used Tomcat multiple times and by knowing Tomcat in little bit in depth I was able to troubleshoot issues better and support my application more confidently.

They are also quite important on interviews as if you mention that you have worked in Tomcat, interviewer will most likely to ask few questions on Tomcat usage, architecture, and  working etc. Earlier, I have shared Apache HTTP Server interview questions and in this article, I'll share some of the frequently asked Apache Tomcat interview questions and their answers. 

If you don't know Tomcat well or just started to learning about Tomcat, these questions will also give you opportunity to test your knowledge and guide you which area you should improve. It will also improve your breadth of knowledge with respect to Tomcat.


22 Apache Tomcat Interview Questions and Answers

Here are the top 20 Apache Tomcat Questions from Java Developer interviews, if you have used Tomcat then you can answer most of the question but if you haven't these questions are great to review and explore Tomcat. 

1) What is Tomcat? Why do you need Tomcat?
Tomcat is a Servlet container, which provides runtime required to run a Java web application. It also provides JSP engine to parse JSP into Servlet and run it. You can see the created Servlet corresponding to each JSP in the work directory of Tomcat Home folder. In short, The Apache Tomcat software is an open source implementation of the Java Servlet, JavaServer Pages, Java Expression Language and Java WebSocket technologies.

2) What is a Connector in Tomcat?
Connector is nothing but a component inside Tomcat which is responsible for accepting web request from clients such as browsers or application sending programmatic request and passing them to Tomcat engine for processing. It act like a bridge between the network (usually HTTP and HTTPS) and Servlet container. 

While Tomcat supports different types of connect the most popular one is HTTP connector which handles HTTP requests. Another connector which is worth knowing is AJP (Apache JServ Protocol) connector which is often used for communication between Apache HTTP Server and Tomcat. 

Here is a nice Tomcat architecture diagram which also shows connectors in action:

Top 20 Apache Tomcat Interview Questions Answers for Java JEE Developers



3) What is the default port on which Tomcat listen?
The default port for Tomcat is 8080. After initialising Tomcat on your local machine, you can verify if Tomcat is running the URL: https://localhost:8080

4) What is Tomcat Coyote Connector?
Tomcat coyote is an HTTP connector based on HTTP/ 1.1 specification which receives and transport web requests to the Tomcat engine by listening to a TCP/IP port and sent request back to the requesting client.

5) Can you explain Servlet lifecycle running on Tomcat
The life-cycle of a typical servlet running on Tomcat looks like following
  • Tomcat receives a request from a client through one of its connectors
  • For processing, this request Tomcat maps this request to appropriate
  • Once the request has been directed to the appropriate servlet, Tomcat verifies that servlet class has been loaded. If it is not than Tomcat wraps the servlet into Java Bytecode, that is executable by the JVM and forms an instance of the servlet
  • Tomcat initiates the servlet by calling its init The servlet contains code that is able to screen Tomcat configuration files and act accordingly, as well as declare any resources it might require
  • Once the servlet has been started, Tomcat can call the servlet's service method to proceed the request
  • Tomcat and the servlet can co-ordinate or communicate through the use of listener classes during the servlet's lifecycle, which tracks the servlet for a variety of state changes.
  • To remove the servlet, Tomcat calls the servlets destroy method

6) Can you use SSL with Tomcat?
Yes, you can use SSL with Tomcat. By default Tomcat listens for HTTPS traffic on port 8443. See here to learn more about how to enable SSL in Apache Tomcat Server.

7) What are the different types of Connectors used in Tomcat?
There are two main connectors you will see in Tomcat:

HTTP Connectors: It has many attributes that can be changed to determine exactly how it works and access functions such as redirects and proxy forwarding

AJP Connectors: It works in the same manner as HTTP connectors, but they practice the AJP protocol in place of HTTP. AJP connectors are commonly implemented in Tomcat through the plug-in technology mod_jk

You will see this setup when Apache web server is used in front of Tomcat. Apache serves the static files and then route other request to Tomcat using AJP connector.  

Also here is a diagram showing Tomcat connector and how they work:

How Connectors work in Tomcat



8) What is Jasper in context of Tomcat?
Jasper is a Tomcats JSP engine. It parses JSP files to compile them into JAVA code as servlets
At runtime, Jasper allows to automatically detect JSP file changes and recompile them

9) How do you find the web application deployed in Tomcat?
You can see all the web application deployed in tomcat by visiting webapps directory. Each directory here represent a web application, where context is often the name of the directory here. For example if you see following directories in webapps folder

$ cd webapps
$ ls -l
ROOT
converter
recorder

then there are three webapps are deployed ROOT, converter and recorder. The ROOT is tomcat's default web application while converter and recorder are user web application. 

10) Can Tomcat listen on more than one port?
Yes, You can configure Tomcat to listen on more than one port. Basically you can create different connectors to listen on differnet ports. 

11) How do you find all the ports Tomcat is listening?
Well, by default Tomcat listens on port 8080, but you can also make Tomcat listening on other ports by creating more connectors. The best way to find all the ports on which Tomcat is listening is to first find the PID of the tomcat process and then use the netstat -nap command to find all ports on which Tomcat is listening. 

Here is the command you can use

# find the PID of Tomcat process running in Linux

$ ps -ef | grep tomcat

# suppose PID of Tomcat is 16741 then following command will list all the ports on which Tomcat is listening

netstat -nap | grep 16741 | grep LISTEN
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp 0 0 0.0.0.0:9007 0.0.0.0:* LISTEN 16741/java
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 16741/java
tcp 0 0 127.0.0.1:14082 0.0.0.0:* LISTEN 16741/java

You can see from the above output that Tomcat is listening on 9007, 8080, and 14082 ports. 


12) How do you start and stop the Tomcat in Linux?
You can start and stop a Tomcat instance by going to TOMCAT_HOME/bin directory and running ./startup.sh to start the server and ./shutdown.sh to stop the Tomcat server. If this doesn't work then you can also kill the Tomcat process by first finding its PID using ps command and later using kill -9 PID to kill the process. 

13) What is the main configuration file for Tomcat?
The server.xml is the main configuration file for Tomcat. It defines Connector, thread poos and other important properties. You can find the server.xml in TOMCAT_HOME/conf directory. There are other useful files as well e.g. context.xml or resource.xml to define database and JNDI resources. 

14) Where can you see the Tomcat logs?
The tomcat server writes into Catalina.out file, which you can find in TOMCAT_HOME/logs directory.

15) How do you find the version of Apache tomcat running?
You can run the version.sh script from TOMCAT_HOME/bin directory, it will print the apache tomcat version, JRE version, OS name and version, Architecture and JVM version as well

$ ./version.sh

16) How do you find the port on which Tomcat is listening request from Apache web server?
You can check the server.xml for AJP connector which is used to listen traffic from Apache web server. That block also includes the port on which Tomcat is listening requests from Apache:

<Connector port="9007"
protocol="AJP/1.3"
maxThreads ="50"
minSpareThreads ="20"
maxSpareThreads ="40"
acceptCount ="10"
connectionTimeout="10000"
/>

You can see that this Connector is listening on port 9007 and protocol is AJP, so this must be the port on which Tomcat is listening traffic from Apache. You can further confirm by looking at mod_jk.conf file in Apache web server

17) What is the ROOT application in Tomcat?
The ROOT is the default web application comes with Tomcat. If you install Tomcat and access the https://localhost:8080/ the page you see is served from this ROOT web application. You can find the corresponding web.xml file in the TOMCAT_HOME/conf folder and webapp itself in the TOMCAT_HOME/webapps directory. 

18) How do you override the default homepage shown by Tomcat?
The default page which is served by Tomcat is from the index.jsp in the ROOT webapp. You can edit this file to redirect the default request e.g. https://localhost:8080 to any other web application as shown below:

/webapps/ROOT]$ cat index.jsp
<%
response.sendRedirect("/demo");
%>

Now, the request will be redirected to web application listening on /demo and served there.

There is one more way to override the default homepage in Tomcat e.g. by changing the list of welcome-file tag in web.xml

<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

The files are chosen in the order listed here, if you want another file, just add that inside ROOT webapp and add an entry here. 

19) What is difference between a web server and application server? What is tomcat, a web server?
From Java perspective, a web server is the one which server static and dynamic HTML e.g. by allowing JSP and Servlet to run e.g. Tomcat or Jetty. They are also known as Servlet Container because they provide runtime environment for Servlet and keeps a pool of that. An application server e.g. WebLogic or WebSphere additional provide EJB support. You can also see my earlier article web server vs application server to learn more differences. 





20) How do you take the thread dump of Tomcat?
Since Tomcat is a Java process, you can take the thread dump by issuing kill -3 PID command in Linux. The thread status will be dumped in Catalina.out file. If your Tomcat is running on Windows then you can use jstack.exe tool from JDK to get the thread dump. If you are running on Console then you can use also use Ctrl + Break to get the thread dump. See here for steps.

21) What are the .hprof files you usually see in bin directory of Tomcat?
They are nothing but the heap dump of Tomcat which is generated when Tomcat crashes due to OutOfMemoryError. 

22) How do you take the heap dump of tomcat?
If Tomcat is running using JDK then you can use tools like jmap and jcmd to take the heap dump. You can also provide the JVM option -XX:HeapDumpOnOutOfMemoryError to instruct JVM to dump all heap in case of OOM error in Tomcat. See here for step by step guide.


That's all about some of the frequently asked questions on Tomcat web server from Java developer's perspective. As I said, Tomcat is one of the most popular web server and servlet container and it is the standard server for Java Web application without EJB. As an experienced Java JEE developer you should know basics of Tomcat server including key directories, config files, flow of requests, log files, how tomcat and apache work together, which port tomcat listens etc. If you come across any good Tomcat questions which is not in this list, you can let me know and I'll add into this list for everyone's benefit.

Some more interview questions articles for Java web developers
  • 10 Spring Framework Interview Questions with Answers (see here)
  • 12 RESTful Web Services Questions from Interviews (read here)
  • 10 Hibernate Interview Questions for Java EE developers (see here)
  • 20 Java Design Pattern Questions asked on Interviews (see here)
  • 10 popular Struts Interview Questions for Java developers (list)
  • Top 10 EJB Interview Questions and Answers (see here)
  • 10 Frequently asked Servlet Interview Questions with Answers (see here)
  • 20 jQuery Interview Questions for Java Web Developers (list)
  • Top 10 JSP Questions  from J2EE Interviews (read here)
  • Top 10 JMS and MQ Series Interview Questions and Answers (list)
  • 10 JDBC Interview Questions for Java Programmers (questions)
  • Top 10 XSLT Interview Questions with Answers (read more)

Thank you for reading this article till the end. You can also checkout official Tomcat website to learn more about Tomcat web server, its architecture and documentations. 

1 comment:

  1. Thank you for such amazing questions, I have used Tomcat in passed but didn't know answer of many questions , especially connectors etc. Now that I am reading it I am feeling more confident about Tomcat.

    ReplyDelete