Switch to full style
What's behind JSP & Servlets
Post a reply

Important JSP tags

Thu Oct 09, 2008 11:45 pm

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.

Code:
<%@page language="java" %>

Code:
<%@page language="java" session="true" %>

Code:
<%@page language="java" session="true" errorPage="error.jsp"  %>

Code:
<%@page language="java" import="java.sql.*, java.util.*" %>

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




Post a reply
  Related Posts  to : Important JSP tags
 Important : Read before you post .     -  
 Usage of the CSS property !important     -  
 Java Important interview questions     -  
 web hosting sites free check out important useful     -  
 Get the important variables of random forest classifier     -  
 HTML Tags -2     -  
 help in meta tags     -  
 PHP Start and End Tags     -  
 Examples of heading tags     -  
 H1 H2 H3 H4 H4 H5 H6 heading html tags     -