JSP actions are special XML tags which control the behavior of servlet engine. JSP actions allow you to insert a file dynamically, reuse external JavaBean components, forward the request to the other page and generate HTML for Java Applet Plugin.
jsp:include action
JSP include action allows you to include a file at runtime. The syntax of JSP include action is as follows:
1 | <jsp:include page="Relative URL" flush="true" /> |
In the page attribute, you insert a relative URL of a file which could be an HTML file or another JSP page. Unlike the include directive, the jsp include action insert a file at the time page is being requested.
jsp:useBean action
JSP useBean action lets you load a JavaBean component into the page and use it later. JSP useBean action allows you to reuse other Java classes. The syntax of JSP useBean action is as follows:
1 | <jsp:useBean id="objectName" class="package.class" /> |
By using the jsp:useBean action, you create a new object with the object nameobjectName of the class package.class. Later on, you can access the properties of this object by using either jsp:setProperty or jsp:getProperty. Let’s take a look at an example. First, we have a JavaBeans call Message:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public class Message { private String text; /** * @return the text */ public String getText() { return text; } /** * @param text the text to set */ public void setText(String text) { this.text = text; } } |
Then we create a JSP page which uses the jsp:useBean action to access JavaBean Message inside that page.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <html> <head> <title>jsp:useBean Demo</title> </head> <body> <jsp:useBean id="msg" class="com.jsptutorial.Message" /> <jsp:setProperty name="msg" property="text" value="JSP useBean Demo" /> <h1> <jsp:getProperty name="msg" property="text" /> </h1> </body> </html> |
We use jsp:setProperty to set the text property of the JavaBean Message and then we call jsp:getProperty to get that message and print it out. Here is the output screenshot:
jsp:forward Action
jsp:forward action allows you to forward a request to the other page. The syntax of the jsp:forward action is listed as below. There is one attribute called page which value is a page you want to forward the request to. You can specify the page statically or dynamically by using the expression.
1 2 | <jsp:forward page="error.jsp" /> <jsp:forward page="<%= java-expression %>" /> |
jsp:plugin Action
jsp:plugin action allows you to embedded Java Applet into a page. Suppose you have an applet which demonstrates the JSP page life cycle called com.jsp.jspapplet. Here is the way we use jsp:plugin action to embedded that applet into a page:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <html> <head> <title>jsp:plugin Demo</title> </head> <body> <jsp:plugin type="applet" code="com.jsp.jspapplet" codebase="." width="500" height="400"> <jsp:fallback> <p>Unable to use Java Plugin</p> </jsp:fallback> </jsp:plugin> </body> </html> |