Home > Articles

Using JSP Scripting Elements

  • Print
  • + Share This
JavaServer Pages (JSP) scripting elements are primary ways of performing server-side tricks in your JSP pages. Drew Falkman's beginner's guide teaches you how to get started using these elements.
This sample chapter is excerpted from Drew Falkman's JRun Web Application Construction Kit (Que, 2001).
This chapter is from the book

The JSP scripting elements (or scriptlets for short) are the way you can perform Java and JavaScript (in a few JSP engines, such as Macromedia JRun) scripting magic. They are always enclosed in <% %> brackets.

Scripting elements are processed by JRun (or another JSP engine) before any custom actions are processed. In the scripting element <% Date today = new Date(); %>, the Java keyword new signals JRun to create a new object. Date() tells Java it will be a Date object created from the java.util.Date class. The result is a new date object, whose value is the current date and time. This is where the variable today gets its value.

NOTE

A Java object is an instance of a class. Each class will define within itself what its objects will be made up of. For example, the Date object here will contain a date. The Date class defines how that information will be stored and how it can be accessed.

Classes also contain methods that are ways of accessing the object. To use a method, you specify the method name, following a dot (.) after the object name. Here, you could use the getTime() method of the Date class like this: today.getTime(), where today is the name of the Date object. Calling this method would return the number of milliseconds between midnight on January 1, 1970 and the time and date stored in the today Date object.

In every scripting element, the <% signals to the JRun processor that it should be processing the information that follows. Expressions are modified scripting elements, with an added = sign, and are used to output String information directly to the JSP page. Scripting elements always begin and end with <% %> tags. Expressions always begin and end with <%= and %> tags. Between the expression tags, there must be valid scripting information that will output a String result. If there is any additional information between the tags, you get an error. We are telling it to output the following:

new Date()

Most of the time, JSP templates consist of a mixture of scripting elements and expressions. You can use the scripting elements to process data and the expressions to output the results. Say you want to change the formatting of the date in the JSP template. To do this, you need to take a different approach than you used earlier. Listing 1 is a new version of the hello template; save it as hello3.jsp, and browse the file to see output similar to what is shown in Figure 1.

Listing 1—The hello3.jsp File

<%@ page import="java.util.*,java.text.*"%>
<html>
<head>
  <title>Hello 2</title>
</head>

<body>

Hello, and welcome to JRun!
<br>

<% Date today = new Date();
  DateFormat formatter; 
  formatter = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);%>

It is now <%= formatter.format(today) %>


</body>
</html>

Figure 1 JRun allows you to perform processes with scripting elements and then output the results in expressions.

What if you had not placed your expression within the script tags? Try removing the <% %> tags, saving the page, and executing the page. You should see output similar to that in Figure 2—obviously not what you want. Because content that is not within <% %> tags are sent to the client as-is, using the script outside a <% %> block causes the text of the script itself, rather than the data returned by the script, to be sent to the client. Why? Because if it's outside a <% %> block, JRun will never process it.

Figure 2 If scripting code is sent to the browser, it usually means you have omitted the <% %> tags.

If you are intending to output information using an expression, remember to include the equals sign. Omitting it can cause an error. To test this, remove the = from the line <%= formatter.DateFormat(today) %>. Add a semicolon at the end of your expression, after the close ) parenthesis. This seems minor, but Java syntax requires this after every expression outside a JSP Expression (like it was before we made this modification). This line should now look like this: <% formatter.DateFormat(today); %>. Now save the template, and reload it in your browser. Note that the date is missing, as shown in Figure 3. This happens because JSP expressions signal to JRun to output the element following the =, whereas normal scripting elements simply process the data.

Figure 3 If = is not included in an expression, the data will not be sent to the browser.

NOTE

When working with scripting elements, it doesn't matter how many spaces you have between the <% and %> tags. You can even bump scripts right up against them.

One of the main ways to use scripting is to format different types of data. Listing 1, for example, uses the DateFormat class to change the way a date (today) is formatted. Take a look at the first scripting element:

<%   Date today = new Date();
   DateFormat formatter; 
   formatter = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);%>

The first thing we did here is to create a new date object, but writing the script directly instead of using the jsp:param action. This is because in this example we want to do more with the date object. The next two lines create a DateFormat object, and modify that object using the getDateInstance() method. Then, we output information in the following expression:

<%= formatter.format(today) %>

The formatter object applies the format to the new date, today, by passing it to the called method format(). Take note of JRun's capability to process and then output the results of that processing.

NOTE

Many Java method calls can be passed arguments. These arguments specify information necessary to execute the method. For example, the DateFormat.format() method needs to be passed a Date object to format.

In JRun, JavaScript can also be used to perform scripting tasks. This is wonderful for those who are already familiar with JavaScript for performing client-side processing. However, because of the portability of Java and all the tools available, the examples throughout this book use Java.

Remember that JSP code is processed on the server, not on the client. The JSP code you write is never sent to the Web browser. What is sent to the browser? Most browsers feature a View Source option that displays code as received. If you view the source of the page generated when you call hello3.jsp in your browser, you see something like this:

<html>
<head>
  <title>Hello 2</title>
</head>

<body>

Hello, and welcome to JRun!
<br>



It is now 6/10/01

</body>
</html>

As you can see, there is no JSP code here at all. The <% %> tags, the expressions, the equal signs—all have been stripped out by the JRun Server, and what was sent to the client is the output that they generated.

TIP

Viewing the generated source is an invaluable debugging trick. If you ever find that output is not being generated as expected, viewing the source can help you understand exactly what was generated and why.

  • + Share This
  • 🔖 Save To Your Account