Normal view

There are new articles available, click to refresh the page.
Before yesterdayMain stream

How to create Servlet and Deploy on Apache Tomcat 10.1 in Linux

4 April 2023 at 17:00

I am going to explain about how to create servlet and Deploy on Apache Tomcat Server 10.1 manually in any Linux distributions.

Directory Structure should be represented as below

directory structure to run servlets

You can keep ‘aaavvv’ folder as any name you want.

How to create Servlet on Apache Tomcat 10 in Linux

Step:1

Create a folder in /usr/share/tomcat10/webapps folder , In that folder create any folder name as you like, I create ‘myapps’ folder name as example.

cd /usr/share/tomcat10/webapps/;sudo mkdir myapps

Step:2

Go into myapps , then create ‘WEB-INF’ directory

cd myapps;sudo mkdir WEB-INF

Step:3

Go into WEB-INF, then create ‘classes’ directory

cd WEB-INF;sudo mkdir classes

Step:4

Go into classes directory, and create java file named ‘TeamTesters.java’ for this example, you can create any name you want.

cd classes;sudo nano TeamTesters.java

code for TeamTesters.java

Step:5

Run java program using javac command

sudo javac TeamTesters.java -cp /usr/share/tomcat10/lib/servlet-api.jar

here -cp represents classpath to run the program

Step:6

Go to backward directory (i.e., WEB-INF) and copy the web.xml file from ROOT directory present in the webapps folder present in tomcat10 folder

cd ..;sudo cp ../../ROOT/WEB-INF/web.xml web.xml;

Then edit web.xml file by adding <servlet> and <servlet-mapping> tag inside <web-app> tag

sudo nano web.xml

<servlet> and <servlet-mapping> in web.xml file

Step:9

Goto backward directory , (i.e., aaavvv) then create index.html file

cd ..; sudo nano index.html

content in index.html

Step:10

goto browser and type,

http://localhost:8080/myapps

servlet running on browser
Statement printed on html page declared in java

Common Troubleshooting problems:

  1. make sure tomcat server and java latest version is installed on your system .
  2. check systemctl or service status in your Linux system to ensure that tomcat server is running.

Automate this stuff…

If you wanted to automate this stuff… checkout my github repository

GitHub - vishnumur777/ServletCreationJava


How to create Servlet and Deploy on Apache Tomcat 10.1 in Linux was originally published in Towards Dev on Medium, where people are continuing the conversation by highlighting and responding to this story.

❌
❌