First JSP example

You are now ready to write and deploy your first JSP. The JSP in Listing 13.3 is a very simple JSP that uses an expression to generate the current date.

Listing 13.3. Full Text of date.jsp
 1: <HTML>
 2:   <HEAD>
 3:     <TITLE>JSP Date Example</TITLE>
 4:   </HEAD>
 5:   <BODY>
 6:     <BIG>
 7:       Today's date is <%= new java.util.Date() %>
 8:     </BIG>
 9:   </BODY>
10: </HTML>

Perform the following steps to deploy this JSP.

1.
Start up the J2EE RI and run deploytool.

2.
Create a new application to store your JSPs. Call it simple.

3.
Select File, New, Web Component and create a new WAR file in the application, call it Simple.

4.
Click Edit and add the JSP file date.jsp. It does not matter if your JSP source file is in a sub-directory or not, it will be placed at the top-level directory of the Web application. Figure 13.1 shows how the date.jsp file in a sub-directory of src/JSP is added to the Web application.

Figure 13.1. Adding a JSP to a Web component.


5.
Click Next.

6.
On the Choose Component Type page, set the Web component to be JSP.

7.
Click Next and in the JSP Filename box, select date.jsp. Leave the component name and display names with default values. Your screen should look like the one shown in Figure 13.2.

Figure 13.2. deploytool Choose JSP General Properties screen.


8.
Click Next twice to get to the Aliases screen.

9.
On the Aliases page, add the alias /date. This alias is used in the URL to find the JSP in the same way as for servlets. Your screen should look like the one shown in Figure 13.3.

Figure 13.3. deploytool JSP Alias page.


10.
Click Finish.

11.
Select Tools, Verifier to check the application before deployment. Ignore any warning about a missing context because you will add this in the next step.

12.
Select Tools, Deploy to deploy the application. Step through each deployment screen and supply a context root of /simple on the War Context Root screen.

As with servlets, the URL to reference the JSP under the J2EE RI is as follows:

http://<Webserveraddress>/<Contextroot>/<Componentalias>

For this example, the URL must specify the local host with a port number of 8000 because the J2EE Web server does not use the standard HTTP port number. A suitable URL follows:

http://localhost:8000/simple/date

Enter this URL in your favorite browser. As long as you did not make an error copying Listing 13.3, you should see the page shown in Figure 13.4. Each time you refresh the page, the time should change by a few seconds.

Figure 13.4. Browser showing the date JSP.


JSP Problems

There are three types of errors you can make with JSP pages:

  • JSP errors causing the translation to fail

  • Java errors causing the compilation to fail

  • HTML errors causing the page to display incorrectly

Finding and correcting these errors can be quite problematic because the information you need to discover the error is not readily available. Before looking at resolving errors, you will need to understand the JSP lifecycle.

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

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