Friday, July 6, 2012

Tomcat Interview Questions & Answers

Q: - What is Tomcat?

Tomcat is a Java Servlet container and web server from Jakartha project of Apache software foundation. A web server sends web pages as response to the requests sent by the browser client. In addition to the static web pages, dynamic web pages are also sent to the web browsers by the web server. Tomcat is sophisticated in this respect, as it provides both Servlet and JSP technologies. Tomcat provides a good choice as a web server for many web applications and also a free Servlet and JSP engine. Tomcat can be used standalone as well as behind other web servers such as Apache httpd.

Q: - How do I configure Tomcat to work with IIS and NTLM?

Follow the standard instructions for when the isapi_redirector.dll

Configure IIS to use "integrated windows security"

In server.xml, make sure you disable tomcat authentication:

<Connector port="8009" enableLookups="false" redirectPort="8443" protocol="AJP/1.3" tomcatAuthentication="false" />
Q: -I stored a cookie on the browser; why can't I see it?

First, check to make sure you spelled the cookie name correctly. Next, make sure the browser has cookies enabled. Also, if you specified a root path for the cookie, make sure that the JSP or servlet reading the cookie is in that path. Remember, too, that if you don't give the cookie a specific expiration time, the cookie will vanish when the user shuts the browser down.

Q: - Why is a new object created whenever I call jsp:useBean?

You probably forgot to specify a scope for the bean. Remember, the default scope for a bean is page, and all beans with page scope disappear when the page finishes executing.

Q: - When do I use . and when do I use []?

Although you can use these operators interchangeably, it is a good idea to use an operator that indicates the kind of data being accessed. For example, if you are accessing a bean property, use the . operator. If you are accessing a map value or an array index, use the [] operator. There are plenty of times when you break this rule, especially in cases where you want a map to look like it is a bean, you want things to make sense to the next person who reads your code. You may know the types of all the variables, but the next person may not.

Q: - What is Jasper?
Jasper is a program to read the .class files in binary format. This program can generate ASCII files , which can be used along with Jasmin Assembler. Jasper is intended for generating input into a class file browser which can produce the hierarchy of inheritance and composition maps from the .class files.
Q: - Can I set Java system properties differently for each webapp?

No. If you can edit Tomcat's startup scripts, you can add "-D" options to Java. But there is no way to add such properties in web.xml or the webapp's context.

Q: - Why do I get a ClassNotFoundException when I try to use the ShoppingCart class?

The JSP engine probably can't see the class in its classpath. Tomcat uses the system classpath, so if ShoppingCart.class is visible somewhere in the system classpath, they should see it.

Q: - Explain the concepts of Tomcat Servlet Container.

- Tomcat Servlet Container is a servlet container. The servlets runs in servlet container.
- The implementation of Java Servlet and the Java Server Pages is performed by this container.
- Provides HTTP web server environment in order to run Java code.
- Reduces garbage collection
- Native Windows and Unix wrappers for platform integration

Ques 1. How do you create multiple virtual hosts?

Ans. If you want tomcat to accept requests for different hosts e.g., then you must

1. create ${catalina.home}/www/appBase , ${catalina.home}/www/deploy, and

2. add a host entry in the server.xml file
<Host appBase=\"www/appBase\" name=\"\"/>

3. Create the the following file under conf/Catalina/
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<Context path=\"/\" docBase=\"www/deploy/mywebapp.war\" reloadable=\"true\" antiJARLocking=\"true\"> </Context>
Add any parameters specific to this hosts webapp to this context file

4. put your war file in ${catalina.home}/www/deploy

When tomcat starts, it finds the host entry, then looks for any context files and will start any apps with a context

To add more sites just repeat and rinse, all webapps can share the same war file location and appbase

Ques 2. How will you load properties file?

Ans. * Use a ResourceBundle. See the Java docs for the specifics of how the ResourceBundle class works. Using this method, the properties file must go into the WEB-INF/classes directory or in a jar file contained in the WEB-INF/lib directory.

* Another way is to use the method getResourceAsStream() from the ServletContext class. This allows you update the file without having to reload the webapp as required by the first method. Here is an example code snippet, without any error trapping:

// Assuming you are in a Servlet extending HttpServlet
// This will look for a file called "/more/" relative
// to your servlet Root Context
InputStream is = getServletContext().getResourceAsStream("/more/");
Properties p = new Properties();

Ques 3. Can I set Java system properties differently for each webapp?

Ans. No. If you can edit Tomcat's startup scripts, you can add "-D" options to Java. But there is no way to add such properties in web.xml or the webapp's context.

Ques 4. How do I configure Tomcat to work with IIS and NTLM?

Ans. Follow the standard instructions for when the isapi_redirector.dll

Configure IIS to use "integrated windows security"

In server.xml, make sure you disable tomcat authentication:

<Connector port="8009" enableLookups="false" redirectPort="8443" protocol="AJP/1.3" tomcatAuthentication="false" />

Ques 5. How can I access members of a custom Realm or Principal?

Ans. When you create a custom subclass of RealmBase or GenericPrincipal and attempt to use those classes in your webapp code, you\'ll probably have problems with ClassCastException. This is because the instance returned by request.getUserPrincipal() is of a class loaded by the server\'s classloader, and you are trying to access it through you webapp\'s classloader. While the classes maybe otherwise exactly the same, different (sibling) classloaders makes them different classes.

This assumes you created a My``Principal class, and put in Tomcat\'s server/classes (or lib) directory, as well as in your webapp\'s webinf/classes (or lib) directory. Normally, you would put custom realm and principal classes in the server directory because they depend on other classes there.

Here\'s what you would like to do, but it throws ClassCastException:

MyPrincipal p = request.getUserPrincipal(); String emailAddress = p.getEmailAddress(); Here are 4 ways you might get around the classloader boundary:

1) Reflection

Principal p = request.getUserPrincipal();
String emailAddress = p.getClass().getMethod(\"getEmailAddress\", null).invoke(p, null);
2) Move classes to a common classloader

You could put your custom classes in a classloader that is common to both the server and your webapp - e.g., either the \"common\" or bootstrap classloaders. To do this, however, you would also need to move the classes that your custom classes depend on up to the common classloader, and that seems like a bad idea, because there a many of them and they a core server classes.

3) Common Interfaces

Rather than move the implementing custom classes up, you could define interfaces for your customs classes, and put the interfaces in the common directory. You\'re code would look like this:

public interface MyPrincipalInterface extends {
public String getEmailAddress();

public class MyPrincipal implements MyPrincipalInterface {
public String getEmailAddress() {
return emailAddress;

public class MyServlet implements Servlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
MyPrincipalInterface p = (MyPrincipalInterface)request.getUserPrincipal();
String emailAddress = p.getEmailAddress();
Notice that this method gives you pretty much the webapp code you wanted in the first place

4) Serializing / Deserializing

You might want to try serializing the response of \'request.getUserPrincipal()\' and deserialize it to an instance of [webapp]MyPrincipal.

Ques 6. How do I override the default home page loaded by Tomcat?

Ans. After successfully installing Tomcat, you usually test it by loading http://localhost:8080 . The contents of that page are compiled into the index_jsp servlet. The page even warns against modifying the index.jsp files for this reason. Luckily, it is quite easy to override that page. Inside $TOMCAT_HOME/conf/web.xml there is a section called <welcome-file-list> and it looks like this:

The default servlet attempts to load the index.* files in the order listed. You may easily override the index.jsp file by creating an index.html file at $TOMCAT_HOME/webapps/ROOT. It's somewhat common for that file to contain a new static home page or a redirect to a servlet's main page. A redirect would look like:


<meta http-equiv="refresh" content="0;URL=">


This change takes effect immediately and does not require a restart of Tomcat.

Ques 7. How do I enable Server Side Includes (SSI)?

Ans. Two things have to be done for tomcat to aknowledge SSI scripts:

1. Rename $CATALINA_BASE/server/lib/servlets-ssi.renametojar to $CATALINA_BASE/server/lib/servlets-ssi.jar.

2. Uncomment the section of web.xml found in $CATALINA_BASE/conf/web.xml that deals with SSI. it looks like this when it is uncommented:


Ques 8. How do I use DataSources with Tomcat?

Ans. When developing J2EE web applications, the task of database connection management can be daunting. Best practice involves using a J2EE DataSource to provide connection pooling, but configuring DataSources in web application servers and connecting your application to them is often a cumbersome process and poorly documented.

The usual procedure requires the application developer to set up a DataSource in the web application server, specifying the driver class, JDBC URL (connect string), username, password, and various pooling options. Then, the developer must reference the DataSource in his application's web.xml configuration file, and then access it properly in his servlet or JSP. Particularly during development, setting all of this up is tedious and error-prone.

With Tomcat 5.5, the process is vastly simplified. Tomcat allows you to configure DataSources for your J2EE web application in a context.xml file that is stored in your web application project. You don't have to mess with configuring the DataSource separately in the Tomcat server.xml, or referencing it in your application's web.xml file. Here's how:

Install the JDBC Driver Install the .jar file(s) containing the JDBC driver in Tomcat's common/lib folder. You do not need to put them in your application's WEB-INF/lib folder. When working with J2EE DataSources, the web application server manages connections for your application.

Create META-INF/context.xml In the root of your web app directory structure, create a folder named META-INF (all caps). Inside that folder, create a file named context.xml that contains a Resource like this:

<?xml version="1.0" encoding="UTF-8"?>


<Resource name="jdbc/WallyDB" auth="Container"
type="javax.sql.DataSource" username="wally" password="wally"


This example shows how to configure a DataSource for a SQL Server database named mytest located on the development machine. Simply edit the Resource name, driverClassName, username, password, and url to provide values appropriate for your JDBC driver.

Access the DataSource in Your Application From a Servlet
Here's how you might access the data in a servlet:

InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup("java:comp/env/jdbc/WallyDB");
Connection c = ds.getConnection();
Notice that, when doing the DataSource lookup, you must prefix the JNDI name of the resource with java:comp/env/

Ques 9. What is Tomcat?

Ans. 1) Tomcat is a Java Servlet container and web server from the Jakarta project of the Apache software foundation.
2) A web server responds with web pages to requests from client browsers.
3) Web servers can provide dynamic content based on the requests sent by the user.
4) Tomcat is very good at this because it provides both Java servlet and JavaServerPages (JSP) technologies.
5) Tomcat can also be used as a web server for many applications even if free servlet and JSP engine is wanted.
6) It can be used standalone or used behind traditional web servers such as Apache httpd, with the traditional server serving static pages and Tomcat serving dynamic servlet and JSP requests.

Ques 10. What is Jasper?

Ans. 1) Jasper is Tomcat's JSP Engine. Tomcat 5.x uses Jasper 2, which is an implementation of the Sun Microsystems's JavaServer Pages 2.0 specification.
2) Jasper parses JSP files to compile them into Java code as servlets (that can be handled by Catalina).
3) At runtime, Jasper is able to automatically detect JSP file changes and recompile them.

Ques 11. Explain the concepts of Tomcat Servlet Container.

Ans. 1) A servlet container is a specialized web server that supports servlet execution.
2) It combines the basic functionality of a web server with certain Java/servlet specific optimizations and extensions (such as an integrated Java runtime environment, and the ability to automatically translate specific URLs into servlet requests).
3) Individual servlets are registered with a servlet container, providing the container with information such as the functionality, the URL used for identification.
4) The servlet container then initializes the servlet as necessary and delivers requests to the servlet as they arrive.
5) Many containers can dynamically add and remove servlets from the system, allowing new servlets to quickly be deployed or removed without affecting other servlets running from the same container.
6) Servlet containers are also referred to as web containers or web engines.

Ques 12. What is WebServers? Why it is used?

Ans. Transaction with HTTP request and HTTP response is called webserver.
Using the internet listening the HTTP request and providing the HTTP response is also called webserver.It gives only html output.It will not process business logic .They can provide Http server.They are static.

Ques 13. What is different between webserver and application server?

Ans. The basic difference between a web server and an
application server is Webserver can execute only web applications i,e servlets and JSPs and has only a single container known as Web container which is used to interpret/execute web
Application server can execute Enterprise application, i,e (servlets, jsps, and EJBs) it is having two containers
1. Web Container(for interpreting/executing servlets and jsps)
2. EJB container(for executing EJBs). it can perform operations like load balancing , transaction demarcation etc.

Ques 14. Question : How to communicate between two webservers in two diff systems?

Ans. By using plug module .

Ques 15. How web server handles multiple requests for same action class(Struts) concurrently?

Ans. Struts or any webserver makes new thread for each new request. so multiple request is served with new request object.

Ques 16. Suppose when we are starting startup.bat file of Tomcat server it is not started. DOS window appears for a Second only. What we need do?

Ans. Your set up might have been not done well.
Make sure you have added tomcat root directory path in the CATALINA_HOME environment variable and added the bin path in the path variable.


  1. very nice. some more <a href="'>tomcat interview questions and answers</a> from me.

  2. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.
    I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging.
    If anyone wants to become a Java developer learn from Java EE Online Training from India.
    or learn thru Java EE Online Training from India .
    Nowadays Java has tons of job opportunities on various vertical industry.

  3. CIITN is located in Prime location in Noida having best connectivity via all modes of public transport. CIITN offer both weekend and weekdays courses to facilitate Hadoop aspirants. Among all Hadoop Training Institute in Noida , CIITN's Big Data and Hadoop Certification course is designed to prepare you to match all required knowledge for real time job assignment in the Big Data world with top level companies. CIITN puts more focus in project based training and facilitated with Hadoop 2.7 with Cloud Lab—a cloud-based Hadoop environment lab setup for hands-on experience.

    CIITNOIDA is the good choice for Big Data Hadoop Training in NOIDA in the final year. I have also completed my summer training from here. It provides high quality Hadoop training with Live projects. The best thing about CIITNOIDA is its experienced trainers and updated course content. They even provide you placement guidance and have their own development cell. You can attend their free demo class and then decide.

    Hadoop Training in Noida
    Big Data Hadoop Training in Noida

  4. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.
    Advanced AWS Training in Bangalore | Best Amazon Web Services Training Institute in Bangalore
    Advanced AWS Training Institute in Pune | Best Amazon Web Services Training Institute in Pune
    Advanced AWS Online Training Institute in india | Best Online AWS Certification Course in india
    AWS training in bangalore | Best aws training in bangalore

  5. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.
    microsoft azure training in bangalore
    rpa interview questions and answers
    automation anywhere interview questions and answers
    blueprism interview questions and answers
    uipath interview questions and answers
    rpa training in bangalore

  6. Awesome article. It is so detailed and well formatted that i enjoyed reading it as well as get some new information too.
    Best Devops Training in pune
    Devops Training in Bangalore
    Power bi training in Chennai

  7. Great content thanks for sharing this informative blog which provided me technical information keep posting.
    python Course in Pune
    python Course institute in Chennai
    python Training institute in Bangalore

  8. Wow it is really wonderful and awesome thus it is veWow, it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot.

    python training in bangalore

    python courses in bangalore

    python classes in bangalore

    python training institute in bangalore

    python course syllabus

    best python training

    python training centers

  9. This is the exact information I am been searching for, Thanks for sharing the required infos with the clear update and required points. To appreciate this I like to share some useful information.

    aws training in bangalore

    aws courses in bangalore

    aws classes in bangalore

    aws training institute in bangalore

    aws course syllabus

    best aws training

    aws training centers

  10. Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.
    AWS training in chennai | AWS training in annanagar | AWS training in omr | AWS training in porur | AWS training in tambaram | AWS training in velachery

  11. Thanks for posting the best information and the blog is very helpful.python course in Bangalore

  12. Great, thanks for sharing this post.Much thanks again. Awesome.
    best online java corse
    java online classes

  13. Are you looking for the best Azure Training in Chennai? Here is the best suggestion for you, Infycle Technologies the best Software training institute in Chennai to study Azure platform with the top demanding courses such as Graphic Design and Animation, Cyber Security, Blockchain, Data Science, Oracle, AWS DevOps, Python, Big data, Python, Selenium Testing, Medical Coding, etc., with best offers. To know more about the offers, approach us on +91-7504633633, +91-7502633633