Set CORS header in Tomcat

If it’s a static site, then starting with Tomcat 7.0.41, you can easily control CORS behavior via a built-in filter. References: Tomcat 7 Tomcat 9 Pretty much the only thing you have to do is edit the global web.xml in CATALINA_HOME/conf and add the filter definition: <!– ================== Built In Filter Definitions ===================== –> … … Read more

How to change the port of Tomcat from 8080 to 80?

1) Go to conf folder in tomcat installation directory e.g. C:\Tomcat 6.0\conf\ 2) Edit following tag in server.xml file <Connector connectionTimeout=”20000″ port=”8080″ protocol=”HTTP/1.1″ redirectPort=”8443″/> 3) Change the port=8080 value to port=80 4) Save file. 5) Stop your Tomcat and restart it.

How to change the ROOT application?

There are three methods: First shutdown your Tomcat from the its bin directory (sh shutdown.sh). Then delete all the content of your Tomcat webapps folder (rm -fr *). Then rename your WAR file to ROOT.war, and finally start your Tomcat from the bin directory (sh startup.sh). Leave your war file in $CATALINA_BASE/webapps under its original … Read more

Servlet 5.0 JAR throws compile error on javax.servlet.* but Servlet 4.0 JAR does not

When I place the .class file in the corresponding Tomcat directory, start the server and try to interact with the app, I get the following exception: java.lang.ClassNotFoundException: javax.servlet.http.HttpServlet I tried placing the javax.servlet-api-4.0.1 in the Tomcat/lib directory, but then I get: java.lang.ClassCastException: class com.example.controllers.BeerSelect cannot be cast to class jakarta.servlet.Servlet The jakarta.servlet.Servlet is part of … Read more

How should I connect to JDBC database / datasource in a servlet based application?

A common practice is to configure this as a DataSource in the servlet container in question. It will provide you connection pooling facilities which will greatly improve performance. Also a common practice is to externalize the raw settings in some configuration file which is been placed in the classpath. In case you’re using Tomcat as … Read more

How to set the context path of a web application in Tomcat 7.0

What you can do is the following; Add a file called ROOT.xml in <catalina_home>/conf/Catalina/localhost/ This ROOT.xml will override the default settings for the root context of the tomcat installation for that engine and host (Catalina and localhost). Enter the following to the ROOT.xml file; <Context docBase=”<yourApp>” path=”” reloadable=”true” /> Here, <yourApp> is the name of, … Read more

Simplest way to serve static data from outside the application server in a Java web application

I’ve seen some suggestions like having the image directory being a symbolic link pointing to a directory outside the web container, but will this approach work both on Windows and *nix environments? If you adhere the *nix filesystem path rules (i.e. you use exclusively forward slashes as in /path/to/files), then it will work on Windows … Read more