Updating Java Servlet without restarting Tomcat in Tomcat 8.0 ownwards

Problem: Recently i started learning Java Servlets and faced an issue regarding updating of Servlet after recompiling the servlet class. Initially, I what i did was shutting down Tomcat and restarting again after making any changings to servlet and recompiling it. But then i did some digging and found the solution for this issue. Following are the steps for handling this problem.

The Guide is for Tomcat 8 ownwards.

Open Tomcat directory and navigate to conf Folder. In Conf folder, you will find a file tomcat-users.xml . Open that xml file and you will something like following in that file:


<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<!--
<role rolename="tomcat"/>
<role rolename="role1"/>
<user username="tomcat" password="tomcat" roles="tomcat"/>
<user username="both" password="tomcat" roles="tomcat,role1"/>
<user username="role1" password="tomcat" roles="role1"/>
-->
</tomcat-users>

There you can see the default users. The next step is to add a new User here. Edit that file and add following code for a new user before closing ‘tomcat-users’ tag.


<role rolename="manager-gui"/>
<user username="admin" password="shumail" roles="manager-gui,manager-jmx,manager-script,manager-status"/>
<!-- Just add above 2 lines for creating a new user -->

After addition of this code, the tomcat-users.xml file will look something like this:

<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<!--
<role rolename="tomcat"/>
<role rolename="role1"/>
<user username="tomcat" password="tomcat" roles="tomcat"/>
<user username="both" password="tomcat" roles="tomcat,role1"/>
<user username="role1" password="tomcat" roles="role1"/>
-->

<!-- Following is the code for new user -->
<role rolename="manager-gui"/>
<user username="admin" password="shumail" roles="manager-gui,manager-jmx,manager-script,manager-status"/>

</tomcat-users>

Save the xml file. At this point, we have successfully created a new user with username admin and password shumail and assigned 4 roles to this user – manager-gui, manager-jmx, manager-script, manager-status.

We are almost done. Now whenever you do any changes in servlet code and recompile it, all you need to do is open following URL in browser that uses Tomcat Manager Application to redeploy the servlet:

http://localhost:8080/manager/text/reload?path=/

This will prompt for username and password. Enter username and password of the user we created earlier here i.e admin/shumail. You should see this type of message if it will be successful.

OK - Reloaded application at context path /

Refresh your application URL and you will see the updated version.
Share your thoughts in comments below – Don’t forget to share this article – Sharing is Caring..