Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Servlet provides a lot of implicit benefits for improving applications. JSP applications can take advantage of these benefits. JSP Actions labels allow the software engineer to take advantage of these capabilities.
JSP Actions are defined using the XML format. If they contain actions, they get re-evaluated each time a page is visited, making them different from directives.
What are JSP actions?
Action tags are defined in the JSP specification and can be used within JSP code to remove or eliminate Scriptlet code as they are outdated and aren't used today. JSP Actions can be implemented to define XML labels in the JSP pages. During the execution or solicitation handling stage, these inside labels used the functionality of the servlet-programming interface.
It is possible to use many JSP action tags or elements, and each has its characteristics and uses. These tags are implemented to perform particular functions.
As well as implementing the action tag, it employs a Java Bean to control flow between pages. Under the XML standard, the syntax for the action element is as follows:
<jsp:(action_name) attribute = "content" />
Types of JSP Actions
<jsp:include>
Unlike the JSP directive, the jsp:include Action tag is different. Include directives contain resources at the time of translation, whereas include actions control resources at the request. It has static assets like HTML and other JSP pages to prepare a customer request.
Under include, there are two attributes:
Page: The page contains the value of the URL of the resource.
Flush: Prior to including a resource, it checks that its buffer has been flushed.
<b>new.jsp</b><br>
<h3> Here is the content of next.jsp webpage</h3>
Output:
<jsp:usebean>
This Action is used to create a bean class (many objects in one object) within a JSP page.
It has the following attributes:
Id: Uniquely identifies a bean in a given scope.
Class: This creates a bean object.
Type: It specifies the type of Bean residing in the scope. Classes and beans are both named with this property.
BeanName: Instantiates a bean using java.beans.Beans.instantiate().
<jsp:setProperty>
A Bean's property can be set with this action tag. You may need to specify the Bean's unique name when using this action tag (it is nothing more than the id value of the useBean action tag).
Four attributes are present: name, property, value, and parameter.
<jsp:getProperty>
A bean's name and Bean must be defined to obtain its properties. This function aims to determine the value of the bean property set by setProperty, where the property represents the name of the bean property.
The attributes used are: getProperty are name, property.
info.java
package jsp.com;
public class Info {
public Info() {
}
private String name;
private int age;
public String getname() {
return name;
}
public void setname(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
index.jsp
<html>
<head><title>
useBean action
</title></head>
<form action="Information.jsp" method="post">
Enter your name: <input type="text" name="name"><br>
Enter your age: <input type="text" name="age"><br>
<input type="submit" value="Submit">
</form>
</html>
We create a main class in the Java file info.java, and we invoke useBean in the JSP file. Therefore, the information.jsp instantiates the object, referenced by its id. The details entered are displayed.
<jsp:forward>
Redirecting the request is accomplished using <jsp:forward>. When encountered on a JSP page, the control is transferred to the page mentioned in this Action.
index.jsp
<html>
<head>
<title>Forward action </title>
</head>
<body>
<a>Clicked this page but forwarded to the next.jsp page</a>
<jsp:forward page="next.jsp" />
</body>
</html>
next.jsp
<html>
<title>Forward action</title>
</head>
<body>
<a>Here is the forwarded page.</a>
</body>
</html>
Output:
After executing the after-action tag in the index.jsp, the request is sent to next.jsp.
<jsp:param> Action
You can use this Action to pass parameters to other JSP action tags such as JSP include and JSP forward. New JSP pages will access those parameters by utilizing the request object directly.
The above example shows index.jsp passing company and position to next.jsp and next.jsp calling getParameter() to access these parameters.
<jsp:plugin>
In the client browser, the <jsp:plugin> is used to display objects, such as applets and beans. Based on the browser version, the <jsp:plugin> element is either replaced by the <object> element or by the <embed> element.
The attributes of the item are:
Code: This is the name of the Java class file that the plugin will attempt to execute.
CodeBase: Plug-in code will be executed from the codebase, the directory where the java class file is located.
Type(applet/bean): Plug-ins will execute this object type. Objects must either be beans or applets.
package jsp;
import java.applet.*;
import java.awt.*;
public class Draw extends Applet{
public void paint(Graphics g){
g.drawString("Jsp Plugin Action",40,20);
}
}
In the index.jsp we are using plugin attributes type, code, and codebase. Whereas in Draw.java, we have used paint(Graphics g) to display the" JSP Plugin Action".
<jsp:body>, <jsp:element> and <jsp:attribute>
<jsp:body>: Gradually, it can be utilized to describe the XML. The system labels the components and can produce during demand rather than accumulation times.
<jsp:element>: Provides dynamically defined XML elements.
<jsp:attribute>: It is used to set the characteristics of XML elements dynamically defined.
Write some tasks performed by JSP Actions. An action on a JSP page is one of the three most essential elements. It performs some specific tasks and is predefined. Functionalities provided by them are: - It will insert a file dynamically - It controls the servlet engine's behavior - It helps the user to forward to some other page. - Maintains the flow between the pages
How do the attributes id and scope in the action elements work? - Id: The action element id identifies an action that can be referenced by the JSP page, by giving it an id attribute. The implicit object PageContext can be used to reference the object created by the Action if an instance is created of the object. - Scope: This attribute identifies how the Action element will be processed. A connection exists between the id and scope attributes, as the scope attribute determines how long the associated object will live. The scope attribute can have four values: (a) page, (b) request, (c) session, and (d) application.
Why are JavaBeans important in JSP? JavaBeans are used instead of JSP code to create separate java classes to create dynamic web pages. Property values can be retrieved and set using the getter and setter methods. Sharing objects between different Web pages are accessible when using JavaBeans.
How do you develop JSP beans with three tags? An example of its use is in jsp:plugin. In order to develop beans, the following tags are used: <jsp:useBean>, <jsp:setProperty> and <jsp:getProperty>.
Key Takeaways
Let's have a look at what we have learned in this blog:
The servlet engine is controlled by JSP actions written in XML syntax.
In addition to dynamically inserting a file, we can reuse the components of the Bean and move the user to another page. By using include and forward actions in JSP. Each time the page is accessed, actions are re-evaluated in contrast to directives.
In JSP, there are 12 types of Standard Action Tags.
Start learning more about Web development by visiting our guided path.