JSP – Standard Action

Home/JSP Tutorial/JSP - Elements/JSP – Standard Action
JSP – Standard Action 2017-06-13T10:29:29+00:00

Actions are high-level JSP elements that create, modify or use other objects.

Unlike directives and scripting elements, JSP actions are coded using strict XML syntax form.

By the use of JSP actions elements you can dynamically insert a file, reuse JavaBeans components, forward the user to another page, or generate HTML for the Java plugin etc.

The standard action types are:

(1)<jsp:param>

(2)<jsp:include>

(3)<jsp:forward>

(4)<jsp:fallback>

(5)<jsp:plugin>

(6) <jsp:useBean>

(7)<jsp:setProperty>

(8)<jsp:getProperty>

 

(1)<jsp:param>

The <jsp:param> action is used with other tags to provide additional information in the form of name value pairs.

This action is used in conjunction with jsp:include, jsp:forward and jsp:plugin actions.

Syntax :

<jsp:param name=”parameter_name” value=”parameter_value” />

OR

<jsp:param name=”parameter_name” value=”parameter_value”>

</ jsp:param>

For Example,

<jsp:param name=”font_size” value=”20” />

OR

<jsp:param name=”font_size” value=”20”> </jsp:param >

(2) <jsp:include>

This action allows user to include static or dynamic resource in the JSP at request time.

If page is buffered then the buffer is flushed prior to the inclusion.

Syntax :

<jsp:include page= “file_name” flush = “true|false” />

OR

<jsp:include page= “file_name” flush = “true|false” >

<jsp:param name=”parameter_name” value=”parameter_value” />

</jsp:include >

NOTE : A <jsp:include> action may have one or more <jsp:param> tags in its body, to provide additional name-value pairs.

Included.jsp :-


 


Include.jsp :-

 


Now run the Include.jsp in the appropriate browser so you can get the below output in the browser,
Output:

This is the message of Include.jsp file…………..
This is the message of Included.jsp file…………..

Example-2 : Write a simple program which demonstrate the use of <jsp:param> in <jsp:include> action element.

Employee.jsp :-


 


Information.jsp :-


 


Now run the Information.jsp in the appropriate browser so you can get the below output in the browser,
Output:

Name : Prof. Ashutosh A. Abhangi
He is a H.O.D. of Info. Tech. dept. in Noble Engineering College – Junagadh.
Name : Prof. Kamal N. Kotecha
He is a H.O.D. of C.S.E. dept. in Noble Engineering College – Junagadh.

NOTE : Difference between Directive include and Action include element.

Include

Syntax

Done when

Included Content

Parsing

Directive

<%@ include file=”file_name” %>

Compilation time

Static

Container

Action

<jsp:include page=”file_name” />

Request Processing time

Static or dynamic

Not parsed but included in container

(3)<jsp:forward>

With the forward action, the current page stops processing the request and forwards the request to another page.

Execution never returns to the calling (current) page.

Syntax :

<jsp:forward page= “file_name” />

OR

<jsp: forward page= “file_name”>

<jsp:param name=”parameter_name” value=”parameter_value” />

</jsp: forward>

The meaning as well as use of the attributes and the <jsp:param> element are same as for <jsp:Include> action element.

Example-1 : Write a simple program which demonstrate the <jsp:forward> action element.

Current.jsp :


Forward.jsp :


 


Now run the Current.jsp in the appropriate browser so you can get the below output in the browser,

Output:

Welcome, Forward is working now and this is a Forwarded page……………

Example-2 : Write a simple Login Application by using <jsp:forward> action element.

Login.jsp :


 


Check.jsp :

 


Retry.jsp :

 


Success.jsp :

 


Now run the Login.jsp in the appropriate browser so you can get the below output,

 

Now put the,

User Name : aaaaaa

Password : jjjjjj

And click on Sign in button so you can get the below output in browser,

Output :

Wrong User ID and Password, Please try again by click below link……………………..
Here You Will Find Your Local Host Link

By clicking the Login page link, the Login.jsp page is call and you can put the

User Name : Ashutosh

Password : java

And click on Sign in button so you can get the below output in browser,

Output :

You are successfully login…………….. 

(4)<jsp:fallback>

Syntax :

<jsp:fallback> text message for user </jsp:fallback>

A text message to display for the user if the plug-in cannot be started.

If the plug-in starts but the applet or Bean does not, the plug-in usually displays a popup window explaining the error to the user.

Its use with <jsp:plugin> element.

(5)<jsp:plugin>

 

By the use of <jsp:plugin> you can include an applet and JavaBean in your JSP page. Syntax of <jsp:plugin> is :

 

Syntax :

 

<jsp:plugin   type=”bean|applet
code=”className.class
codebase=”Path of  className.class  after moving it in WebRoot”
[ name=”name of bean or applet instance” ]
[ align=”bottom|top|middle|left|right” ]
[ height=”displayPixels” ]
[ width=”displayPixels” ]
[ jreversion=” version of Java Runtime environment ” ]
…………….. >
[<jsp:params>
<jsp:param name=”parameter_name”
value=”parameter_value” />
……….
</jsp:params>]

            [<jsp:fallback>
text messag……….
</jsp:fallback>]
</jsp:plugin>

Example-1 : Write a simple application in which the applet is used in JSP file by <jsp:plugin> action element.

(1)Create a Applet_Jsp.java file(applet) in default package at src in  your Web Project(JSP_EXAMPLE).

The source code of this java file(applet) are as under,

Applet_Jsp.java :


 


 

(2) Now, Run this applet.

(3) Create one new folder, name is AppletClass in WebRoot.

(3) Now,Copy the Applet_Jsp.class file(compile file/Bytecode file) which is available at ……………workspace\.metadata\.me_tcat\webapps\Your_Project_name\WEB-INF\classes\ Applet_Jsp.class in AppletClass folder in WebRoot.

(4) Create a Applet_Use_In_Jsp.jsp file in JSPFILES folder at your Web Project(JSP_EXAMPLE).

The source code of this jsp file are as under,

Applet_Use_In_Jsp.jsp :


 


 

(6)<jsp:useBean>

Forms are a very common method of interactions in web sites.The standard way of handling forms in JSP is to define a “bean”.

For that you just need to define a class that has a field corresponding to each field in the form.  The class contains the “setters” and “getter” method of the form fields.

By this action element <jsp:useBean>, we use java bean in a JSP page.

Syntax :

Scope of <jsp:useBean> :

1. page: This is default value. It means that we can use the Bean within the JSP page.

It indicates that the bean is only available on the current page (stored in the PageContext of the current page).

2. request: It means that we can use the Bean from any JSP page processing the same request.

A value of request indicates that the bean is only available for the current client request (stored in the ServletRequest object).

3. session: It means that we use the Bean from any Jsp page in the same session as the JSP page that created the Bean.

A value of session indicates that the object is available to all pages during the life of the current HttpSession.

4. application: It means that we use the Bean from any page in the same application as the Jsp page that created the Bean.

A value of application indicates that it is available to all pages that share the same ServletContext.

(7)<jsp:setProperty>

This action sets the value of a bean’s property.

Syntax :

(8)<jsp:getProperty>

This element retrieves the value of a bean property, converts it to a string, and inserts it into the output.

Syntax :

Example : Write a simple program which demonstrate the <jsp:useBean>,<jsp:getProperty> and <jsp:setProperty> action elements.


 


BeanData.jsp :

 


Now run the BeanData.jsp in the appropriate browser so you can get the below output in the browser,

Output :

STUDENT DATA :

Name : Rahul

Email-ID : rahul.kumar@gmail.com

Age : 20

Prev Next