Unit3 JSP ACTION N Scriptlets
Unit3 JSP ACTION N Scriptlets
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
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:
}
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:
Example-
main.jsp
<jsp:forward =“otherpage.jsp”>