• No results found

Web Application Deployment

In document Java Web Programming with Eclipse (Page 55-59)

At this point our Web application can be deployed to the Tomcat server.

There are several ways to deploy web applications in tomcat, which are de-tailed in the tomcat documentation. This book recommends that you deploy applications using the manager application that is installed with tomcat. To do this, do the following. (Make sure that you replace ${WORKSPACE}

with the actual path to your Eclipse workspace.)

In Eclipse create a new file in the top level of your website project called website.xml with the following contents. (Convert the back slashes to for-ward slashes if you are working under Linux or MacOS. In all cases, the slash before website in the path attribute needs to be a forward slash.)

<Context path="/website" docBase="${WORKSPACE}\website\web" />

Go to the manager application and locate the section called Deploy. Set the context path to /website. (Use a forward slash regardless of whether you are working in Windows, Linux or Mac.) Set the XML Configuration file URL to the following, where ${WORKSPACE} is the path to your Eclipse workspace. (Convert the back slashes to forward slashes if you are working under Linux or MacOS.)

${WORKSPACE}\website\website.xml

Click the Deploy button and then inspect the message section at the top of the manager application web page. If you see the following message, then you need to debug.

FAIL - Failed to deploy application at context path /website

If the application failed to start, you should check to make sure the path to website.xml is valid. You may also want to open the various tomcat log files and search for an error message that can give you a clue to solve the problem.

If the manager application reports successful deployment, the next step is to verify that the application is working correctly by going to the following url in your browser.

55

http://localhost:8080/website/home

If you make changes to Java code in a project, the changes will not be immediately deployed. To see the changes, you should reload the application through the manager application. If you make changes to the deployment descriptor (web.xml) in a project, reloading the application will not work.

Instead, you need to stop and then start the application, which you can do in the manager application.

5.8 Exercises

(1) Servlet Mapping Element

Add an additional servlet mapping element to the deployment descriptor that associates the home servlet with the url pattern index.html. Test that the home servlet can be reached in the following 4 different ways.

• http://localhost:8080/website/home

• http://localhost:8080/website/index.html

• http://localhost:8080/website/

• http://localhost:8080/website (2) Methods doGet and doPost

Become familiar with the doGet and doPost methods by creating web ap-plication that accepts a string provided by the user and displays it back to the user. To do this, you need to create an HTML form with an input field and a submit button. In the servlet, you need to call getParameter on the HTTPServletRequest object passed into doPost (assuming the method attribute of the form is ”post”) in order to get the value that the browser is sending. The key that you pass into getParameter should match the name of the input field you used in the HTML form.

(3) Adding Two Numbers

Modify the web application in exercise 1, so that it accepts two numbers provided by the user and displays the sum of the 2 numbers.

Chapter 6

Web Application Logging

6.1 Objectives

• Learn how to add libraries to a Java web application project.

• Learn how to add log4j logging support to a Java web application.

• Learn how to use the organize imports development tool provided by Eclipse.

6.2 Overview

Software developers usually spend a lot of time debugging code. There are several techniques that you can use to find bugs. One technique is to use a debugger, which is a service normally provided by an IDE. With a debugger the developer can step through code one statement at a time as the program executes and inspect that state of variables. For this process to work, the IDE needs to control the execution of the program. When developing a desktop application, you can launch its execution inside Eclipse, set breakpoints, step through the code and inspect the state of variables.

However, this procedure is not available to us because Eclipse does not control execution of our code; tomcat controls execution of the code. In order to have these capabilities, we would need to have Eclipse control the execution of Tomcat. One way to do this is to extend Eclipse functionality by installing the Eclipse Web Tools feature. However, we will not cover this option in this book.

Another technique that is useful for debugging is trace statements. In this case, we insert statements at various points in the code to print out

useful information. When this output goes into a file, it is called a log file. One of the benefits of using logging is that logging statements can be left in production code, so that log files are generated in the production environment. This enables the developer to diagnose bugs that surface in the production environment.

Rather than simply using the basic print facilities, a logging framework provides a developer with the ability to set different logging levels, so that logging can be more or less verbose depending on the situation. For example, when the code is running in the production environment, you will most likely record error events rather than the fine details that would be useful to debug a known bug.

There are two main choices of logging frameworks when working in Java:

log4j and Java logging API. Log4j is the older of the two and is more widely used than the Java logging API. However, the Java logging API provides the same basic functionality as log4j. We will illustrate the use of the log4j logging framework in this chapter.

In this chapter, you will add logging to the website application you started in the chapter on servlet basics.

In document Java Web Programming with Eclipse (Page 55-59)