Chapter 4. Web Development

In this chapter we will develop together a simple web application. This will give us the chance to understand and observe some tricks that could be useful for us when we're developing applications using IntelliJ. The application simply tries to pass through some situations the developer goes through when he/she is developing an application.

Creating a web module

Web applications are the most used kind of application; they could be a small and simple website or a complete and powerful system. When we are creating a web project, we generally create a new project and add the other necessary modules to that project as needed. In this chapter we won't do this; instead, we will re-use the project we created in the last chapter; this way, you can see how flexible IntelliJ is compared to the project structure. As we are going to re-use the last project, open IntelliJ if it isn't already open.

Once you open the IDE, you will see that the last project is loaded in IntelliJ. To create a new module, we can proceed like we did before: go to File | New Module...; in the new module window, select the option JavaEE Web Module and change the module name to contacts-web. When you click on the Finish button, the new module will be positioned on top of the modules of the project.

If you expand the nodes of the new module, you will see the default structure of the web module with an empty src folder, a web folder with a blank index.jsp page, and web.xml file as leaf nodes. Let's see the properties of the new module using the Project Structure window (Ctrl + Alt + Shift + S). Everything looks okay until we get to the Facets option and select the web module we created. At the bottom of the window, you will see a message saying 'Web' Facet resources are not included in an artifact. Fortunately, this little problem can be solved by just clicking on the button Create Artifact to the right of the message. Now you will see that a new artifact is available for our web project.

An artifact can be understood as the specification of the input of the project and as the output too; it can be a simple JAR file or other kinds of outputs (WAR or EAR) depending on the facets that were set in the module. In the Artifacts option, you will see how the module is configured to generate the outputs. As we won't change anything here, you can click on the OK button to save the changes.

Now that our project is correctly configured, we can build it by just going to Build | Make Project (Ctrl + F9); and if you go to the out directory in the project root, you won't find any WAR file. This happens because the module is configured to generate an exploded artifact, so IntelliJ will only compile the existing Java files in the project and won't package it.

If you want to generate the WAR file, open the Project Structure window and, in the Type: combobox, select the desired format (Web Application: Archive). Notice that in the Output Layout tab, a new option will appear asking about the MANIFEST.MF file; you can click on the Create Manifest... button and select the web folder as the location for the file. When you click on the OK button, the option in the Output Layout tab will direct you to another panel you can use to configure the MANIFEST.MF file; when you are done, click on OK to save your changes.

Finally, to build you artifact, you can go to the Build | Build Artifacts menu and you will see a folder called artifacts in the out directory along with the WAR file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.39.133