Tutorial
Menus

JSP - JSP Tutorial

JSP Scripting Elements

Rating: 4.3/5 (3 votes cast)

Level   : Beginners
Author : Arunkumar S
Download Source : Not Avaliable

JSP Scripting Elements

JSP scripting elements let you insert Java code into the servlet that will be generated from the current JSP page. There are three forms:
  1. Expressions of the form <%= expression %> that are evaluated and inserted into the output,
  2. Scriptlets of the form <% code %> that are inserted into the servlet's service method, and
  3. Declarations of the form <%! code %> that are inserted into the body of the servlet class, outside of any existing methods.
Each of these is described in more detail below.

JSP Expressions

A JSP expression is used to insert Java values directly into the output. It has the following form:
<%= Java Expression %>  
The Java expression is evaluated, converted to a string, and inserted in the page. This evaluation is performed at run-time (when the page is requested), and thus has full access to information about the request. For example, the following shows the date/time that the page was requested:
Current time: <%= new java.util.Date() %>
To simplify these expressions, there are a number of predefined variables that you can use. These implicit objects are discussed in more detail later, but for the purpose of expressions, the most important ones are:
  • request, the HttpServletRequest;
  • response, the HttpServletResponse;
  • session, the HttpSession associated with the request (if any); and
  • out, the PrintWriter (a buffered version of type JspWriter) used to send output to the client.
Here's an example:
Your hostname: <%= request.getRemoteHost() %>
Finally, note that XML authors can use an alternative syntax for JSP expressions:
<jsp:expression>
Java Expression
</jsp:expression>
Remember that XML elements, unlike HTML ones, are case sensitive. So be sure to use lowercase.

JSP Scriptlets

If you want to do something more complex than insert a simple expression, JSP scriptlets let you insert arbitrary code into the servlet method that will be built to generate the page. Scriptlets have the following form:
<% Java Code %>  
Scriptlets have access to the same automatically defined variables as expressions. So, for example, if you want output to appear in the resultant page, you would use the out variable.
<% 
String queryData = request.getQueryString();
out.println("Attached GET data: " + queryData);
%>
Note that code inside a scriptlet gets inserted exactly as written, and any static HTML (template text) before or after a scriptlet gets converted to print statements. This means that scriptlets need not contain complete Java statements, and blocks left open can affect the static HTML outside of the scriptlets. For example, the following JSP fragment, containing mixed template text and scriptlets
<% if (Math.random() < 0.5) { %>
Have a <B>nice</B> day!
<% } else { %>
Have a <B>lousy</B> day!
<% } %>
will get converted to something like:
if (Math.random() < 0.5) { 
out.println("Have a <B>nice</B> day!");
} else {
out.println("Have a <B>lousy</B> day!");
}
If you want to use the characters "%>" inside a scriptlet, enter "%\>" instead. Finally, note that the XML equivalent of <% Code %> is
<jsp:scriptlet>
Code
</jsp:scriptlet>

JSP Declarations

A JSP declaration lets you define methods or fields that get inserted into the main body of the servlet class (outside of the service method processing the request). It has the following form: <%! Java Code %> Since declarations do not generate any output, they are normally used in conjunction with JSP expressions or scriptlets. For example, here is a JSP fragment that prints out the number of times the current page has been requested since the server booted (or the servlet class was changed and reloaded): <%! private int accessCount = 0; %> Accesses to page since server reboot: <%= ++accessCount %> As with scriptlets, if you want to use the characters "%>", enter "%\>" instead. Finally, note that the XML equivalent of <%! Code %> is <jsp:declaration> Code </jsp:declaration>


1 | 

Discussion about this tutorial

  Start a new Discussion | Read All Discussion
Subject RepliesLast Post
Javaorigin.com contact@javaorigin.com