Knowledge Share Helps you in some way
Configure run Java servlet ubuntu tomcat

Configure run Java servlet ubuntu tomcat

Thursday, 6 July 2023 |

I am explaining every step to run tomcat 8.0.21 in ubuntu and how to setup your projects and running Servlets ,Jsp. Tomcat 8.0.21 running in ubuntu 14.04 TLS needs couple of steps to configure it , i have already ubuntu 14.04 TLS as my operating system. so first we will install and configure tomcat , then we will run a servlet. Before installing tomcat we need jdk 7 so let copy the below command and run in ubuntu terminal , you can also see the download link below which show the information about jdk 7.

									     
sudo apt-get install openjdk-7-jdk										
									

Install JDK 7 download before installing tomcat.

Install JDK 7 download before installing tomcat.

Download the Tomcat and then I rename dowonload folder to "apache-tomcat" and move it to location /usr/local/apache-tomcat then go to /usr/local/apache-tomcat/conf/tomcat-users.xml or you can edit from terminal below is command.

									     
sudo nano /usr/local/apache-tomcat/conf/tomcat-users.xml										
									

Place below lines & save , you can use keyboard key as for saving [ctrl+^+x then press Y].

									     
<role rolename="manager-gui">
 <role rolename="admin-gui">
  <user password="rohit" roles="manager-gui,admin-gui" username="rohit">
 </user>
 </role>
</role>										
									

We have to create a bash file for running and stoping tomcat. Create a new file tomcat8021 in /etc/init.d/ and add the below code.

									      
#!/bin/bash
export CATALINA_HOME=/usr/local/apache-tomcat PATH=/sbin:/bin:/usr/sbin:/usr/bin
start() {
   sh $CATALINA_HOME/bin/startup.sh
}
stop() {
  sh $CATALINA_HOME/bin/shutdown.sh
}
case $1 in
start|stop) $1;;
restart) stop; start;;
*) echo "Run as $0 "; exit 1;;
esac										
									

Give 755 permission

									     
sudo chmod 755 /etc/init.d/tomcat8021										
									

For starting / stoping tomcat

									     
sudo /etc/init.d/tomcat8021 start / stop										
									

We need to set Classpath and java home varibale to work properly. so i edited the file call environment place below lines there.

									     
sudo nano /etc/environment CLASSPATH=.:/usr/lib/jvm/default-java/bin:/usr/local/apache-tomcat/lib/servlet-api.jar JAVA_HOME=/usr/lib/jvm/default-java										
									

Creating a new project in ubuntu 14.04 TLS

I created a folder called store in /usr/local/apache-tomcat/ , like store(Project Folder) ---> WEB-INF ---> classes(DIR) , lib(DIR) , web.xml (file)

web.xml file looks like below , you can just copy paste the code accordingly.

									     
<web-app>
<servlet>
 <servlet-name>HelloWorld</servlet-name>
 <servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
 <servlet-name>HelloWorld</servlet-name>
 <url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>										
									

Inside classes folder your source file HelloWorld.java will be store for compiling it use below command

									     
mangel@mangel-desktop:/usr/local/apache-tomcat/webapps/store/WEB-INF/classes$ javac -classpath /usr/local/apache-tomcat/lib/servlet-api.jar HelloWorld.java										
									

Go to cd /usr/local/apache-tomcat/conf/Catalina/localhost/ from terminal. sudo nano store.xml (store is the name of your project folder). Just paste below line

									     
<!-- Store Context -->
<context debug="0" docbase="store" path="/store" reloadable="true">
</context>										
									

So above all configure manually. just open locahost:8080/store/HelloWord

Ubuntu Linux CentOS