Time for action – creating a standalone application

We are going to package our project and then close Eclipse. We will manually configure the configuration files of Apache Tomcat to create a user who has access to the Manager App and increase the default size of the deployable applications. We will initiate Apache Tomcat and, through a web browser, start the Manager App. Within the Manager App, we will select the project we packaged earlier and deploy it so that we can execute our app in a standalone way. The steps to do this are as follows:

  1. We will create a package for the distribution of our application. We will head to the Project Explorer panel, select the node named prd5Ch14, right-click on it, and go to Export | WAR File. We will be presented with a window on which we will configure the following:
    • For Web project, we will select our project (prd5Ch14).
    • For Destination, we will select a path, for example, /home/user/prd5Ch14.war.
    • Regarding Target runtime, we will check the option Optimize for a specific server runtime and select Apache Tomcat v7.0.
    • We will click on Finish to continue.

    Note

    Web Application Archive (WAR) is a file format that contains every component of a web application (Servlets, JSPs, HTML files, and so on).

  2. We are now done working with the IDE, so we will close Eclipse.
  3. Before deploying our application, we need to make a few modifications to Tomcat's Manager App. The Manager App will allow us to deploy and administer our web applications.

    We will now create a user who can access the Manager App. To do so, we will edit the file [TOMCAT_HOME]/conf/tomcat-users.xml, delete its current contents, and copy the following code:

    <?xml version='1.0' encoding='utf-8'?>
    <tomcat-users>
      <role rolename="tomcat"/>
      <role rolename="manager-gui"/>
      <user username="tomcat" password="password" roles="tomcat,manager-gui"/>
    </tomcat-users>

    Here we are defining a user with a username called tomcat and a password with the value password, as well as giving him/her the roles tomcat and manager-gui; this last role is the one needed to access the Manager App.

    We will save the changes to continue.

  4. We will now increase the default size allowed for applications that can be deployed through the Manager App. We will open the file web.xml by going to [TOMCAT_HOME]/webapps/manager/WEB-INF, search for the section under the tag <multipart-config>, and edit it in the following way:
    <multipart-config>
      <!-- 150MB max -->
      <max-file-size>157286400</max-file-size>
      <max-request-size>157286400</max-request-size>
      <file-size-threshold>0</file-size-threshold>
    </multipart-config>

    By doing so, we will have established a maximum size of 150 MB (157,286,400 bytes). We will save the changes to continue.

  5. We are now ready to start Apache Tomcat as explained in the beginning of this chapter.
    • On a Linux environment:
      1. Open a terminal and type:
          shell> cd [TOMCAT_HOME]/bin
        
      2. Start the service:
          shell> sh startup.sh
        
    • On a Windows environment:
      1. Open a terminal and type:
          shell> cd [TOMCAT_HOME]in
        
      2. Start the service:
          shell> startup.bat
        
  6. Having initiated Apache Tomcat, we will direct our favorite browser to the URL http://localhost:8080 and select the option Manager App as shown in the following screenshot:
    Time for action – creating a standalone application

    We will then log in using the previously defined user (username: tomcat and password: password).

  7. We will be presented with a new web page on which we will scroll down and search for the section Deploy. Once there, we will use the option Choose File to search for the file prd5Ch14.war (which we created in step 1) and then click on the Deploy button as shown in the following screenshot:
    Time for action – creating a standalone application

    Once the deployment has finished, we will see that our application has now been added to the Applications section as shown in the following screenshot:

    Time for action – creating a standalone application

    We are now ready to access our application through the following URL: http://localhost:8080/prd5Ch14.

What just happened?

We created the package prd5Ch14.war so that it contains our project and then closed Eclipse. We manually configured Apache Tomcat's tomcat-users.xml file and added the user tomcat with the necessary permissions to access the Manager App. We also configured the file web.xml to increase the maximum size of the applications that are deployed on Apache Tomcat; we set this value to 150 MB. After performing the said configurations, we initiated Apache Tomcat and used a web browser to access the URL http://localhost:8080; once there, we entered the Manager App and logged in using the user tomcat. We then headed to the Deploy section, selected the file prd5Ch14.war, and proceeded with the deployment of our application.

Have a go hero

Next, we will invite you to employ the knowledge you have acquired through this chapter to execute another PRD report in a standalone way.

The steps you should use are roughly the following:

  1. Use Eclipse to create and configure a new dynamic web project.
  2. Create and configure a Tomcat instance and all infrastructure components.
  3. Add a report of your choice and modify it so that it connects through JNDI.
  4. Create and configure code components (Listener and Servlet).
  5. Package the project and deploy it on Apache Tomcat.

Pop quiz – graphics

Q1. Which of the following statements are true?

  1. JEE is a programming language.
  2. Servlets are software components executed on the client side.
  3. JSPs are more oriented to UI.
  4. Apache Tomcat is a multiplatform server written in Java.
  5. Eclipse is an IDE.
  6. We can extend Eclipse by adding new components through plugins.
  7. In Eclipse, a Perspective is a combination of Views and Editors used for the modification of Java and HTML code.
  8. Every time we wish to include a report within an Eclipse project, we need to set its connection type to JNDI.
..................Content has been hidden....................

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