Pr.Pg Next Pg

JSP Interview Questions and Answers Part 9

 

81. What is the <jsp:useBean> standard action?

  • The <jsp:useBean> standard action is used to locate an existing JavaBean or to create a JavaBean if it does not exist.

  • It has attributes to identify the object instance, to specify the lifetime of the bean, and to specify the fully qualified classpath and type.

 

82.What are the scopes available in <jsp:useBean>?

  • The scopes available in <jsp:useBean> are as follows:

  • page scope:: It specifies that the object will be available for the entire JSP page but not outside the page.

  • request scope: It specifies that the object will be associated with a particular request and exist as long as the request exists.

  • application scope: It specifies that the object will be available throughout the entire Web application but not outside the application.

  • session scope: It specifies that the object will be available throughout the session with a particular client.

 

83.What is the <jsp:forward> standard action?

  • The <jsp:forward> standard action forwards a response from a servlet or a JSP page to another page.

  • The execution of the current page is stopped and control is transferred to the forwarded page.

  • The syntax of the <jsp:forward> standard action is : 
    <jsp:forward page="/targetPage" />
    Here, targetPage can be a JSP page, an HTML page, or a servlet within the same context.

  • If anything is written to the output stream that is not buffered before <jsp:forward>, an IllegalStateException will be thrown.

  • Note : Whenever we intend to use <jsp:forward> or <jsp:include> in a page, buffering should be enabled. By default buffer is enabled.

 

84. What is the <jsp:include> standard action?

  • The <jsp:include> standard action enables the current JSP page to include a static or a dynamic resource at runtime.

  • In contrast to the include directive, the include action is used for resources that change frequently.

  • The resource to be included must be in the same context.

  • The syntax of the <jsp:include> standard action is as follows:
    <jsp:include page="targetPage" flush="true"/>
    Here, targetPage is the page to be included in the current JSP.

 

85. Differentiate between pageContext.include and jsp:include?

  • The <jsp:include> standard action and the pageContext.include() method are both used to include resources at runtime.

  • However, the pageContext.include() method always flushes the output of the current page before including the other components, whereas <jsp:include> flushes the output of the current page only if the value of flush is explicitly set to true as follows:

  • <jsp:include page="/index.jsp" flush="true"/>

 

86. What is the jsp:setProperty action?

  • You use jsp:setProperty to give values to properties of beans that have been referenced earlier.

  • You can do this in two contexts.

  • First, you can use jsp:setProperty after, but outside of, a jsp:useBean element, as below:

<jsp:useBean id="myName" ... />

...

<jsp:setProperty name="myName" property="myProperty" ... />

  • In this case, the jsp:setProperty is executed regardless of whether a new bean was instantiated or an existing bean was found.

  • A second context in which jsp:setProperty can appear is inside the body of a jsp:useBean element, as below:

<jsp:useBean id="myName" ... >

...

<jsp:setProperty name="myName"

property="someProperty" ... />

</jsp:useBean>

  • Here, the jsp:setProperty is executed only if a new object was instantiated, not if an existing one was found.

 

87. What is the jsp:getProperty action?

  • The <jsp:getProperty> action is used to access the properties of a bean that was set using the <jsp:getProperty> action. The container converts the property to a String as follows:

  • If it is an object, it uses the toString() method to convert it to a String.

  • If it is a primitive, it converts it directly to a String using the valueOf() method of the corresponding Wrapper class.

  • The syntax of the <jsp:getProperty> method is: <jsp:getProperty name="Name" property="Property" />

  • Here, name is the id of the bean from which the property was set. The property attribute is the property to get. A user must create or locate a bean using the <jsp:useBean> action before using the <jsp:getProperty> action.

 

88.What is the <jsp:param> standard action?

  • The <jsp:param> standard action is used with <jsp:include> or <jsp:forward> to pass parameter names and values to the target resource.

  • The syntax of the <jsp:param> standard action is as follows:
    <jsp:param name="paramName" value="paramValue"/>

 

89.What is the jsp:plugin action ?

  • This action lets you insert the browser-specific OBJECT or EMBED element needed to specify that thebrowser run an applet using the Java plugin.

 

90.What are scripting elements?

JSP scripting elements let you insert Java code into the servlet that will be generated from the current JSP page. There are three forms:

1.     Expressions of the form <%= expression %> that are evaluated and inserted into the output,

2.     Scriptlets of the form <% code %> that are inserted into the servlet's service method,

3.     Declarations of the form <%! code %> that are inserted into the body of the servlet class, outside of any existing methods.


Pr.Pg border                                              Next Pg