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 EL to provide information about the request. EL is discussed in detail in the section “Element Language.”

Listing 13.3. Full Text of request-info.jsp
<HTML>
  <HEAD>
    <TITLE>Request Information</TITLE>
  </HEAD>
  <BODY>
    <P>Server ${header.Host}</P>
    <P>Header Accept-Encoding ${header["Accept-Encoding"]}</P>
    <P>Remote Host ${pageContext.request.remoteHost}</P>
    <P>Request Locale ${pageContext.request.locale}</P>
  </BODY>
</HTML>

Start up the J2EE RI and run deploytool and perform the following steps to deploy this JSP:

NOTE

This process is very similar to deploying servlets covered in Day 12. Alternatively you can use deploytool to open the file Day13/examples/j2ee-ri/examples.war to look at the completed Web Application.


1.
From the File menu, select New, Web Component. This will bring up the New Web Application Wizard. Skip over the introductory page if you didn't disable this page when working through the servlet examples yesterday.

2.
On the WAR File page, select Create New Stand-Alone WAR File in the WAR Location section. Under WAR Naming, select a location for the WAR file (this can be anywhere, but the code on the accompanying Web site uses the directory Day13/examples/j2ee-ri for storing EAR and WAR files). Call the WAR file examples.war, set the Display Name to examples and set the Context Root (Sun-specific settings) to examples. Click the Edit button and add the Day13/examples/JSP/examples/request-info.jsp file.

CAUTION

It is important that you keep the filename of the WAR file and the web application context root consistent. If you deploy the application using the asadmin command (or the asant deploy-examples-j2ee-ri command), the J2EE RI uses the name of the WAR file as the context root rather than the value supplied in the deployment descriptor. As long as you keep the WAR filename and context root consistent you will be fine. In this example the context root will be set to examples and the WAR filename to examples.war.

3.
On the next page, choose JSP as the Web component type and click on Next.

4.
On the next page, select /request-info.jsp as the JSP filename (see Figure 13.1). Accept the defaults for the remainder of the parameters on this screen.

Figure 13.1. deploytool Component General Properties page.


5.
Click Next and after viewing the list of next steps, select Finish.

6.
You can now add a component alias for your JSP (as you did for your servlets yesterday). Click on your request-info Web component in the left panel of deploytool. Select the Aliases tab in the right panel. Click on Add. Type /info.

7.
Select the Web Application and use the menu Tools, Deploy to deploy it. You will need to supply the name and password of the administrative user defined when you installed the J2EE RI.

Now, start up a Web browser. First, type in the URL for the J2EE Web server (http://localhost:8000) and check that the server is up and running.

NOTE

If, when you installed the J2EE RI, you configured the J2EE HTPP server on an alternate port, replace 8000 with your port number.


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

http://<Web server address>/<Context root>/<Component alias>

Now access your JSP using the following URL (see Figure 13.2):

http://localhost:8000/examples/info

Figure 13.2. Browser showing output of request-info.jsp.


As long as you did not make an error copying Listing 13.3, you should see the page shown in Figure 13.2. If you do not see the JSP output but see a stack trace instead, you should read the following sections to understand and fix the problem.

You already know that JSPs differ from servlets because they undergo a translation and compilation when first accessed. This means three types of errors can stop the JSP from displaying correctly:

  • JSP syntax errors causing the translation to fail

  • Java syntax errors causing the compilation to fail

  • Java runtime errors throwing an uncaught exception

  • 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
18.221.172.50