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.

Docker Ep 8: Tomcat: Exploring Docker Port Mapping and Logs

15 August 2024 at 09:32

Caution: We are just starting from basics. Even if you don’t understand below concepts there is no problem.

In our previous adventures, we’ve dabbled in Docker’s mysterious arts, running containers, inspecting them, and even detaching them to roam in the background.

Today, we’re stepping up our game by diving into Docker port mapping and the powerful docker logs command. And what better companion for this journey than the Tomcat image, a trusty open-source web server that brings Java servlets to life?

Summoning the Tomcat Image

To begin, we need to summon our new ally: the Tomcat image. Tomcat is a legendary web server that, by default, operates on port 8080 within its container. But what if we want to make this web server accessible to the outside world through a different port? This is where Docker’s port mapping comes into play.

First, let’s visit the Docker Hub and search for the Tomcat image. Once there, we can see that Tomcat will run on port 8080 by default. We’ll need to expose this port and map it to a port on our host machine using the -p option.

Port Mapping: Connecting the Container to the World

Docker port mapping allows us to make the services inside our container accessible from the outside world by forwarding a host port to a container port. The syntax for port mapping is:

-p <host_port>:<container_port>

For example, let’s say we want to map port 8080 inside the container to port 8888 on our host machine. This means that when we access port 8888 on our host, we’ll actually be talking to port 8080 inside the Tomcat container.

Let’s see this in action.

Running Tomcat with Port Mapping

Fire up your Docker terminal, make sure the font size is nice and large, and enter the following command:

docker run -it -p 8888:8080 tomcat:8.0

Here’s what’s happening:

  • -it runs the container interactively.
  • -p 8888:8080 maps port 8080 inside the container to port 8888 on our host.
  • tomcat:8.0 specifies the Tomcat image and its version.

Now, sit back and relax (or maybe grab a coffee) as Docker pulls down the Tomcat image. It’s about 300 MB, so depending on your internet connection, this might take a moment.

Once the image is downloaded, Docker will spin up the container, and our Tomcat server will be ready to roll.

Accessing the Tomcat Server

With the Tomcat container up and running, we can access the Tomcat web server through our web browser. But first, we need to know where to find it.

If you’re running Docker on Linux, or using Docker for Mac or Windows, the host IP is simply localhost. If you’re using Docker Machine, you’ll need to grab the IP address of the Docker Machine.

Now, open up your browser, type in localhost:8888 (or your Docker Machine’s IP address followed by :8888), and hit Enter.

The Tomcat console page should greet you warmly.

Running Tomcat in Detached Mode

While running a container in the foreground is great for testing, in production, we typically want our containers to run in the background. For this, we use the -d flag to run the container in detached mode.

Let’s modify our previous command to run Tomcat in the background:

docker run -d -p 8888:8080 tomcat:8.0

After hitting Enter, Docker will return a long container ID, confirming that Tomcat is now running in the background, silently serving up Java servlets.

Checking Container Status with docker ps -a

Curious about the status of your Tomcat container? Use the docker ps -a command to list all the containers on your system, including those that have exited. This is a handy way to check if your container is still running or if it has quietly exited.

docker ps -a

Reading the Tea Leaves: docker logs

Sometimes, you want to peek into the inner workings of your running containers to see what they’re up to. This is where the docker logs command comes in. It lets you view the logs generated by your container, which can be incredibly useful for debugging or just keeping an eye on things.

Let’s say you want to check the logs of your running Tomcat container. You would run:

docker logs <container_id>

There is more on docker logs, we shall see later.

Basic Linux Commands- cat

By: Gowtham G
8 February 2024 at 07:34

Here,we are going to discuss about the command called cat also known as ‘concatenate’.This can be also used to create,append to,and concatenate files.Let’s see some of the commands using cat.

cat > file_name.txt

‘>’ ==>This symbol denotes “Funnel“.
We already discussed earlier, cat is used to create files.In this command we created a file name called ‘file_name’ with .txt extension.

cat -n file_name.txt

‘-n’ ==> denotes number.
Usually,it displays the number of lines present in a file.This will also add the lines which is empty.The gap between paragraphs are also counted.To Override this,we can use the below command

cat -b file_name.txt

‘-b’ ==> it also denotes the number,but this overrides ‘-n’.
It only count the lines where characters are present.

cat -s file_name.txt

‘-s’ ==> it squeeze the continuous empty areas.
That is,If a file has more than one non-output lines.
There are so many spaces between each paragraph,it squeeze them and displays one empty line.

cat -t file_name.txt
cat -T file_name.txt

This command is used to identify the TAB present in the file by showing “^I” in the front of the line.

cat -E file_name.txt

This command displays ” $ ” special character at the end of each line.

cat -e file_name.txt

This command displays ” $ ” at the beginning of each empty line.

cat file_name.txt file_name1.txt

This command concatenate two files into a single file and displays the result.

NOTE :
Cat is not a text editor.Here we can’t able to access the directional keys to navigate or edit the file.

❌
❌