49. what is the use of jsp:forward action tag?
<jsp:forward page=”success.jsp” />
Forwards the request to the success.jsp.
The jsp:param action tag is used to add the specific parameter to current request. It can be used inside a jsp:include or jsp:forward. Consider a case in which on successful login the user needs to be forwarded to the success page with the user name set as an parameter to the request object so that name can be accessed in the home page and displayed.
Jsp:param can be used along with jsp:forward as shown below:
<jsp:forward page=”success.jsp” />
<jsp:param name=”userName” value=”<name>”/>
52. What is jsp:useBean action tag?
The <jsp:useBean> tag attempts to locate a bean or if the bean does not exist, instantiates it from the class specified. Syntax:
<jsp:useBean id=”name” class=”package.class” scope=”request/session/page/application” />
id – The name used for referring bean object
Class – The bean class
Scope – The scope in which the bean object is available.
53. How jsp:useBean works?
For example: <jsp:useBean id=”user” class=”com.a4academics.beans.userBean” scope=”request” />
Sequence of steps:
- Attempts to locate a bean with the name “userBean” in the request scope.
- If it finds the bean, stores a reference in the variable user.
- If it does not find the bean, instantiates a bean using the class userBean, and stores the reference to the variable user.
54. Name JSP bean object scopes and explain them briefly?
Bean object scopes are:
- page: It is available only within the JSP page and is destroyed when the page has finished generating its output for the request.
- request: It is valid for the current request and is destroyed when the response is sent.
- session: It is valid for a user session and is destroyed when the session is destroyed.
- application: valid throughout the application and is destroyed when the web application is destroyed/uninstalled.
55. Explain jsp:setProperty action?
The setProperty action sets the properties of a bean. The bean must have been previously defined before this action.
Syntax: <jsp:useBean id=”myName” class=”package.class” />
<jsp:setProperty name=”myName” property=”someProperty” value=”someValue” />
- name: The name of the bean object and should be the same as the id value specified in useBean.
- property: the bean property (field name) for which the value is to be set. There should be a instance variable with the property name specified and accessors/mutator methods.
- Value: The value to be set for the property.
56. What are the two options by which setProperty automatically set the values from an HTML form to a java bean?
The following options can be used to automatically set the values from an HTML form to a java bean using the setProperty action.
Option 1: <jsp:setProperty name=”UserBean” property=”someProperty” param=”userName” />
- Sets the value of the HTML form element with the name userName into the bean UserBean’s property someProperty.
Option 2: <jsp:setProperty name=”myName” property=”*” />
- Sets all the values of the form elements into the bean properties. In this case the name of the property and the name of the form element should be the same.
57. Explain jsp:getProperty action tag with example?
The getProperty action is used to retrieve the value of a given property and converts it to a string, and finally inserts it into the output. Syntax:
<jsp:useBean id=”myName” type=”package.class” />
<jsp:getProperty name=”myName” property=”someProperty” />
- Name: Bean name same as the id specified in the userbean action.
- Property: The bean property name whose value is to be retrieved.
<jsp:useBean id=”user” type=”com.a4academics.beans.UserBean” />
<jsp:getProperty name=”user” property=”userName” />
This reads the value of the property named userName from the userBean and prints it.
58. What is a custom tag?
Custom tags are nothing but tags which are developed by programmers for performing specific functionalities. For example:
Custom tag developed by programmers to print the current date in a specified format. The above tag when used in JSP will print the current system date.
59. Why JSP custom tags are required?
Any common code which needs to be reused across the web application can be developed using custom tags. It is easy to maintain as the logic is centralized. Any change to the logic just requires a one place change thus reducing the effort to change it.
60. What are the types of JSP custom tags and explain them briefly?
Simple tags: A simple tag contains no body and no attributes.
Tags with attributes: A custom tag can have attributes. Attributes are listed in the start tag and have the syntax: attributeName=”value”. Attribute are like configuration details for the custom tag.
Tags with bodies: A custom tag can contain custom, core tags, scripting elements and HTML text content between the start and end tag.
<h1>This is body inside the tag</h1>