Running JSP in Tomcat

To run the JSP we created in the previous section in the web browser, you will need to deploy the application in a servlet container. We have already seen how to configure Tomcat in Eclipse. Make sure that Tomcat is running by checking its status in the Servers view of Eclipse:

Figure 2.15: Tomcat started in the Servers view

There are two ways to add a project to a configured server so that the application can be run on the server:

  1. Right-click on the server in the Servers view and select the Add and Remove option. Select your project from the list on the left (Available resources) and click Add to move it to the Configured list. Click Finish.
Figure 2.16: Add a project to the server
  1. The other method to add a project to the server is to right-click on the project in Project Explorer and select Properties. This opens the Project Properties dialog box. Click on Server in the list and select the server in which you want to deploy this project. Click OK or Apply.
Figure 2.17: Select server in project properties

In the first method, the project is immediately deployed in the server. In the second method, it will be deployed only when you run the project in the server.

  1. To run the application, right-click on the project in Project Explorer and select Run As | Run on Server. The first time you will be prompted to restart the server. Once the application is deployed, you will see it under the selected server in the Servers view:
Figure 2.18: Project deployed on the server
  1. Enter some text other than admin in the username and password boxes
    and click Submit. You should see the error message and the same form should be displayed again.
Figure 2.19: Project running in the built-in browser in Eclipse
  1. Now enter admin as username and password and then submit the form. You should see the welcome message.

JSPs are compiled dynamically to Java classes, so if you make any changes in the page, in most cases, you do not have to restart the server; just refresh the page, and Tomcat will recompile the page if it has changed and the modified page will be displayed. In cases when you need to restart the server to apply your changes, Eclipse will prompt you if you want to restart the server.

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

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