Tomcat Part II

48) Can Tomcat be configured to interpret all, or selected, .html files within a given context as JSP? Or, do JSP files have to end with a .jsp extension?
yes you can do that by modifying the web.xml file. You will have to invoke the org.apache.jasper.runtime.JspServlet for all the requests having extension .html. You can do that by changing the Servlet mapping code:



And comment out the following block



49) Is there any way I can assign different values of CLASSPATH to different contexts?
One way to get the functional equivalent is to put the files in question in either "/WEB-INF/classes" or "/WEB-INF/lib", depending on whether you are talking about a .class or .jar. The CLASSPATH for a given webapp will include WEB-INF/classes and all the .jar files found in WEB-INF/lib. Say you had utils.jar and form-taglib.jar in WEB-INF/lib, the resulting classpath would look like:

50) How do I change the port from 8080 to 80?
Open server.xml in %TOMCAT_HOME%\conf directory and edit the element.
For SimpleTcpConnector, change the sub-element "port" value to 80. Yes, You got it!
51) How to deploy a servlet that includes javax.mail.* on Tomcat?

Just place the mail.jar and activation.jar files (and possibly the pop.jar file if you are using POP3) in the CLASSPATH of the web server. The simplest way to do this is to rely on the Java extensions framework. Add them to the JAVA_HOME/jre/lib/ext directory. Just copy the files there. Otherwise, set the CLASSPATH environment variable before starting Tomcat. The startup scripts retain the existing CLASSPATH, adding the Tomcat-specific stuff to the end.

52) What is the difference between apache webserver, java webserver and tomcat server?

Apache is an HTTP server written in C that can be compiled and run on many platforms. See
Java WebServer is an HTTP server from Sun written in Java that also supports Servlets and JSP.
Tomcat is an open-source HTTP server from the Apache Foundation, written in Java, that supports Servlets and JSP. It can also be used as a "plug-in" to native-code HTTP servers, such as Apache Web Server and IIS, to provide support for Servlets (while still serving normal HTTP requests from the primary, native-code web server).
53) What is Catalina?
Catalina is the name given to the Tomcat 4.0 servlet engine component, in the same way that the JSP compiler/runtime component is called Jasper.
54) Where do I install the JDBC driver ZIP files, JAR files & DLLs? How do I ensure they're in Tomcat's CLASSPATH?
Since version 3.2, all the JARS present in the %TOMCAT_HOME%/lib directory are automatically appended to Tomcat's classpath, but only the jars are appended. To have a ZIP automatically appended rename it to *.jar ( as a jar is only a renamed zip ) and voila....
[There are two other options:
1. Put the jar file in your webapp's WEB-INF/lib/ directory. This set of JARs is added to the classpath for your webapp only. Be careful, however: if a different driver for the same JDBC URL is already on the system classpath, then it may (may!) be loaded instead of yours. (Whether it is or not depends on whether that driver has already been loaded yet.)
2. Change the Tomcat CLASSPATH environment variable, or "-classpath" command-line option, to contain your JAR or ZIP. This is set in the Tomcat startup script, tomcat.bat/ Read that file for details on how to edit it.
55) Where do System.out and System.err go in Tomcat?

By default, they will go to the console where the tomcat is started. You have to modify the startup scripts to redirect them to appropriate files.
On the other hand, if you are running Tomcat as an NT Service, you can modify the conf/ file and set wrapper.stdout and wrapper.stderr properties to point to your log files.
56) I've written a servlet that was using JServ and now I upgrade to use Tomcat. What would I need to change?
The only thing you should have to change code-wise is whatever has been changed in the Servlet API between the API used in JServ and the api used it Tomcat... just a couple that come to mind are
encodeURL() is now encodeUrl() and encodeRedirectURL is now encodeRedirectUrl(). You can get the entire list by looking at the specs for the servlet APIs on the java site. There is usually a section in the specs that is "What's New in the API".
57) Can I do servlet chaining in Tomcat? If so, how?
Not yet.
The next version (2.3) of the Servlet Spec will allow something like servlet chaining. It's called "filters."
58) Can I use a Generic SAX/DOM XML parser for Tomcat instead of parser.jar? I tried using Xerces and get class not found errors for sun.xxxx parser during Tomcat initialization.

Try to configure Xerces as customized SAX/DOM implementation by system properties. By default jaxp uses com.sun.xml.parser.DocumentBuilderFactoryImpl and com.sun.xml.parser.SAXParserFactoryImpl (this is why you get ClassNotFoundException if you miss parser.jar from the classpath).


59) Under what circumstances will a servlet be reloaded?
That depends on the Servlet container.
Most of the Servlet containers reload the servlet only it detects the code change in the Servlet, not in the referenced classes.
In Tomcat's server.xml deployment descriptor, if you have mentioned

trusted="false" >

The reloadable = true makes the magic. Every time the Servlet container detects that the Servlet code is changed, it will call the destroy on the currently loaded Servlet and reload the new code.
But if the class that is referenced by the Servlet changes, then the Servlet will not get loaded. You will have to change the timestamp of the servlet or stop-start the server to have the new class in the container memory.
60) Why do I get the compiler error "package javax.servlet does not exist" or "package javax.servlet not found in import"?

Try this: Assume that you have tomcat location like:
at the command promt type:
set classpath=c:\tomcat\lib\servlet.jar
61) How can you set the session timeout in tomcat higher?
Does anybody know how you can set the session time in tomcat higher? Now it is 30 minutes, but I want to set it higher.
You can specify the timeout of a session in the deployment descriptor of your web application (web.xml):



The number within the session-timout element must be expressed in minutes
62) Is JavaScript supported as a JSP scripting language under Tomcat?

In short, no.
Putting things very very simply, Tomcat is essentially a Java Servlet Compiler, taking the .jsp file you write and translating it to a java servlet that runs in the background.
Java, according to the Apache development group that wrote Tomcat (known as the Jakarta group) conforms solely to the JSP 1.2 / Servlet 1.1 specification by Sun Microsystems. When they wrote Tomcat, they only had in mind to use Java as the "scripting language". In fact, Tomcat is built with java, thereby heavily integrating the technology.
I hope you know this, but JavaScript and Java are two entirely different languages (some things are similar, but most are not). The two technologies are by no means interchangeable, and this intuitively applies to Tomcat. To the best of my knowledge the Apache Tomcat developmental teams have no current plans to integrate other scripting technologies into the software (for many reasons, of which I won't go into here for brevity).
However, you may include JavaScript within the .jsp file (not within jsp interpretable <% %> tags), and it will be output to the client browser for interpretation just like in an HTML page. The key here is to realize that it has no interaction/functionlity with the server before it is sent to the client.
63) Does TOMCAT provide JDBC Connection Pooling ?
Tomcat (talking about version 3.x.x) is the reference implementation of the webapp part of J2EE (ie, no EJB). In this spec connection pooling is not contemplated.
64) How do I include a file in a JSP page?

Yes, there are two different ways to do that:
The first one is done using the 'include' directive:
<%@ include file="relativeURL" %>
In this case the given page will be statically included inside the the jsp page.

The second include is done using the jsp tag 'include':
}" flush="true" />
With this syntax, the page will be included statically or dinamically, depending on the page itself.
If the page to be included is dynamic, parameters can be passed to it using this syntax:
}" flush="true" >
value="{parameterValue | <%= expression %>}" />

Note: with Jsp 1.1 the "flush" parameter can only be true.

65) Where to save the Images, HTML, and other static data in Tomcat?

Images and HTML shall be put inside the context of your application:
 somewhere inside /jakarta-tomcat-3.2.1/webapps/your_context/
 somewhere inside the war file of your application
war file must be put inside /jakarta-tomcat-3.2.1/webapps/
An example of tree of a web application (in a .war archive) can be:
| |---web.xml
| |---jsp
| | |---taglib (.tld)
| |---classes
| |---(.class/.properties)
| |---(.jsp/.html/.shtml)
| |---(.gif/.png/.jpeg)
check examples in jakarta-tomcat-3.2.1/webapps/
66) How do I find out Tomcat's home directory from inside a running servlet?

When Tomcat is executed (and you can check both and tomcat.bat in the tomcat's bin directory) the property 'tomcat.home' is set using -D. This means that you should be able to access it using System.getProperty("tomcat.home").

67) How to read a file from the WAR or webapp? If I have a configuration file, say an XML file, stored inside the WAR, how do I open it from a servlet? I.e.,
InputStream in = new FileInputStream(????);

The WAR file is automatically opened by the servlet container during the startup... at least this is what the servlet specs are saying (and it is happening with both Resin and Tomcat). Once you reach the servlet just use ServletContext.getRealPath() to get the file.
For example if the file is in a 'template' dir under the root of the context, use


68) How and where do I change the option for JVM heap memory in Tomcat?
If you look into the or tomcat.bat under the bin directory of tomcat, you can see an environment variable called TOMCAT_OPTS.
You can use it for passing all the JVM specific parameters, like the -X or -D.
You can set it directly in the file, or set it in the environment before calling it. For example in unix you can do:
export TOMCAT_OPTS=" -Xms15M -Xmx15M" start
69) Where are the servlet source files for JSPs compiled in Tomcat?

[JSP is first translated into servlet than compiled. So where are those servlet .java or .class files for JSP examples in Tomcat?]
They are located in the work directory of the TOMCAT_HOME, under a directory that is normally called localhost_%20.
The source for JSPs from the examples webapp should be something like localhost_8080%20examples.
70) What is the difference between startup.bat, shutdown.bat, and tomcat.bat?
tomcat.bat takes a parameter, "start" or "stop".
Running "startup" is the same as running "tomcat start". Running "shutdown" is the same as running "tomcat stop".
71) How do I assign the user's role without using a login form?
Basically the concept is that you can use the standard http authentication schemes (i.e.: basic, digest). This means that you can authenticate a user among a database (or, for example, another source) without developing html login forms.

72 ) What's the difference between PoolTcpConnector and SimpleTcpConnector ?

The main difference is that the Pool connector will create a pool of simple connectors, allowing the server to answer to more requests at the same time, while a simple connector can anwer only a response at a time.
The simple connector hasn't been designed to be used unless you know that you will process only a request at a time.
73) Do I really need to build a complete 'webapp' just to add a simple 'Helloworld' servlet to the server?
Not at all. You can put a single servlet anywhere on your hard disk but you will have to configure your path in server.xml and servlet alias in web.xml as usual.
Let me be a bit more descriptive.
If I have my HelloWorld servlet under c:/foo/hello directory, i will set my path in server.xml like

And then in web.xml :
that tomcat considers while starting and you can start your servlet.
74) Why does Tomcat's first servlet request take so long?
Sounds like it is the SecureRandom class initializing. Please disable this (set the value in server.xml to "java.util.Random") and see if it helps.
However, be warned that this method leaves your sessions open to attack. It would take a dedicated hacker, but if you're storing any sensitive data, this is a big no-no. So put it back to SecureRandom when you're done testing this.
75) How do I disable port 8080? I want Tomcat to run only through my Web server, not directly through Tomcat.

Go to the TOMCAT_HOME directory and edit server.xml, find :
and wrap it in a comment so it's not interpreted. That's it !
76) How do I prevent users from viewing the contents of my WEB-INF directory?

Servlet Engines (e.g. Resin or Tomcat) should not allow directory browsing of WEB-INF. Tomcat 3.2, for example, had that same issue and they have fixed it in Tomcat 3.2.1. This was happening when Tomcat was used standalone (not just as a container, but even as a web server).
If this problem happens with a specific version of a standalone Resin, you should consider to try the latest version (I'm running 1.2.5) and, eventually, send a bug to Caucho.
Consider that this issue should happen when using the container in standalone mode. When the container (Resin, Tomcat or abother one) is used just for serving servlet & jsp behind a more reliable and complete web server, like Apache, the problem is on Apache, that is serving everything else. When you ask for WEB-INF, in fact, Apache doesn't even connect to Tomcat, there is no reason.
So, if this is your scenario, you should add lines like these:

AllowOverride None
Deny From All

Inside the virtual host or whatever you think is appropriate and, obviously, changing "/WEB-INF" with the appropriate context.
77) What is the password for the Tomcat admin webapp?

You have to modify tomcat-users.xml in your $TOMCAT_HOME/conf directory to add an administrative role as in following example:

Then you can login with scott/tiger.
78) Do I need to create my own web.xml file?
No, you don't. But it's normally better to.
Normally the servlet container knows how to execute servlets and what to call for executing jsps, so you can survive without defining your own web.xml file.
There are many advantages of the web application descriptor, though. For example if you have a servlet that it's part of a package, you would be able to assign it a name and a mapping, so instead of calling it, for example, with /servlet/, you can make it /MyServlet. Or, you can use a servlet name that it's common and when you change the class that you are using, you don't have to change the code or the pages that are using it.
Plus there are other interesting features, like parameter passing, securities and so on.
But, again, to answer to your question: yes, you don't need to create a web.xml file.
79) Can the web application reside on a machine other than where Tomcat is running?

If by "web application" you mean the set of jsps, servlets, classes, and all the other required files, I'm afraid not. [Tomcat must be able to read the files from a locally mounted file system.]
What you can do, though, is to have Tomcat running on one machine and the web server running on another, but I don't think that you can have the files of the application be read through the net without some sort of file server mounting solution like ntfs, samba or similar.
80) Can anyone tell me the advantages and disadvantages between Tomcat, JRun, and JServ for Apache Web Server?

Product Pros Cons
Tomcat  Free as in both beer and speech
 Active open-source development effort
 Very current in terms of servlet API compliance  Not the fastest implementation on the planet
 You're on your own for support
JRun  Free as in beer for developers
 Relatively speedy
 Supported by Allaire (Macromedia)  Costs money for production use
 Versions that I've used (pre-3.0) had lots of configuration headaches
 Slightly behind the times with updates (compared to open source projects)
Apache JServ Free as in both beer and speech  No longer in active development (Tomcat inherited this effort)
 Has some thread-leaking issues, in my experience
 Only supports servlet API 2.0 (not later)
 You're on your own for support
81) Does Tomcat support JMS (Java Messaging Service)?

Tomcat is just a servlet container, not an EJB container nor an application server, so it does not contains any JMS basic support.

82) How do I load an applet from a servlet or JSP? Where do I place my applet class files?

An applet is executed on the client side (browser), not on the server side (servlet container). You need to place the applet class files in a location accessible from the browser, which means you have to treat them like normal files (like HTML or GIF files that you want the browser to load). Thus they need to go in the webapp directory tree, but not in the WEB-INF subdirectory.
It is best to think of the set of applet class files as entirely different from the set of servlet class files. They will be loaded by different Virtual Machines, and may even have different versions of classes. It is a simple matter to configure your build environment (Ant or make) to create copies of common class files in the two different classpath directories.
Since the concept of "current directory" is kind of fuzzy in the Servlet API, it is usually easiest to make a separate directory just for your applet class files, and use the optional CODEBASE parameter to the APPLET tag. Here is an architecture that works well:
Then if your servlet generates a line like:
out.println("&lt;APPLET CODE='MyApplet.class' WIDTH=50 HEIGHT=50 CODEBASE='/applets'&gt;"&gt;;
The browser will be able to load MyApplet.class and Util.class from your /applets web directory. It will not be able to load anything from WEB-INF/classes, since those files are accessible only by the servlet engine.
Note that loading an applet from a page generated by a servlet is much the same as loading an image. It is also easier to use a common "images" directory in that case, so that images can be loaded from any page regardless of "current" directory.
83) Is it possible in Tomcat to allow running all servlets from a directory without configuring each one in web.xml ?

maybe you can just use the same logic that is used by the servlet, calling an invoker for the given directory. I never tested it, but it sound right:





84) Using jsp:forward on Tomcat, why do I get the error java.lang.IllegalArgumentException at javax.servlet.http.HttpUtils.parseName( ?

Tomcat misinterprets jsp:param tag of jsp:forward if the name is represented as variable, i.e.

works well but
" value="bottom"/>
doesnt. The later is what we had in our code hence the exception.
85) How do I restrict access from servlets to certain directories?

It's not that difficult. You can simply use the Java Security, provided you have Java 2.
For example :
grant {
permission "-", "read,write";
This only allows to read and write in the current directory and its sub-directories. Write this in your tomcat.policy file and then start tomcat like this :
-Dtomcat.home=/path/to/tomcat org.apache.tomcat.startup.Tomcat "$@" &
Don't forget the necessary permissions for tomcat to work properly.

86) How do I determine a class's file location at runtime?

This method returns a URL to the class file in question. From that you can manipulate it to get what you want.
public locateClass(Class pClass){
// get the name of the class (e.g. my.package.MyClass)
String className = pClass.getName();

// create the resource path for this class
String resourcePath = "/" + className.replace('.','/') + ".class";

// get the resource as a url (e.g. file:/C:/temp/my/package/MyClass.class classURL = pClass.getResource(resourcePath);

return classURL;

87) Does Tomcat 4 have a GUI?
Accessing http://localhost:8080/manager directly will not do anything. You gan give command to it like:
You will get a page (very simple) that show all the active web applications.
88) How do I set session timeouts of greater than 30 minutes?

Tomcat session keeps expiring in spite of entries in web.xml to the effect of


89) Why can't Tomcat find my Oracle JDBC drivers in

TOMCAT 4.0.1 on NT4 throws the foll exception when i try to connect to Oracle DB from JSP.
javax.servlet.ServletException : oracle.jdbc.driver.OracleDriver
Root Cause : java.lang.ClassNotFoundException: oracle:jdbc:driver:OracleDriver
But, the Oracle JDBC driver ZIP file ( avbl in the system classpath.

My problem was solved. I copied the Oracle Driver class file ( in %TOMCAT_HOME%\lib directory and renamed it to classess111.jar. It worked.
Now i am able to connect to Oracle DB from TOMCAT 4.01 via Oracle JDBC-Thin Driver.
90) Can I use *.ear files in Tomcat?

No, Tomcat is a Web container, not an EJB container.

91) Can I place my classes somewhere other than inside WEB-INF?
Sun's specifications for Servlet define only the WEB-INF/classes and WEB-INF/lib directories in order to make the web application portable.
If you are not interested in portability, you can still put all your classes into the CLASSPATH environment variable.
The startup script for Tomcat 3.2.x should automatically add that classpath to the one used by Tomcat, while, with version 4.0.x and 3.3.x, you definitely need to make a small change in the startup script to add the CLASSPATH environment to the one that is generated by the script to esecute Tomcat.
The only issue is that the classes/jars you've added will be available to all the web applications running under that instance.
92) I am attempting to configure Tomcat's WebDav application (http://localhost:8080/webdav) to allow everyone to view the directory. However I need authorized users to be able to edit the content of the directory.
Here some additional info regarding the problem.
First I set the property in the webapps/webdav/web.xml file to allow read and write access. By default this gives everyone access to change content.


I tested this and it worked as expected--everyone was able to make changes to the content. Next I attempted to change the security settings for the directory so only certian users could make changes.

The Entire Web Application


Tomcat Supported Realm

An example role defined in "conf/tomcat-users.xml"


This modified things so you need to login as a user defined as having a role of "tomcat" in conf/tomcat-users.xml in order to edit or view anything in the webdav directory.
I need it to allow anyone to access the file over the web, but only prompt for a password if they try to modify the file.
Trying to remove the section doesn't work.

Basically whithin the section of the web.xml file you can define waht methods are protected. So you could protect the PUT command while leaving the GET command open to everyone.
I added the following and the behavior is what I was trying for:


This prompts for authentication when a user tries to lock a file for editing as well as POSTing and PUTting. I'm not sure if this will have any effect on forms ability to function and send stuff to a jsp page. (For example: will the user be asked for a password if they fill out and submit a form.) This isn't really a problem because all my webdav content will be in a separate folder at this point.
Hopefully that is useful to some other people as well.
93) Where should I copy the class file of applet in Tomcat? Where should I copy respective HTML file?

HTML (and .JSP) files are placed under the webapps/ROOT directory under the installation directory (or other than ROOT if you've defined a different context). Applet .class files would then be placed relative to the location of the HTML file. You can also specify a specific codebase by providing that paramter to the HTML file.
94) What's the initial user name and password for Jakarta Tomcat 4's admin tool?
As far as I know, there is no user that is assigned the 'admin' role after an installation of tomcat. (For security reasons I assume).
You can assign this role to a username/password combination of your choice in the /conf/tomcat-users.xml file.
95) I put my JAR file inside WEB-INF/lib, but my classes are not getting found. However, when i unzipped classes into WEB-INF/classes/, it works. What's up?

Some old versions of Tomcat do not include WEB-INF/lib/*.jar in WebApp classpath. You may handle it updating to a newer version of tomcat or (as you did) unpacking jars into WEB-INF/classes/.

96) Why request.getRemoteUser() returns NULL ?
As specified by the documentation, this method returns the login of the user making this request, if the user has been authenticated, or null if the user has not been authenticated.
The authentication, in this case, is the http authentication.

No comments: