0% found this document useful (0 votes)
5 views30 pages

4.0 Actions

JSP Actions refer to built-in and custom tags that perform specific tasks in JSP, including actions like <jsp:include>, <jsp:forward>, and <jsp:useBean>. The <jsp:useBean> action is particularly important for creating and managing JavaBeans, which encapsulate data and promote reusability and separation of concerns. Other actions like <jsp:setProperty> and <jsp:getProperty> are used to manipulate JavaBean properties, while <jsp:forward> allows for resource redirection within JSP applications.
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)
5 views30 pages

4.0 Actions

JSP Actions refer to built-in and custom tags that perform specific tasks in JSP, including actions like <jsp:include>, <jsp:forward>, and <jsp:useBean>. The <jsp:useBean> action is particularly important for creating and managing JavaBeans, which encapsulate data and promote reusability and separation of concerns. Other actions like <jsp:setProperty> and <jsp:getProperty> are used to manipulate JavaBean properties, while <jsp:forward> allows for resource redirection within JSP applications.
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/ 30

JSP Actions

• In JSP, "Actions" typically refer to JSP actions or custom


tags that perform specific tasks or actions.

• There are several built-in actions available in JSP, such as


<jsp:include>, <jsp:forward>, <jsp:useBean>,
<jsp:setProperty>, and more.

• As shown above, these actions use constructs in XML


syntax to control the behavior of the servlet engine

• We can also create custom actions using Java classes or


libraries.
JSP Actions

The <jsp:include> Action


• This action lets you insert files into the page being generated.
The syntax looks like this −

<jsp:include page = "relative URL" flush =


"true" />

• Unlike the include directive, which


inserts the file at the time the JSP
page is translated into a servlet, this
action inserts the file at the time the
page is requested.

• Check out the example


show.jsp

date.jsp

• Run “show.jsp”  output


JSP Actions

The <jsp:useBean> Action


• The <jsp:useBean> action in JSP is used
to create or retrieve a JavaBean and
make it available for use within a JSP
page.

What are “JavaBeans” ?

• JavaBeans are reusable and maintainable Java classes that


encapsulate data and provide getter and setter methods for
accessing and modifying that data.
JSP Actions
The <jsp:useBean> Action

Why Use “JavaBeans” ?


Encapsulation

• JavaBeans encapsulate data and behavior,


promoting a clean separation of concerns.

• They allow us to package related data and


methods into a single, reusable component.
Reusability

• Once we create a JavaBean, we can use it


across multiple JSP pages or servlets
without duplicating code.

• This promotes code reusability and


maintainability.
Abstraction

• JavaBeans abstract away the complexities of


data management.

• Instead of directly manipulating data in our


JSP page, we can work with JavaBean
properties, which provides a higher level of
abstraction.
Separation of Concerns

• JavaBeans allow us to separate our data


model from the presentation logic in our
JSP pages.

• This separation of concerns is a


fundamental principle of good software
design and makes our code easier to
manage and maintain
Scoped Data

• We can control the scope of a JavaBean


(e.g., request, session, application) to
manage the lifecycle of the data it
represents.

• This ensures that data is available where


and when it's needed, without unnecessary
duplication.
JSP Actions
The <jsp:useBean> Action

<jsp:useBean> Example
Steps :

1) We will create a JavaBean in Java and create a class file


2) Later, we have to put the class file in “tomcat\webapps\
Root\WEB-INF\classes\<java-package-name>” folder.
Above “<java-package-name>” must be replaced by
the package name the JAVA file resided before
compilation.
3) After that, we will create a “.jsp” file that will load our
JavaBean.
The Getter method

The Setter method

1) Create a new test(package)  TestBean.java  type the


above code  “Clean and Build “ the Java project to
create the class file (the class file can be found in
“<JavaProjectName>\build\classes\<packageName>”

2) In the above code, the class file will be in


“<javaProjectName>\build\classes\test\”
What are Getter and Setter methods ?
What are Getter and Setter methods ?
Getter methods are used to access the values of the
properties within the JavaBean. They allow you to read the
state of the bean

Setter methods are used to modify the values of the


properties within the JavaBean. They allow you to update the
state of the bean.

They provide a standardized way to access and modify the


properties of the bean

It is not mandatory for a JavaBean code file to have both getter


and setter methods, but it is a common convention and a best
practice
What are Getter and Setter methods ?

Property Naming Conventions


• In programming languages, getter and setter methods are
named according to the properties they represent.

• For example, for a property named name, the getter


method would be getName() and the setter method
would be setName(String name).

• Adhering to these conventions makes your JavaBeans


more consistent and easier to work with.
1) Put the “TestBean.class” file in the following path :

1) There must be a folder with name of the package at the


path “…WEB-INF\classes\....” (in this case, “test”) and
the class file must be put inside it.  Next  Write .JSP
script to use the bean
<jsp:setProperty> action sets
the property of a Bean.

<jsp:getProperty> action is
used to retrieve the value of a
given property and converts it
to a string.

Above, the name attribute of <jsp:setProperty> and <jsp:getProperty>


must match the ID of <jsp:useBean id>
Running the .JSP file with the Java Bean
JSP Actions

The <jsp:forward> Action


• The <jsp:forward> action in JSP is used to
forward a request to another resource, such
as another JSP page, servlet, or URL.

• In the next example, run “main.jsp” and it will


forward the request to “destination.jsp”

• Check out the example


• Output 
Output
JSP Actions

The <jsp:plugin> Action


• The <jsp:plugin> action was used in older versions of JSP to
include browser-specific code for embedding applets or
other plugins in a JSP page.

• However, modern web development typically relies on


HTML5 and other technologies for embedding media or
interactive content, and the use of Java applets, which were
commonly embedded with <jsp:plugin>, has significantly
declined.

You might also like