A very simple example to demonstrate the MVC pattern with servlets and JSPs
- There is an issue with the latest version of Payara Server, depending on the Java Version that you are using. You might get an error about the keystore if you are subject to this issue. The way to address it is to use JDK 11 both for running the application server AND for running asadmin (which IntelliJ is doing behind the scenes). Make sure to configure your IntelliJ Project and to select JDK 11 as the SDK.
- Open the project in IntelliJ
- You should see a popup saying "Maven projects need to be imported": click on "Enable Auto-Import"
- Check that you can build the project: in the "Maven Projects" pane, open "Lifecycle" and double-click "install". You should logs messages in the output console. Maven should create a
target
directory, in which you should see a file namedmvc-simple.war
. - Define one more "Run configurations":
- In the menu, select Run > Edit Configurations...
- Click on the + icon
- Select Glassfish Server > Local
- Give a name to the run configuration: "Run with Glassfish (local)"
- Assuming that you have downloaded the Glassfish version provided by Payara, you should be able to select the directory where you unpacked it by clicking on "Configure..."
- In the panel "Glassfish Server Settings", select the "domain1" domain
- At the bottom of the screen, you should see a red warning: No artifacts marked for deployment:
- Click on Fix
- Select the
xxx:war exploded
artifact (more practical than thexxx:war
for developement) - Click on Apply
- In the On 'Update' action drop-down, select Redeploy. With this setting, when you do a reload, the application will be redeployed in the application server.
- In the On frame deactivation, select Update resources and classes. With this setting, when you switch from IntelliJ to another application (typically the browser), the IDE will hot deploy your code modifications. This depends on the application server and likely to work only in Debug mode.
- Start the configuration in "Debug" mode: Run > Debug 'Run with Glassfish (local)' (warning: the ports used by Glassfish, 8080 and 4848 by default, need to be free. Watch out for running Docker containers and other projects).
- Intellij should build the project, start Glassfish and deploy the application. It should then open your web browser and load the page
http://localhost:8080/mvc-simple/
. You should see a Hello World! message. Type the following URL in the browser:http://localhost:8080/mvc-simple/quotes
. You should now see a page showing a list of 3 quotes, with their author. - Test if Hot Reload work:
- Open the
QuoteGenerator
class - Modify the code to add an extra quote.
- Go back to the browser (this triggers a 'frame deactivation') and reload the page
- You should now see 4 quotes in the list.
- Open the
- Test the debugger
- Add a breakpoint in the
doGet
method of theQuoteServlet
class. - Reload the page in the browser and verify that you the breakpoint works and that you can inspect variables.
- Do the same thing in the
view.jsp
file: the debugger also works in Java Server Pages!
- Add a breakpoint in the
- You can apply the same process for Wildfly (JBoss) and TomEE.