Skip to main content

JSP Scriptlet tag (Scripting elements)

In JSP, java code can be written inside the jsp page using the scriptlet tag. Let's see what are the scripting elements first.

JSP Scripting elements

The scripting elements provides the ability to insert java code inside the jsp. There are three types of scripting elements:

  • scriptlet tag
  • expression tag
  • declaration tag

JSP scriptlet tag

A scriptlet tag is used to execute java source code in JSP. Syntax is as follows:

  1. <%  java source code %>  

Example of JSP scriptlet tag

In this example, we are displaying a welcome message.

  1. <html>  
  2. <body>  
  3. <% out.print("welcome to jsp"); %>  
  4. </body>  
  5. </html>  

Example of JSP scriptlet tag that prints the user name

In this example, we have created two files index.html and welcome.jsp. The index.html file gets the username from the user and the welcome.jsp file prints the username with the welcome message.

File: index.html

  1. <html>  
  2. <body>  
  3. <form action="welcome.jsp">  
  4. <input type="text" name="uname">  
  5. <input type="submit" value="go"><br/>  
  6. </form>  
  7. </body>  
  8. </html>  

File: welcome.jsp

  1. <html>  
  2. <body>  
  3. <%  
  4. String name=request.getParameter("uname");  
  5. out.print("welcome "+name);  
  6. %>  
  7. </form>  
  8. </body>  
  9. </html>  

JSP expression tag

The code placed within JSP expression tag is written to the output stream of the response. So you need not write out.print() to write data. It is mainly used to print the values of variable or method.

Syntax of JSP expression tag

  1. <%=  statement %>  

Example of JSP expression tag

In this example of jsp expression tag, we are simply displaying a welcome message.

  1. <html>  
  2. <body>  
  3. <%= "welcome to jsp" %>  
  4. </body>  
  5. </html>  

Note: Do not end your statement with semicolon in case of expression tag.

Example of JSP expression tag that prints current time

To display the current time, we have used the getTime() method of Calendar class. The getTime() is an instance method of Calendar class, so we have called it after getting the instance of Calendar class by the getInstance() method.

index.jsp

  1. <html>  
  2. <body>  
  3. Current Time: <%= java.util.Calendar.getInstance().getTime() %>  
  4. </body>  
  5. </html>  

Example of JSP expression tag that prints the user name

In this example, we are printing the username using the expression tag. The index.html file gets the username and sends the request to the welcome.jsp file, which displays the username.

File: index.jsp

  1. <html>  
  2. <body>  
  3. <form action="welcome.jsp">  
  4. <input type="text" name="uname"><br/>  
  5. <input type="submit" value="go">  
  6. </form>  
  7. </body>  
  8. </html>  

File: welcome.jsp

  1. <html>  
  2. <body>  
  3. <%= "Welcome "+request.getParameter("uname") %>  
  4. </body>  
  5. </html>  

JSP Declaration Tag


The code written inside the jsp declaration tag is placed outside the service() method of auto generated servlet.The JSP declaration tag is used to declare fields and methods.

So it doesn't get memory at each request.

Syntax of JSP declaration tag

The syntax of the declaration tag is as follows:

  1. <%!  field or method declaration %>  

Difference between JSP Scriptlet tag and Declaration tag

Jsp Scriptlet TagJsp Declaration Tag
The jsp scriptlet tag can only declare variables not methods.The jsp declaration tag can declare variables as well as methods.
The declaration of scriptlet tag is placed inside the _jspService() method.The declaration of jsp declaration tag is placed outside the _jspService() method.

Example of JSP declaration tag that declares field

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

index.jsp

  1. <html>  
  2. <body>  
  3. <%! int data=50; %>  
  4. <%= "Value of the variable is:"+data %>  
  5. </body>  
  6. </html>  

Example of JSP declaration tag that declares method

In this example of 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

  1. <html>  
  2. <body>  
  3. <%!   
  4. int cube(int n){  
  5. return n*n*n*;  
  6. }  
  7. %>  
  8. <%= "Cube of 3 is:"+cube(3) %>  
  9. </body>  
  10. </html>  



Anurag Rana

Comments

Popular posts from this blog

JAVA Scrollbar, MenuItem and Menu, PopupMenu

ava AWT Scrollbar The  object  of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is a  GUI  component allows us to see invisible number of rows and columns. AWT Scrollbar class declaration public   class  Scrollbar  extends  Component  implements  Adjustable, Accessible   Java AWT Scrollbar Example import  java.awt.*;   class  ScrollbarExample{   ScrollbarExample(){               Frame f=  new  Frame( "Scrollbar Example" );               Scrollbar s= new  Scrollbar();               s.setBounds( 100 , 100 ,  50 , 100 );               f.add(s);               f.setSize( 400 , 400 );               f.setLayout( null );               f.setVisible( true );   }   public   static   void  main(String args[]){           new  ScrollbarExample();   }   }   Output: Java AWT Scrollbar Example with AdjustmentListener import  java.awt.*;   import  java.awt.event.*;   class  ScrollbarExample{        ScrollbarExample(){               Frame f=  new  Frame( "Scro

Difference between net platform and dot net framework...

Difference between net platform and dot net framework... .net platform supports programming languages that are .net compatible. It is the platform using which we can build and develop the applications. .net framework is the engine inside the .net platform which actually compiles and produces the executable code. .net framework contains CLR(Common Language Runtime) and FCL(Framework Class Library) using which it produces the platform independent codes. What is the .NET Framework? The Microsoft .NET Framework is a platform for building, deploying, and running Web Services and applications. It provides a highly productive, standards-based, multi-language environment for integrating existing investments with next-generation applications and services as well as the agility to solve the challenges of deployment and operation of Internet-scale applications. The .NET Framework consists of three main parts: the common language runtime, a hierarchical set of unified class librari

C++ this Pointer, static, struct and Enumeration

C++ this Pointer In C++ programming,  this  is a keyword that refers to the current instance of the class. There can be 3 main usage of this keyword in C++. It can be used  to pass current object as a parameter to another method. It can be used  to refer current class instance variable. It can be used  to declare indexers. C++ this Pointer Example Let's see the example of this keyword in C++ that refers to the fields of current class. #include <iostream>    using   namespace  std;   class  Employee {       public :           int  id;  //data member (also instance variable)               string name;  //data member(also instance variable)            float  salary;          Employee( int  id, string name,  float  salary)             {                   this ->id = id;                  this ->name = name;                  this ->salary = salary;            }             void  display()             {                 cout<<id<< "  " <<name<&