0% found this document useful (0 votes)
12 views

Unit3 JSP ACTION N Scriptlets

The document discusses JSP scripting elements, action tags, and expressions. Scripting elements like scriptlets and declarations embed Java code in JSP pages. Expression tags evaluate expressions and output values. Common action tags include jsp:include to dynamically insert pages and jsp:forward to redirect between pages.

Uploaded by

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

Unit3 JSP ACTION N Scriptlets

The document discusses JSP scripting elements, action tags, and expressions. Scripting elements like scriptlets and declarations embed Java code in JSP pages. Expression tags evaluate expressions and output values. Common action tags include jsp:include to dynamically insert pages and jsp:forward to redirect between pages.

Uploaded by

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

JSP

SCRIPTING ELEMENTS
&
ACTION TAGS
JSP SCRIPTING ELEMENTS

• JSP scripting elements are one of the most vital elements of a JSP code. These <% %> tags contain JSP scripting elements. These tags are of
the utmost importance as, at the time of translation, the JSP engine will consider anything inside these tags. Only this code will convert to Java
code. Code other than this is plain or HTML text.
• The scripting elements thus help to embed java code to the HTML, CSS, JavaScript code. There are three main subdivisions of scripting
elements in JSP.
1. Expression Tags
2. Scriptlet Tags
3. Declaration Tags
• They all provide different ways to include java code in a JSP file.
1.Scriptlets in JSP

Scriptlet is a tag that is used to write java source code to


implement business logic. This Scriptlet tag is declared
inside the _jspService() method. _jspService()method
always create new object for every new request. So as
Scriptlet tag is inside the _jspService() method it will also
create new object for every new request.

JSP Scriptlets help in embedding Java in HTML for JSP


code. <%___%> tags contain the Java code and are
called as scriptlet tags. Unlike expressions, scriptlets
have a set of statements in Java language. It allows
writing java code in it.
The Syntax of Scriptlet tag is as follows:
<% statement; statement ;….%>
The code inside a scriptlet tag goes to the _jspService() Explanation
method of the generated servlet for processing the The above code shows the use of a scriptlet tag that embeds the java code in it. The code inside the scriptlet
request. For each request sent, the _jsp service() method tag will go _jspService() method and will become pure java code. The following is the output.
will invoke. If multiple scriptlets are present in the code,
then they are appended to this service method in an
ordered fashion.
2.JSP Expressions
a. expression.jsp In this example, we can see the use of expression to get
the result of the logical expression. The result is shown
<html> in the output below.
<head><title>Expressions</title>
• JSP Expression tags, as the name suggests, evaluates </head>
expressions. They incorporate arithmetic and logical <body>
expressions so that they can be evaluated. They form <h3>--DataFlair--</h3>
an easy means to access the value of a Java variable. <h3>Use of Expressions in JSP</h3>
The Expressions in JSP can be an access point for <% String s1="expressions: done by kajal"; %>
other expressions and merge that value in an HTML <% out.print(s1); %> </br>
file. They can easily access data stored in applications.
<%=s1.toUpperCase() %>
• Syntax of the expression is: </body>
</html>
<%=expression/statement %>
• This code will get written to the output stream of the
response.

• These expressions or statements can be any valid Java


statement. This code gets converted to out.print( ) b. expression2.jsp
Contrary to the first example, we can see the use
statement when an auto generated servlet gets formed. Use
of expressions generates scriptless code. <html> of arithmetic in the JSP code. The output is
<head><title>Expressions2</title> below.
</head>
<body>
<h3>--DataFlair--</h3>
<h3>Use of Expressions in JSP</h3>
<% int n1=10, n2=20; %>
<% out.print("Addition of numbers is ="); %>
<%=n1+n2 %>
</body>
</html>
3. JSP Declarations
Example of declaration in JSP

• Declarations in JSP declare java language statements.


<%!___%> tags contain declarations. They can
declare classes/instances/inner
classes/variables/methods.
• Unlike the other two, this code doesn’t go to
_jspService() method. This code rather goes to the
source file that gets generated outside the _jspService
method.
• Syntax for declarations are:
<%! statement,[statement,….] %>
• Declarations have no access to implicit objects as they
don’t appear in the service method. A declaration will
remain outside the service method. If a method Explanation:
declared under declaration wants to access an object In this code, we declared num1 and num2. Then we performed calculations on it. It will thus generate
from scriptlet then it needs to pass the object as the following output.
parameter.
DIFFERENCE BETWEEN JSP SCRIPTLET AND DECLARATIVE TAG

Expression tag is used to display output of the JSP application. Scriptlet tag is used to include Java source code.
Scriptlet tag is mostly used for Form action pages implementation.
JSP ACTION
• JSP Actions are one of the three important elements of a JSP page. JSP actions use the
construct in XML syntax to control the behavior of the servlet engine. We can
dynamically insert a file, reuse the beans components, forward user to another page, etc.
through JSP Actions like include and forward. Unlike directives, actions are re-evaluated There are in all twelve JSP action tags. They are:
each time the page is accessed. 1. jsp:include
2. jsp:useBean
• They perform some specific tasks and are predefined. They provide functionalities like-
3. jsp:setProperty
• Dynamic insertion of a file 4. jsp:getProperty
5. jsp:forward
• Controlling behavior of the servlet engine 6. jsp:plugin
• Forwarding a user to another page 7. jsp:body
8. jsp:text
• Controlling flow between pages 9. jsp:element
10. jsp:param
<jsp:action_name attribute=”value”/> 11. jsp:attribute
12. jsp:output
There are two important attributes that are common to all actions.
They are id and scope.
id refers the object uniquely created by actions,
scope tells about the life of that object. The scope attribute has four values. They
are page, application, session, and request.
<jsp:include>

• It also used to insert a jsp file into another file, just like
including Directives.
• It is added during request processing phase
• This action will include the required resources like html, servlets and
JSP.
This jsp:include action is different from jsp directive. Include directive
includes resources at the time of translation, whereas include action
includes resources dynamically at request time. Action directives work
well for static pages, whereas later works better for dynamic pages.
There are two attributes under include:
• Page: its value is the url of the required resource.
• Flush: it checks that the buffer of the resource is flushed before it is
included.

Syntax of jsp:include
<jsp:include page="page URL" flush="true/false">
Advantages of include actions are:
• It is best for dynamic pages. Explanation:
• It promotes code reusability as we include pages In this example we can see that main.jsp includes date.jsp that gives date and time, whereas
date.jsp contains the code to retrieve the date.
• This saves time as we can use one page again and again.
Output:
• It can be created with or without parameters.
<jsp:usebean>

square.java
• This action name is used when we want to use beans in the JSP page. Package demotest;
public class square
• With this tag, we can easily invoke a bean. {
• If we want to instantiate bean (many objects encapsulated in one object) public int square(int n)
class in our JSP page, then this action comes into play. {return n*n;}
}
• It has following attributes:
•id: id uniquely identifies bean in a specified scope. example.jsp
•Scope <html>
1.Page: It is the default scope that states that we can use bean within this JSP <title>usebean</title>
page. <body>
2.Request: It is broader than a page as bean can be used from any page that <jsp:useBean id="obj" class="demotest.square"/>
processes similar requests. <%
3.Session: It has a wider range than above as bean can be used from any page in int m=obj.square(3);
theid: id uniquely identifies bean in a specified scope. out.print("square of 3 is "+m);
4.Application: It has the maximum range as bean can be used from any page %>
present in the same application. </body></html>
• Class: Creates object of bean class.
Explanation:
• Type: It gives the data type to the bean existing in the specified scope. It is In the java file square.java we prepare a basic class and in jsp file we
used with class and beanName. invoke useBean. The object is thus instantiated, referenced through id. We
• beanName: Uses the java.beans.Beans.instantiate() method and instantiates generate the square of the given number.
bean.
Output:
Syntax-
<jsp:setProperty>
<java:getProperty>

This action gets the value of the property and gives it out on the output as a
• jsp:setProperty and getProperty are used with usebeans. It is used to string.
modify the properties of beans. It gets executed when new objects
are created. This property is used to get the property of the bean.
It converts into a string and finally inserts into the output.
• This has four attributes: To get the property of a bean, bean and its name must be defined. Our main
purpose is to get the value of the property where the property is the name of
1. name: It is literally the name of the bean whose property we want to
the bean property that was set by setProperty as well. The attributes used by
set. It has the same name as the object we instantiated using
usebean. getProperty are
•name: It gets the name of the bean whose property we need to get as its
2. property: sets the property of the bean “*” means all the requests value.
that match bean properties are included. •property: It defines the name of the property required for the bean.
3. value: gives specific value to the bean.
Syntax:
4. Param: a name that will fetch the value .
<jsp:setProperty name="bean" property="message" value="Myexample" />
Syntax- <jsp:getProperty name="MyPage" property="message" />
<jsp:setProperty name=“bean” property= “*”|
property=“propertyName” value= “<%=expression>”|
property=“propertyName” param= “Parametername”
TestBean.java
package demotest; Explanation:
import java.io.Serializable; Firstly, a TestBean class of package demotest that implements serializable
public class TestBean implements Serializable class has been defined that gets and sets the message using getters and setters.
{ In jsp code, we used useBean action that will instantiate the object myBean.
private String message = "null"; Then we set the property of the object and give it a value “GetSetAction”.
Thereafter, using getProperty we fetch the value for myBean that was set as
public String getMessage() { GetSetAction as our output.
return msg; Output:
}

public void setMessage(String message) {


this.message = message;
}
}

GetSet.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>Get Set Property</title>
</head>
<body>
<jsp:useBean id="myBean" class="demotest.TestBean" />
<jsp:setProperty name="myBean" property="message" value="GetSetActions" />
<jsp:getProperty name="myBean" property="message" />
</body>
</html>
<jsp:forward> Output:

It is used to forward the request to another jsp or any static page.


Here the request can be forwarded with no parameters or with
parameters.
Syntax-
<jsp:forward page="value">

Example-
main.jsp
<jsp:forward =“otherpage.jsp”>

Explanation: User calls main.jsp but gets forwarded to other.jsp


First.jsp
<jsp:plugin> <html>
<head>
<title>Plugin Param </title> Explanation: In the First.jsp we are using plugin
Plugin inserts java objects or embed tags into a </head> attributes type, code and codebase. In that we are having
JSP page. It has three attributes: <body> param child objects that get the name and value for the
plugin. Whereas in Colour.java, we have used getters
<jsp:plugin type="bean" code="colour.class"
1. type: applet/bean and setters to get and set the values of name and id.
codebase="demotest.colour">
2. code: It is the name of the class file. <jsp:params>
<jsp:param name="id" value="5" />
3. codebase: It is the directory of the name of <jsp:param name="nameofcolour" value="red" />
the class file.
</jsp:params>
Syntax- </jsp:plugin>
</body>
<jsp:plugin type="bean/applet" </html>
code="className" codebase="package.class">
Colour.java
<jsp:param> package demotest;
import java.io.Serializable;
It sends parameters to a bean or applet. It public class Colour implements Serializable {
can also be said as the child object of a public String getName () {
plugin. return name;
Syntax: }
<jsp:params> public void setName (String name) {
<jsp:param name=“__” value="__"/ > this.name = name;
</jsp:params> }
public int getId() {
return id;
}
public void setId (int id) {
this.id = id;
} //getters and setters
private String name = "null";
private int id = 0;
}
1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2. pageEncoding="ISO-8859-1"%>
<jsp:body>and <jsp:element>
3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
•This tag is used to define the XML dynamically i.e.,
4. <html>
the Elements can generate during request time than
5. <head>
compilation time.
6. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
•It actually defines the XML, which is generated dynamically
7. <title>Action Guru JSP6</title>
element body.
8. </head>
Explanation of the code:
9. <body>
Syntax: Code Line 10: Here we are defining element, which
10. <jsp:element name="GuruXMLElement">
dynamically generated as XML, and its name will
11. <jsp:attribute name="GuruXMLattribute">
<jsp:body>..... be GuruXMLElement
12. Value
</jsp:body> Code Line 11-13: Here we are defining an attribute
13. </jsp:attribute>
which will XML attribute of the dynamically
14. <jsp:body>Guru XML</jsp:body>
generated XML.
15. </jsp:element>
Code Line 14: Here we have body action where we
16. </body>
<jsp:attribute></jsp:attribute> are writing the XML body which will be generated
17. </html>
in dynamically XML.
•This tag is used to define the XML dynamically i.e. the elements
can be generated during request time than compilation time
•It actually defines the attribute of XML which will be generated
dynamically. Output:
Here we get the output from the body tag of generated XML.
Syntax:
<jsp:attribute name="Put your XMLattribute here..">
</jsp:attribute>
jsp:text
•It is used to template text in JSP pages.
•Its body does not contain any other elements, and it contains only text
and EL expressions.
Syntax:
<jsp:text>template text</jsp:text>
Here template text refers to only template text (which can be any
generic text which needs to be printed on jsp ) or any EL expression.

1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"


2. pageEncoding="ISO-8859-1"%>
3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4. <html>
5. <head>
6. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7. <title>Guru Action JSP7</title>
8. </head>
9. <body>
10. <jsp:text>Guru Template Text</jsp:text> Explanation of the code:
11. </body> Code Line 10: Here we are taking text object to print the template text
12. </html> When you execute the above code, you get the following output
jsp:output
•It specifies the XML declaration or the DOCTYPE
declaration of jsp
•The XML declaration and DOCTYPE are declared by
the output
Syntax-
<jsp:output doctype-root-element="" doctype-
system="">

Here, doctype-root-element indicates the root element of


XML document in DOCTYPE.
Doctype-system indicates doctype which is generated in
output and gives system literal

1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>


2. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
3. <html>
4. <head>
5. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6. <title>Action Guru JSP8</title>
7. </head>
8. <body> Explanation of the code:
9. <jsp:output doctype-root-element="html PUBLIC" Code Line 9: Here we are using output action object to
doctype-system="http://www.w3.org/TR/html4/loose.dtd"/> generate a DOCTYPE, and internally it will be generated in this
10. </body> format:
11. </html> <!DOCTYPE html “http://www.w3.org/TR/html4/loose.dtd”>

You might also like