JSP Declaration

Syntax :

<%! %>

XML Syntax

<jsp:declaration></jsp:declaration>

Purpose:

Used to declare a variable or to define methods.
Declarations do not generate output, hence they are normally used in conjunction with JSP expressions or scriptlets.

When translated into servelt
inserted into the main body of the servlet class (outside the _jspService method)

Example :Your hostname:

<%! int count=0;%>

 

JSP Scriptlet

Syntax :

<% %>

XML Syntax

<jsp:scriptlet></jsp:scriptlet>

Purpose:
To do some more complex task then just a printing scriptlet is used.

Automatically defined variables [e.g. request, response, application….] can be accessed here

Scriptlets can perform a number of tasks that cannot be accomplished with expressions.

–setting response headers and status codes,

–invoking side effects such as writing to the server log or

– updating a database, or

–executing code that contains loops, conditionals, or

–other complex constructs

When translated into servelt
will appear as it is i.e. directly inserted in_jspService method.

Example :

<% String queryData = request.getQueryString();

out.println("Attached GET data: " + queryData); %>

__________________________________________

<% String queryData = request.getQueryString(); %>

Attached GET data: <%= queryData %>

_____________________________________________

Attached GET data: <%= request.getQueryString() %>

 

JSP Expression

Syntax :

<%= %>

XML Syntax

<jsp:expression></jsp:expression>

Purpose:
The Java expression is evaluated, converted to a string, and inserted in the page
predefined variables that you can use
request, the HttpServletRequest;
response, the HttpServletResponse;
session, the HttpSession associated with the request (if any); and

When translated into servelt
will appear in out.print without double quotes in_jspService method.

Example :Your hostname:

<%= request.getRemoteHost()%>

Note : Do not use semicolon within JSP Expression. [Explanation : Content written in JSP Expression When translated into servlet will appear in out.print, where already ; is there so inside jsp expression ; is not permitted.]