JSP Declarations

A declaration declares one or more variables or methods that you can use in Java code later in the JSP file. You must declare the variable or method before you use it in the JSP file.

Following is the syntax of JSP Declarations

 <%!  field or method declaration %>
 

Example of JSP declaration tag

index.jsp


 <%@ page import="java.util.*" %>
 <html>
 <body>
 <%!
   Date theDate = new Date();
   Date getDate()
    {
       System.out.println( "In getDate() method" );
       return theDate;
   }
%>
Hello!  The time is now <%= getDate() %>
 </body>
 </html>
 

JSP Implicit Objects:

JSP supports nine automatically defined variables, which are also called implicit objects. These variables are:

Objects Description
request This is the HttpServletRequest object associated with the request.
response This is the HttpServletResponse object associated with the response to the client.
out This is the PrintWriter object used to send output to the client.
session This is the HttpSession object associated with the request.
application This is the ServletContext object associated with application context.
config This is the ServletConfig object associated with the page.
pageContext This encapsulates use of server-specific features like higher performance JspWriters.
page This is simply a synonym for this, and is used to call the methods defined by the translated servlet class.
Exception The Exception object allows the exception data to be accessed by designated JSP.

JSP Comments:

JSP comment marks text or statements that the JSP container should ignore. A JSP comment is useful when you want to hide or "comment out" part of your JSP page.

Following is the syntax of JSP comments:

 <html> 
<head<<title>Comment Test</title<</head> 
<body> 
<h2> Test of Comments</h2> 
<%-- This comment will not be visible in the page  view after some time --%< 
</body> 
</html>

There are a small number of special constructs you can use in various cases to insert comments or characters.

Syntax Purpose
<%-- comment --%> A JSP comment. Ignored by the JSP engine.
<!-- comment --> An HTML comment. Ignored by the browser.
<\% Represents static <% literal.
%\> Represents static %> literal.
\' A single quote in an attribute that uses single quotes.
\" A double quote in an attribute that uses double quotes.