| Java FAQ | ||
| JSP FAQ | ||
| Servlet FAQ | ||
XyzWs JSP FAQ:
What are JSP scripting elements?
Printer-friendly version |
Mail this to a friend
|
Advertisement
|
What are JSP scripting elements?JSP scripting elements enable you insert Java code into the servlet that will be generated from the current JSP page. There are three forms:
Expression
The Syntax<%= Java expression %>
The <html> ... <body> Your hostname : <%=request.getRemoteHost()%><br> Current time : <%= new java.util.Date() %> ... </body> </html> 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. ScriptletThescriptlet can contain any number of language statements, variable or method declarations, or expressions that are valid in the page scripting language.
Within a scriptlet, you can do any of the following:
Any text, HTML tags, or JSP elements you write must be outside the scriptlet. For example,
<HTML>
<BODY>
<%
// This scriptlet declares and initializes "date"
java.util.Date date = new java.util.Date();
%>
Hello! The time is now
<%
out.println( date );
out.println( "<BR>Your machine's address is " );
out.println( request.getRemoteHost());
%>
</BODY>
</HTML>
Scriptlets are executed at request time, when the JSP container processes the request. If the scriptlet produces output, the output is stored in the out object. 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> Declaration
A The JSP you write turns into a class definition. All the scriptlets you write are placed inside a single method of this class. You can also add variable and method declarations to this class. You can then use these variables and methods from your scriptlets and expressions. You can use declarations to declare one or more variables and methods at the class level of the compiled servlet. The fact that they are declared at class level rather than in the body of the page is significant. The class members (variables and methods) can then be used by Java code in the rest of the page. Syntax<%! declaration; [ declaration;]+...%>When you write a declaration in a JSP page, remember these rules:
For example,
<%@ page import="java.util.*" %>
<HTML>
<BODY>
<%!
Date getDate()
{
System.out.println( "In getDate() method" );
return new Date();
}
%>
Hello! The time is now <%= getDate() %>
</BODY>
</HTML>
A declaration has translation unit scope, so it is valid in the JSP page and any of its static include files. A static include file becomes part of the source of the JSP page and is any file included with an include directive or a static resouce included with a <jsp:include> element. The scope of a declaration does not include dynamic resources included with <jsp:include>. 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> |