adplus-dvertising

Scriptlet tag in JSP&JSP declaration tag by R4R Team

Scriptlet tag in JSP:
The scriptlet tag is used to write java class statements inside the jspService() method. In other words, writing java code in a Java Server Page is done through scriptlet tag. A scriptlet tag starts with <% and ends with %>.

Example: insert the following codes between the <body> tags.

<%

// This scriptlet declares and initializes "date"

System.out.println( "Evaluating date now" );

java.util.Date date = new java.util.Date(); %>

The time is now:

<% // The following scriptlets generate HTML output

out.println( String.valueOf( date ));

out.println( "<br>Your machine's address is: " );

out.println( request.getRemoteHost()); %>

 JSP declaration tag:

The JSP declaration tag is used to declare fields and methods.

The code written inside the jsp declaration tag is placed outside the service() method of auto generated servlet.

So it doesn't get memory at each request.

Syntax of JSP declaration tag:

The syntax of the declaration tag is as follows:

<%! field or method declaration %>

Example: JSP declaration tag that declares field

In this example we explain JSP declaration tag, we are declaring the field and printing the value of the declared field using the jsp expression tag.
 

index.jsp

<html>

<body>

<%! int data=100; %>

<%= "Value of the variable is:"+data %>

</body>

</html>


Example:JSP declaration tag that declares method:

In this example we explain JSP declaration tag, we are defining the method which returns the cube of given number and calling this method from the jsp expression tag. But we can also use jsp scriptlet tag to call the declared method.
 

index.jsp

<html>

<body>

<%!

int cube(int n){

return n*n*n*;

}

%>

<%= "Cube of 4 is:"+cube(4) %>

</body>

</html>

Leave a Comment:
Search
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!