This project contains two examples of Java servlets.
Servlets run inside a servlet container such as Apache Tomcat, and handle a subset of the HTTP requests sent to that servlet container, according to their configuration. Such configuration can be either by annotations, or by a web.xml
config file. This example shows the web.xml
approach.
As you can see in the POM file for this project, the <packaging>
of servlets is set to war
(line 11). This will result in a *.war
file being produced instead of a *.jar
file, when we execute Maven's package
goal.
Important: This will not work on the Community edition. Make sure you download and install the Ultimate edition from JetBrains' website (this is included on all lab machines). The Ultimate edition is free for academic use - simply create your JetBrains account using your University email address.
-
Download Tomcat 9.x, either the Zip or the 64-bit Windows zip, and put it somewhere on your machine (only do this if you haven't configured Tomcat on your machine yet).
-
Click on the drop-down menu of the "run configurations" toolbar, usually found in the top-right corner. It should look similar to the screenshot below. Choose "Edit configurations".
- Click the "+" button at the top-left, and choose Tomcat ➡ Local
-
Under "Application server", select your Tomcat instance. If you can't see it, click "configure".
-
If you need to configure, the dialog below will appear. Browse to your Tomcat instance you downloaded earlier.
-
There may be an error saying "No artifacts marked for deployment". Click the "Fix" button.
-
In the tab which appears, select either the "war" or "war exploded" artifact for deployment (it doesn't matter too much). Maven should have created these when the project was originally imported.
-
Note the "Application context". You can set this to whatever you like. This is to be included in the URL when the server is running. For example, if the server is running on
localhost:8080
, and the context is/hello
, and the Servlet endpoint itself is/world
, then the URL to access that particular Servlet endpoint would behttp://localhost:8080/hello/world
. -
Back on the "Server" tab, choose the port. Port
8080
is a fine default, but may not work on the lab machines, or other machines where this port is already in use.10000
has been found to work ok on the lab machines. -
Click "Ok".
-
Run Tomcat (and your deployed Servlets) by clicking the green "Play" button on the toolbar. Tomcat will start, and then your Servlets will be deployed to it. You can see the message "Artifact is deployed successfully " in the server console, as shown in the following screenshot.
-
You can browse to http://localhost:8080/example_03_servlets_war_exploded/hello in a web browser (assuming you didn't change the port or application context away from their defaults. If you did, modify the URL accordingly). You should see the text "Hello, world!" appear in the browser. This text is generated by the
HelloWorldServlet
class. The path/hello
is configured to point to that Servlet withinweb.xml
. -
While Tomcat is running, you may run either of the provided clients. Again, remember to change the URLs within them, according to your configured port and application context.
-
Remember to stop Tomcat when done.