December 21, 2024

Java Server Pages App in Tomcat

Getting ramped back up on Java is fun!  With Hello World running at the command prompt, it’s time for a web version.

I’d previously installed Tomcat 8 and verified it was functioning with a couple of the sample apps with the help of this tutorial.

To start the web app, I used this tutorial about creating a web application using Maven.

Creation

First, Maven’s code generator started things off again.  Instead of using the quickstart template, I used the webapp archetype:

mvn archetype:generate -DgroupId=edu.gatech.ateam.helloweb -DartifactId=helloweb -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

Packaging

Next I created the WAR file:

mvn package

This created a helloweb.war in the project/target folder.  Note the friendly WAR file name, unlike the package name given to the Hello World JAR quickstart project.

Deployment

The last step was to deploy the WAR file to Tomcat, which was nothing more than copying it from the target folder to the Tomcat webapps folder, which for me was

C:\apache\apache-tomcat-8.0.17\webapps

Then I fired up Tomcat by running the script:

C:\apache\apache-tomcat-8.0.17\bin\startup.bat

NOTE:  Tomcat does not always like to auto-deploy WAR files and some cleanup is helpful.  One thing I do recall from my previous go-around with JSP was that it was better to religiously shutdown the server, clean the folders, and re-deploy than to try to save time by skipping these steps.  This was common advice in many posts.

Execution

I’d originally set up Tomcat to listen on port 9999, per the Tomcat setup tutorial – your mileage may vary.

Pointing Chrome to the site http://localhost:9999/helloweb produced the following:

 

Hello World!

 

Conclusion

Much easier than I’d dared to hope!

Next step is to implement a Java Servlet and perfect the art of deployment again.

Leave a Reply