JSP TUTORIAL
Standard Actions
Following are the standard actions in JSP
1. <jsp:forward>
2. <jsp:include>
3. <jsp:useBean>
4. <jsp:setProperty>
5. <jsp:getProperty>
6. <jsp:plugin>
1. <jsp:forward>
This action is used to forward the request object containing the client request information to another web resource
JSP Syntax
<jsp:forward page="relativeURL | <%= expression %>" />
JSP Exmaple
<jsp:forward page="/servlet/login" /> --- (1)
<jsp:forward page="/servlet/login"> --- (2)
<jsp:param name="username" value="ramesh" /> --- (3)
</jsp:forward> --- (4)
(1) - Forward action to forward request to login servelt
Passing values to login sevlet
(2) - Begining of forward action to forward request login servlet
(3) - passing parameters to login servlet
(4) - ending of forward action
Be careful when using <jsp:forward> with unbuffered output. If you have used the page directive with buffer="none" to specify that the output of your JSP page should not be buffered, and if the JSP page has any data in the out object, using <jsp:forward> will cause an IllegalStateException.
Attributes
page="{relativeURL | <%= expression %>}"
A String or an expression representing the relative URL of the component to which you are forwarding the request. The component can be another JSP page, a servlet, or any other object that can respond to a request.
<jsp:param name="parameterName"
value="{parameterValue | <%= expression %>}" />
Sends one or more name/value pairs as parameters to a dynamic resource. The target resource should be dynamic, that is, a JSP page, servlet, or other resource that can process the data that is sent to it as parameters.
You can use more than one <jsp:param> clause if you need to send more than one parameter to the target resource. The name attribute specifies the parameter name and takes a case-sensitive
literal string as a value. The value attribute specifies the parameter value and takes either a case-sensitive literal string or an expression that is evaluated at request time.
2. <jsp:include>
This action is used to include other file information into the request/output html
JSP Syntax
<jsp:include page="{relativeURL | <%= expression %>}"
flush="true| false" />
Examples
<jsp:include page="/ramesh.html" /> --- (1)
<jsp:include page="scripts/login.jsp"> --- (2)
<jsp:param name="username" value="ramesh" /> --- (3)
</jsp:include> --- (4)
(1) static html file will be included into the jsp page
(2) dynamic jsp file , result will be included into jsp page
(3) passing value to the dynamic jsp page
(4) end of jsp action
3. <jsp:useBean>
This action is used to create an object for a bean, we can define the scope of the bean
using this action
JSP Syntax
<jsp:useBean id="beanInstanceName"
scope="page|request|session|application"
{
class="package.class" [ type="package.class" ]|
beanName="{package.class | <%= expression %>}"
type="package.class" |
type="package.class"
}
{ /> | > other elements </jsp:useBean> }
Example 1
<jsp:useBean id="cart" scope="session" class="session.Carts" />
<jsp:setProperty name="cart" property="*" />
Example 2
<jsp:useBean id="checking" scope="session" class="bank.Checking" > -- (1)
<jsp:setProperty name="checking" property="balance" value="0.0" /> -- (2)
</jsp:useBean> -- (3)
(1) - Create the bean instance ( "checking" ) for the class bank.Checking
(2) - Use set property action to set the balance variable (in the object checking)
value to 0.0
Explanation
scope="page|request|session|application"
The scope in which the bean exists and the variable named in id is available.
The default value is page. The meanings of the different scopes are shown below:
page - You can use the bean within the JSP page with the <jsp:useBean> element or
any of the page's static include files, until the page sends a response back
to the client or forwards a request to another resource.
request - You can use the bean from any JSP page processing the same request, until
a JSP page sends a response to the client or forwards the request to another
resource. You can use the request object to access the bean, for example, request.getAttribute(beanInstanceName).
session - You can use the bean from any JSP page in the same session as the JSP page
that created the bean. The bean exists across the entire session, and any
page that participates in the session can use it. The page in which you
create the bean must have a page directive with session="true".
application - You can use the bean from any JSP page in the same application as the
JSP page that created the bean. The bean exists across an entire JSP
application, and any page in the application can use the bean.
4. <jsp:setProperty>
Using this action we can Set a property value or values in a bean.
JSP Syntax
<jsp:setProperty name="beanInstanceName"
{ property="*" |
property="propertyName" [ param="parameterName" ] |
property="propertyName" value="{stringLiteral| <%= expression %>}"
}
/>
Examples
<jsp:setProperty name="jBean" property="*" /> --- (1)
<jsp:setProperty name="jBean" property="username" /> --- (2)
<jsp:setProperty name="jBean" property="username" value="Steve" /> --- (3)
(1) It will pass all values in the request to the corresponding set methods.
To expalin more... In the request if you have a parameter "username" and the value entered by
user... This action will pass the the value enter by user to setUsername method
in the jBean, in that method we need to code assignment statement to set the passed value to variable.
(2) Only username parameter value from the request object will be passed to bean (bean setter method)
(3) value Steve will be passed to setUsername method of jBean (in this case it wont pass the value
from the request object)
You must declare the bean with <jsp:useBean> before you set a property value with
<jsp:setProperty>.
5. <jsp:getProperty>
This action is used to get property value from a java bean
JSP Syntax
<jsp:getProperty name="beanInstanceName" property="propertyName" />
Example
<jsp:useBean id="calendar" scope="page" class="employee.Calendar" /> -- (1)
<h2>
Calendar of <jsp:getProperty name="calendar" property="username" /> -- (2)
</h2>
(1) - Creating java bean object with id calendar
(2) - get the property value (it will call the method in the clas employee.Calendar)
from the object calendar
6. <jsp:plugin>
The <jsp:plugin> element plays or dispays an object (typically an applet or bean) in the client web browser, using a Java plug-in that is built in to the browser or downloaded from a specified URL.
Example
<jsp:plugin type=applet code="Molecule.class" codebase="/html">
<jsp:params>
<jsp:param name="molecule" value="molecules/benzene.mol" />
</jsp:params>
<jsp:fallback>
<p>Unable to load applet</p>
</jsp:fallback>
</jsp:plugin>
|
|
|
|
|