Important JSP tags

Discussion in 'Java' started by Sanskruti, May 7, 2007.

  1. Sanskruti

    Sanskruti New Member

    Joined:
    Jan 7, 2007
    Messages:
    108
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Consultant
    Location:
    Mumbai, India
    There are four types of JSP tags, which are important and often required.

    1. Directives



    These types of tags are used primarily to import packages. Altenatively you can also use these tags to define error handling pages and for session information of JSP page.
    1. Code:
      <%@page language="java" %> 
    2. Code:
      <%@page language="java" session="true" %> 
    3. Code:
      <%@page language="java" session="true" errorPage="error.jsp"  %>
    4. Code:
      <%@page language="java" import="java.sql.*, java.util.*" %> 
    5. Code:
      <%@ include file="/title.jsp"%> 

    2. Declarations



    JSP declarations starts with '<%!' and ends with '%>'. In this you can make declarions such as int i = 1, double pi = 3.1415 etc. If needed, you can also write Java code inside declarations.

    Example 1

    Code:
    <%!
    
    int radius = 7; 
    double pi = 3.1415;
    
    %>
    
    Example 2

    Code:
    <%!
    
    double radius = 7; 
    double pi = 3.1415;
    
    double area()
    {
        return pi*radius*radius;
    }
    
    %>

    3. Scriptlets



    JSP Scriptlets starts with '<%' and ends with '%>'. This is where the important Java code for JSP page is written.

    Example

    Code:
    <%
    	String id, name, dob, email, address;
                    
        id = request.getParameter("id");
        name = request.getParameter("name");
        dob = request.getParameter("dob");
        email = request.getParameter("email");
        address = request.getParameter("address");
    
    	sessionEJB.addClient(id, name, dob, email, address);
     %>
    request, response, session and out are the variables available in scriptlets.

    4. Expressions



    JSP expressions starts with '<%=' and ends with '%>'. If you want to show some value, you need to put it in between these tags.

    Example:
    Code:
    <%!
    
    double radius = 7; 
    double pi = 22/7;
    
    double area()
    {
        return pi*radius*radius;
    }
    
    %>
    
    <html>
      <body>
    	Area of circle is <%= area() %>
     </body>
    </html>
    
    You will see the output:
    Code:
    Area of circle is 147.0
     
  2. mybestjavascript.com

    mybestjavascript.com New Member

    Joined:
    Sep 15, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
  3. juresh

    juresh New Member

    Joined:
    Sep 11, 2008
    Messages:
    34
    Likes Received:
    0
    Trophy Points:
    0
    Hi

    nice points, so useful for me a noob web designer.

    thanks
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice