Running servlets on Tomcat in Intellij community edition using Smart Tomcat Plugin

Jaya Sundeep K
2 min readJan 27, 2021

In this article we talk only about how to put things in place to run servlets smoothly on Apache Tomcat in Intellij community edition.

Create a project by going through options File -> New -> Project ->Select Maven/Gradle project.

Now we have basic project structure in place. We shall create a deployment directory with any name, traditionally, webapps or WebContent with file structure as,

src

////webapps

////////jsps

////////WEB-INF

////////////web.xml

////////index.html

Note: web.xml file is not necessary if you are using servlet-api.jar version greater than 3.0 with the introduction of annotations

Dependency :

  1. servlet-api.jar : Add this jar file to the dependency file (pom.xml if Maven project or build.gradle if Gradle project) for compiling servlets
  2. Install Tomcat of required version
  3. Install SmartTomcat plugin to help Tomcat find the deployment directory (i.e webapps in the above structure)

Configuring environment to run servlets in Tomcat web container using Smart-Tomcat :

  • Now open ‘Edit Run/Debug Configurations tab by clicking ‘Add Configuration’ option in the top right
  • Select Smart Tomcat; Give it a name;
  • Go to configuration section; In adjacent to Tomcat Server field, click on configuration select the folder in which Tomcat server is installed;
  • Select webapps/WebContent folder with above mentioned structure as Deployment Directory
  • Provide Context Path to set root API URL;
  • You may leave Server Port as is (i.e, 8080), if there is no other application already running in the port;

Note: If we use IntelliJ Ultimate edition, we need not use Smart Tomcat, because it knows which folder to pick up as deployment directory, given we follow fixed project structure as it is configured to detect

Scripting servlets:

  • Now we are all set to add servlets’ packages to src/main/java and start accessing them from root API URL previously mentioned under Context Path

Note : Depending upon the servlet-api.jar version, we might have to map the servlets in web.xml file or use @WebServlet annotations. Refer to below resource for differences between them.

Look at the console for details if you encounter any errors.

Thanks for reading the article. Have a nice day!

--

--