4. Creating a new Web context in Tomcat:

For the new application created, we need to add a context in Tomcat. We can have multiple web applications, each having a different context in Tomcat.

There are multiple ways of creating a new web context in Tomcat. 

  •  Deploying a WAR file
  • Adding a new context folder under <Tomcat_Home>\webapps
  • Adding a new context folder at any location and pointing Tomcat to it

 

  • Deploying a WAR file:
    Create a folder structure as below. The web application root will contain WEB-INF folder and web.xml folder. This is as per the servlet specification. WEB-INF/classes folder will contain all the class file along with its package folder structure. For our application, we will have the path for our servlet  WEB-INF/classes/in/techfreaks/servlet/MyFirstServlet.class. The WEB-INF/lib can contain other third party libraries like JDBC drivers, Graph components, struts libs etc. For our application, we do not need any third party libraries, so the lib folder will be empty. The web application root folder can contain JSPs, HTMLs, JS, CSS, images etc, directly under it or in other container folders.

simple-webapp
- WEB-INF

- lib
- classes
- in
- techfreaks
- servlet
- MyFirstServlet.class
- web.xml
- MyFirstPage.html


Create a WAR (Web ARchive file) from the above structure. We can either use 'jar cvf' command or ZIP utility to create the WAR file based from the folder structure. Create the WAR file by the name simple-webapp.war.

Just drop this simple-webapp.war in <Tomcat_Home>\webapps folder. Start the Tomcat service, if it is not already started, and watch the magic!!! Within a seconds, you will see a new simple-webapp folder created.

Now, you can access our first page using the following URL http://localhost:8080/simple-webapp/MyFirstPage.html.

  • Adding a new context folder manually under <Tomcat_Home>\webapps:
    For this, we just need to create the folder structure as showed above under <Tomcat_Home>\webapps folder. Restart Tomcat and you are done!

          Verify the creation of the context by accessing the URL http://localhost:8080/simple-webapp/MyFirstPage.html

  • Deploying an application already present in a different location:
    For a web application created in a different location as per the above directory structure, but not under <Tomcat_Home>\webapps, we can deploy the application by telling Tomcat to get the context from that location.

    But, how can we tell Tomcat to pick the application from another location.

Tomcat Manager console to rescue!!

Tomcat manager can be accessed using the URL http://localhost:8080/manager/html/ . It will prompt for a user id/password. User Id and password needs to be the one which we entered during our installation process.

Scroll below to the 'Deploy' section and enter the following information and click the Deploy button:
Context Path = Web application context name
WAR or Directory URL = The root folder, which will have the web application code.

  • This way of deployment copies over the whole application from the location entered to the <Tomcat Install Dir>/webapps folder. So, if you make any changes after deployment in the original location, the changes will not be reflected.