- Java EE Tutorials
- JSP Tutorials
- Recommended Books for Java Server Pages (JSP)
JSP actions are XML syntax tags used to control servlet engine.The Action tags are used to control the flow between pages and to use Java Bean. We can insert file, forward one page to another page or we can create HTML page for java plugin. Jsp action tags are listed in the table below:
- <jsp:plugin>Generates HTML code that contains the appropriate client browser-dependent elements (OBJECT or EMBED) needed to execute an applet with the Java plugin software.
JSP Action Tag | Description |
---|---|
<jsp:forward> | It is used to forward request to another resource or page .It may be html page,jsp page or other resource. |
<jsp:include> | Used to include the relative URL at the execution time (not at compile time). |
<jsp:usebean> | Makes a Javabean component available in a page. |
<jsp:setProperty> | sets s Javabeans property value. |
<jsp:getProperty> | It is used to retrieve value of given property from Javabeans component and add it to response. |
Now let us write examples for each of the above tags in the following sections.
jsp:forward
It is used to forward request to another page. For example, we will forward request from mainpage.jsp to welcome.jsp page as demonstrated in the example below:
Listing 1:mainpage.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% out.print("This the main page");%> <jsp:forward page="welcome.jsp"/> </body> </html>
Listing 2:welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h2>Welcome to Javabeat</h2> <% out.print("This is page where control is forwarded to");%> <p>Today's date: <%= (new java.util.Date())%></p> </body> </html>
Output
Execute the above mainpage.jsp in Eclipse by selecting Run As > Run On Server, output as below would be seen:
As you can see the contents of the main page i.e mainpage.jsp is discarded and only the content from welcome.jsp is displayed.
jsp:include
This tag is used to include files such as jsp, html or servlet. For example,we will take above file welcome.jsp to be include in includeexample.jsp
Listing 3:includeexample.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h2>welcome.jsp is included on this page.</h2> <jsp:include page="welcome.jsp"/> </body> </html>
Listing 4:welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h2>Welcome to Javabeat</h2> <% out.print("This is page where control is forwarded to");%> <p>Today's date: <%= (new java.util.Date())%></p> </body> </html>
Output
Execute the above includeexample.jsp in Eclipse by selecting Run As > Run On Server, output as below would be seen:
jsp:usebean
It is used to initialize bean class. If object is not created then it creates specified object. Syntax of jsp:usebean tag is as follows:
<jsp:useBean id="name">.. </jsp:usebean>
Where
- id: It is used to identify bean name
- class: Creates object of bean class
Following example demonstrates use of this tag:
Listing 5: SampleBean.java
package javabeat.net.jsp.actions; public class SampleBean { public int square(int n){return n*n;} }
Listing 6: usebeanexample.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <jsp:useBean id="obj"/> <% int m=obj.square(10); out.print("cube of 10 is "+m); %> </body> </html>
Output
Execute the above usebeanexample.jsp in Eclipse by selecting Run As > Run On Server, output as below would be seen:
jsp:setProperty
It is used to set property value in bean. Syntax is as follows:
<jsp:useBean id="someName" ... > ... <jsp:setProperty name="someName" property="someProperty" .../> </jsp:useBean>
Following is the list of attributes associated with setProperty action:
- name- The name of the bean whose property will be set.
- property-Indicates the property you want to set.
- value- This is the value that will be assigned to the property.
- param- It is the name of the request parameter whose value the property is to receive.
jsp:getProperty
It is used to get value of property. Syntax is as follows:
<jsp:useBean id="someName" ... > ... <jsp:getProperty name="someName" property="someProperty" .../> </jsp:useBean>
Following is the list of attributes associated with getProperty action:
- name- The bean name whose property value is to be retrived.
- property- The name os property whose value is to be retrieved.
Now let us see an example which uses both the action tags jsp:setProperty and jsp:getProperty:
create on java file. Save it as SampleBean.java
Listing 7: SampleBean.java
package javabeat.net.jsp.actions; public class SampleBean { String someMessage; public String getSomeMessage() { return someMessage; } public void setSomeMessage(String someMessage) { this.someMessage = someMessage; } }
Now create one jsp file and save it as gettersetterexample.jsp
Listing 8:gettersetterexample.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <jsp:useBean id="testtag" /> <jsp:setProperty name="testtag" property="someMessage" value="Hello Javabeat" /> <jsp:getProperty name="testtag" property="someMessage" /> </body> </html>
Output
Execute the above gettersetterexample.jsp in Eclipse by selecting Run As > Run On Server, output as below would be seen:
<jsp:plugin> can tried by using it in some applet.
Previous Tutorial : JSP Syntax || Next Tutorial : JSP Directives