To understand param attribute,we will have to know its need.
What is the benefit we are getting by using this.To understand that lets explore some scenarios.
Suppose you are going directly from HTML form to JSP,then how can we set bean property
with the request parameter?
We can do it by using standard action tag and scripting.
Lets have a look.
GetData.html
First way to do it.
by setting some parameter.
Is there a way where without scripting we can achieve the goal??Yes there is.
param Attribute comes to picture.
By using param attribute,we set the bean property with the request parameter just by specifying the name of the request parameter.
It becomes better if we have same name for request Parameter and bean property.
and if we have all the properties of bean having the same name
as request paramteres then instead of setting each property separately,we can use
We can pass parameter names and values to forwarded file by using a <jsp: param> tag.
It is mostly used as a child tag inside an action tag.
It is used in forward and include mostly.
There can be multiple <jsp: param> tag inside the <jsp: forward> tag if we need to pass more than one parameter to the target file. The name attribute specifies the parameter name and takes a string literal as a value.
This tag contains only one parameter page = "relativeUrl".
This relativeUrl represents the file to which the request will be forwarded.
The target can be any html,jsp or servlet.
jspParamExample.jsp
Suppose you are going directly from HTML form to JSP,then how can we set bean property
with the request parameter?
We can do it by using standard action tag and scripting.
Lets have a look.
GetData.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML> <BODY> <FORM METHOD=POST ACTION="RetrieveRequestParameters.jsp"> Enter name <input type=text name=username size=20> Enter address <input type=text name=useraddress size=20> Enterid <input type=text name=userid size=4> <P><INPUT TYPE=SUBMIT> </FORM> </BODY> </HTML>UserData.java
package user; public class UserData { String username; String useraddress; int userid; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUseraddress() { return useraddress; } public void setUseraddress(String useraddress) { this.useraddress = useraddress; } public int getUserid() { return userid; } public void setUserid(int userid) { this.userid = userid; } }
First way to do it.
<jsp:useBean id="obj" class="User"/> <% obj.setName(request.getParameter("username"));%>Second way to do it.
<jsp:useBean id="obj" class="user.UserData"/> <jsp:setProperty name="obj" property="username" value=<%= request.getParameter("username")%>In both the solution, we used scripting to accomplish the task.Here I am atrying to say that we are using scriptlet or expressions for achieving our goal.But it would be greater if we could do it
by setting some parameter.
Is there a way where without scripting we can achieve the goal??Yes there is.
param Attribute comes to picture.
By using param attribute,we set the bean property with the request parameter just by specifying the name of the request parameter.
It becomes better if we have same name for request Parameter and bean property.
... <FORM method=post action="RetrieveRequestParameters.jsp"> Name <input type=text name=name size=20> Id <input type=text name=userid> <P><input type=submit> </FORM> ...Now we can say directly
and if we have all the properties of bean having the same name
as request paramteres then instead of setting each property separately,we can use
<jsp:useBean id="obj" class="User"/> <jsp:setProperty name="obj" property="*" />RetrieveRequestParameters.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <jsp:useBean id="user" class="user.UserData" scope="session"/> <jsp:setProperty name="user" property="*"/> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> You entered You entered<BR> Name: <jsp:getProperty name="user" property="name" /><BR> Address: <jsp:getProperty name="user" property="useraddress" /><BR> Id: <jsp:getProperty name="user" property="userid" /><BR> </body> </html>output
Passing Parameters using <jsp: param>
We can pass parameter names and values to forwarded file by using a <jsp: param> tag.
It is mostly used as a child tag inside an action tag.
It is used in forward and include mostly.
There can be multiple <jsp: param> tag inside the <jsp: forward> tag if we need to pass more than one parameter to the target file. The name attribute specifies the parameter name and takes a string literal as a value.
This tag contains only one parameter page = "relativeUrl".
This relativeUrl represents the file to which the request will be forwarded.
The target can be any html,jsp or servlet.
jspParamExample.jsp
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <jsp:forward page="AnotherResource.jsp"> <jsp:param name="param1" value="Prince"/> <jsp:param name="param2" value="Pune"/> </jsp:forward> </body> </html>AnotherResource.jsp
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Another Jsp</title> </head> <body> Forwarded Parameters:<br> <b>Name:</b> <%= request.getParameter("param1") %> <b>City:</b> <%= request.getParameter("param2") %> </body> </html>Output