Chapter: Internet & World Wide Web HOW TO PROGRAM - JavaServer Pages (JSP): Bonus for Java™ Developers

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail

A First JavaServer Page Example

We begin our introduction to JavaServer Pages with a simple example (Fig. 31.1) in which the current date and time are inserted into a Web page using a JSP expression.

A First JavaServer Page Example

 

We begin our introduction to JavaServer Pages with a simple example (Fig. 31.1) in which the current date and time are inserted into a Web page using a JSP expression.

 

      <?xml version = "1.0"?>

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

 

    <!-- Fig. 10.1: clock.jsp -->

 

    <html xmlns = "http://www.w3.org/1999/xhtml">

 

      <head>

        <meta http-equiv = "refresh" content = "60" />

 

<title>A Simple JSP Example</title>

 

      <style type = "text/css">

      .big { font-family: helvetica, arial, sans-serif;

      font-weight: bold;

      font-size: 2em; }

      </style>

      </head>

 

      <body>

      <p class = "big">Simple JSP Example</p>

     

      <table style = "border: 6px outset;">

      <tr>

      <td style = "background-color: black;">

      <p class = "big" style = "color: cyan;">

     

      <!-- JSP expression to insert date/time -->

      <%= new java.util.Date() %>

     

      </p>

      </td>

      </tr>

      </table>

      </body>

 

      </html>

 


 

Fig. 31.1 Using a JSP expression to insert the date and time into a Web page

 

As you can see, most of clock.jsp consists of XHTML markup. In cases like this, JSPs are easier to implement than servlets. In a servlet that performs the same task as this JSP, each line of XHTML markup typically is a separate Java statement that outputs the string representing the markup as part of the response to the client. Writing code to output markup can often lead to errors. Most JSP editors provide syntax coloring to help program-mers check that their markup follows proper syntax.

 

The JSP of Fig. 31.1 generates an XHTML document that displays the current date and time. The key line in this JSP (line 30) is the expression

 

<%= new java.util.Date() %>

 

JSP expressions are delimited by <%= and %>. This particular expression creates a new in-stance of class Date from package java.util. When the client requests this JSP, the preceding expression inserts the String representation of the date and time in the re-sponse to the client.

Note that we use the XHTML meta element on line 10 to set a refresh interval of 60 seconds for the document. This causes the browser to request clock.jsp every 60 sec-onds. For each request to clock.jsp, the JSP container reevaluates the expression on line 30, creating a new Date object with the server’s current date and time.

 

As in Chapter 30, we use Apache Tomcat to test our JSPs in the advjhtp1 Web application we created previously. For details on creating and configuring the advjhtp1 Web application, review Section 30.3.1 and Section 30.3.2. To test clock.jsp, create a new directory called jsp in the advjhtp1 subdirectory of Tomcat’s webapps directory. Next, copy clock.jsp into the jsp directory. Open your Web browser and enter the fol-lowing URL to test clock.jsp:

 

http://localhost:8080/advjhtp1/jsp/clock.jsp

 

When you first invoke the JSP, notice the delay as Tomcat translates the JSP into a servlet and invokes the servlet to respond to your request. [Note: It is not necessary to create a di-rectory named jsp in a Web application. We use this directory to separate the examples in this chapter from the servlet examples in Chapter 30.]


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.