Important JSP tags

Sanskruti's Avatar author of Important JSP tags
This is an article on Important JSP tags in Java.
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: JSP
    <%@page language="java" %>
  2. Code: JSP
    <%@page language="java" session="true" %>
  3. Code: JSP
    <%@page language="java" session="true" errorPage="error.jsp"  %>
  4. Code: JSP
    <%@page language="java" import="java.sql.*, java.util.*" %>
  5. Code: JSP
    <%@ 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: JSP
<%!

int radius = 7;
double pi = 3.1415;

%>
Example 2

Code: JSP
<%!

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: JSP
<%
    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: JSP
<%!

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
mybestjavascript.com's Avatar, Join Date: Sep 2008
Newbie Member
nice keep it
juresh's Avatar, Join Date: Sep 2008
Go4Expert Member
Hi

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

thanks