Scripting elements in JSP Pages

We can use Java Programming Language statements in JSP pages using scripting elements. There are three types of scripting elements used in JSP.

1. Scriplet Tag
2. Expression Tag
3. Declaration Tag

There are three ways to create and use objects in scripting elements:
  • Instance and class variables of the JSP page’s servlet class are created in declarations and accessed in scriptlets and expressions.
  • Local variables of the JSP page’s servlet class are created and used in scriptlets and expressions.
  • Attributes of scope objects are created and used in scriptlets and expressions.

JSP Scriptlets
A JSP scriptlet is used to contain any code fragment that is valid for the scripting language used in a page. The syntax for a scriptlet is as follows:
<%  scripting-language-statements or Java code  %>

Example of JSP scriplet Tag :
On index.jsp, user enters name that send as request to user.jsp which prints username as output.

index.jsp
   <html>   
   <body>   
      <form action="user.jsp">   
        <input type="text" name="username">   
        <input type="submit" value="Submit"><br/>   
      </form>   
   </body>   
   </html>   

user.jsp
   <html>   
   <body>   
       <%   
          String uname=request.getParameter("username");   
          out.print("Welcome! "+uname);   
        %>     
   </body>   
   </html>   

No comments: