0% found this document useful (0 votes)
299 views4 pages

JSP Tags and Action

The document discusses different tags used in JSP: 1) Declaration tags declare variables and methods in JSP pages. 2) Expression tags write Java expressions that are evaluated and inserted into the JSP. 3) Scriplet tags write Java code that will be included in the servlet's service method. 4) Directive tags configure the generated servlet code, including importing packages and setting MIME types.

Uploaded by

Shobha Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
299 views4 pages

JSP Tags and Action

The document discusses different tags used in JSP: 1) Declaration tags declare variables and methods in JSP pages. 2) Expression tags write Java expressions that are evaluated and inserted into the JSP. 3) Scriplet tags write Java code that will be included in the servlet's service method. 4) Directive tags configure the generated servlet code, including importing packages and setting MIME types.

Uploaded by

Shobha Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Declaration tag: Whenever we use any variables as a part of JSP we have to use those

variables in the form of declaration tag i.e., declaration tag is used for declaring the variables
in JSP page.

Syntax:
<%! Variable declaration or method definition %>

Example

<%!
int a = 10, b = 30;
int count() {
return (a + b);
}
%>

Expression tag: Expression tags are used for writing the java valid expressions as a part of
JSP page. A JSP expression element contains a scripting language expression that is
evaluated, converted to a String, and inserted where the expression appears in the JSP file.

Syntax:

<%= java valid expression %>

<%! int a = 10, b = 20 %>


<%= a + b%>

Scriplet tag: Scriplets are basically used to write a pure java code. Whatever the java code
we write as a part of scriplet, that code will be available as a part of service () method of
servlet.
Syntax:

<% pure java code%>

<html>
<title>Print Numbers 1-10</title>
<head>Numbers 1-10</head><br>
<body>
<%
for (int i = 1; i <= 10; i++) {
out.println (i);
}
%>
</body>
</html>

Directives:
Directives are basically used to configure the code that is generated by container in a servlet.
As a part of JSP we have three types of directives; they are page directives, include directives
and taglib directives.

<%@
page attribute_name1=attribute_value1,
attribute_name2=attribute_value2,
....................................,
Attribute_nameN=attribute_valueN
%>

Attribute Attribute value


name

import This attribute is used for importing either pre-defined or user- defined
packages. The default value is java.lang.*

For example:

<%@ page import="java.sql.*, java.io.*" %>

contentType . This attribute is used for setting the MIME type (plain/text, img/jpeg,
img/gif, audio/wave, etc.). The default value is text/html.
For example:

<%@ page contentType="img/jpeg" %>

language This attribute represents by default java i.e., in order to represent any
business logic a JSP program is making use of java language. The attribute
language can support any of the other programming languages for
developing a business logic at server side.
For example:

<%@ page language="java" %>


Include directives:
Include is the directive to include the server side resource. The server side resource can be
either an html file or JSP or a servlet. If we include html file, it will be executed by browser
when the response is rendering to the client. When we include a JSP or a servlet, it will be
executed by container.

Syntax:

<% include file = "file name to be included" %>


For example:

<% include file = "copyright.html" %>

<%@ taglib ... %>


Declares a tag library, containing custom actions, used in the page

Standard Actions:
These are basically used to pass runtime information to the container. As a part of JSP we
have the following standard actions; they are <JSP:forward/>, <JSP:include/>, <JSP:param/>,
<JSP:useBean/>, <JSP:setProperty/> and <JSP:getProperty>.
1. <JSP:forward/>:

When we want to forward a request and response to the destination JSP page from the
source JSP we must use <JSP:forward>.

Syntax:
Without body:

<JSP:forward page="relative or absolute path of JSP page"/>

With body:

<JSP:forward page=" relative or absolute path of JSP page">


<JSP:param name="param name1" value="param value1"/>
<JSP:param name="param name2" value="param value2"/>
</JSP:forward>

For example:

<JSP:forward page="y.jsp">
<JSP:param name="v1" value="10"/>
<JSP:param name="v2" value="20"/>
</JSP:forward>

When we use this tag we get the response of destination JSP page only but not source
JSP page.

2. <JSP:include/>:

This tag is used for processing a client request by a source JSP page by including
other JSP pages and static resources like html's. One source JSP can include 'n'
number of server side resources and finally we get the response of source JSP only.

Syntax:
Without body:

<JSP:include page="relative or absolute path of JSP page"/>


With body:

<JSP:include page=" relative or absolute path of JSP page">


<JSP:param name="param name1" value="param value1"/>
<JSP:param name="param name2" value="param value2"/>
</JSP:include>
For example-1:

<JSP:include page="y.jsp">
<JSP:param name="v1" value="10"/>
<JSP:param name="v2" value="20"/>
</JSP:include>

You might also like