How to Add Buttons and Text to Your JavaFX Project
How to Overload Methods in Java
10 Useful Classes in the Java API

How to Use Scriptlets in Java Servlets

A scriptlet is a statement or group of statements that’s inserted directly into the Java servlet at the point where the out.print statements that create the surrounding HTML are generated. In short, scriptlets let you add your own code to the code that renders the page.

Scriptlets follow this basic form:

<% statements... %>

Here’s a JSP named DateJSP.jsp that uses the DateFormat class to format the date and display it on the page:

<html>
 <%@ page import="java.text.*" %>
 <%@ page import="java.util.*" %>
 <head>
 <title>Date JSP</title>
 </head>
 <body>
 <h1>
  Today is
  <%
   DateFormat df = DateFormat.getDateInstance(
    DateFormat.FULL);
   Date today = new Date();
   String msg = df.format(today);
   out.println(msg);
  %>
 </h1>
 <h1>Have a nice day!</h1>
 </body>
</html>

This JSP begins with a pair of page import directives to import the java.text and java.util packages. Then the following Java statements are inserted right between the lines that generate the text Today is and Have a nice day!, like so:

DateFormat df
 = DateFormat.getDateInstance(DateFormat.FULL);
Date today = new Date();
String msg = df.format(today);
out.println(msg);

These lines create a string variable named msg and then use out.println to write the string to the response output. As a result, the formatted date is inserted between <h1>Today is </h1> and <h1>Have a nice day!</h1>.

image0.jpg

Scriptlets don’t have to add anything to the HTML output. In many cases, they perform functions such as writing information to a file. Suppose that you have a JSP that gets data from a form that includes input text fields named FirstName and LastName.

Suppose also that you have a class named CustFile with a static method named writeCustomer that accepts a first and last name as parameters and writes them to a file. Here’s a scriptlet that gets the first and last names and calls the writeCustomer method to write the name to the customer file:

<% String firstName = request.getParameter("FirstName");
 String lastName = request.getParameter("LastName");
 CustFile.writeCustomer(firstName, lastName);
%>

If you want, you can get tricky with scriptlets. No rule says that you have to complete block statements such as if or while statements within a single scriptlet. If you leave a block open at the end of a scriptlet, any HTML that follows is generated by out.print statements that are included in the block. The only restriction is that you must eventually end the block with another scriptlet.

Here’s a scriptlet named LoopyJSP.jsp that repeats a line 12 times on the page by including the line in the block of a for loop:

<html>
 <head>
 <title>Can’t you see I’m trying to work here?</title>
 </head>
 <body>
 <% for (int i = 0; i < 12; i++)
  {
 %>
 All work and no play makes Jack a dull boy.<br>
 <%
  }
 %>
 </body>
</html>

If you run this scriptlet, this page appears.

image1.jpg

Obviously, this sort of programming structure is prone to error. Avoid it whenever you can.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Logical Errors in Java
Java All-in-One For Dummies Cheat Sheet
How to Create an Enhanced for Loop in Java
JavaFX: How to Create Reflections
Programming Java: Throwing Errors
Advertisement

Inside Dummies.com