This project is created as an example, demonstrating the principles of Java Servlet technology. It is used as a quideline for practical exercises on the subject Application Architectures of Software Systems at the Faculty of Informatics and Information Technologies in Bratislava. Feel free to use these materials for your needs.
- IntelliJ IDEA Ultimate - 30 Day trial is enough for this demonstration. If you want, you can get free license from JetBrains if you provide your ISIC card information.
- Apache Tomcat 9 Binary
- Java SE Development Kit 8 - Select version specific for your OS
Execute the installer and leave all settings as default.
You have downloaded a zip file with the binary distribution of Tomcat. Unzip the archive and rename the main folder to Tomcat. Place it to some more convenient location like C:\Tomcat
for Windows or /Library/Tomcat
for Mac and Linux
Mac
- Execute the .pkg file
- Double click on the Java installer to start the installation wizard
- Leave all setings as default
- DONE
Windows
- Execute the .exe file
- Leave all settings as default
- Now you need to add Java to your path
- Open System Properties -> Advanced -> Environment Variables... -> (System variables) New...
- Variable name: JAVA_HOME
- Variable value: Folder where you have installed the JDK. Default is
C:\Program Files\Java\jdk1.8.9_121
- Click OK
- DONE
- Open IntelliJ IDEA Ultimate
- Select "Create New Project"
- Select "Java Enterprise" from the left panel
- Add "Project SDK" from the JDK installation folder
- From the "Additional Libraries and Frameworks" panel, select "Web Application"
- Click "Next"
- Enter project name. f.e.: "SimpleJavaWebApp"
- Select "Finish"
First we need to select all artifacts that will be used to create the .war file that will be deployed to the Tomcat server.
- Within the IntelliJ IDE, right click on the root of the project in the "Project" tool window on the left side of the editor
- Select "Open Module Settings"
- Select "Artifacts" from the left panel
- Select the "+" on top of the center panel
- Select the "Web Application: Archive" and select the option for our project
- Make sure that the "Include in project build" checkbox for our archive is selected
- Click "Apply" and "Done"
- Build the project from IntelliJ
- Navigate to the directory
SimpleJavaWebApp_war/out/artifacts/SimpleJavaWebApp_war
- Copy the war file to the
Tomcat/webapps
directory
- Navigate to
Tomcat/bin
- Execute the
startup.sh
script for Mac or Linux orstartup.bat
script for Windows
The application can be accesed at http://localhost:8080/SimpleJavaWebApp_war/
- Navigate to the
src
folder in the "Project" tool window - Right click on the
src
folder - Select "New" -> "Servlet"
- Navigate to
web.xml
- Insert the servlet definition and mapping inside the
web-app
tags
Example:
<servlet>
<servlet-name>ServletName</servlet-name>
<servlet-class>ServletName</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletName</servlet-name>
<url-pattern>/ServletName</url-pattern>
</servlet-mapping>