JSP Interview Questions and Answers

    1 Votes

49. what is the use of jsp:forward action tag?

The forward action tag is used to transfer control to a static or dynamic resource. The forward action terminates the action of the current page and forwards the request to another resource such as a static page, another JSP page, or a java servlet. The static or dynamic resource to which control has to be transferred is represented as a URL. The user can have the target file as a HTML file, another JSP file, or a servlet. For example,
<jsp:forward page=”success.jsp” />

Forwards the request to the success.jsp.

50. When jsp:forward is used in application?
It is used for forwarding request from one JSP to another resource. It provides the same functionality of the forward() method in RequestDispatcher Interface.
51. Explain jsp:param action tag with example?

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>”/>
</jsp:forward>

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” />

Where,
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” />

Where

  • 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” />

Where,

  • Name: Bean name same as the id specified in the userbean action.
  • Property: The bean property name whose value is to be retrieved.

Example:

<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:

<CustomDate></CustomDate>

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.

<tt:CustomDate/>

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.

<tt:CustomDate attribute=”value”/>

Tags with bodies: A custom tag can contain custom, core tags, scripting elements and HTML text content between the start and end tag.

<tt:mytag>
<h1>This is body inside the tag</h1>
</tt:mytag>
Page 4 of 5

Popular Videos

communication

How to improve your Interview, Salary Negotiation, Communication & Presentation Skills.

Got a tip or Question?
Let us know

Related Articles

Servlet Interview Questions and Answers
Advanced Servlet Interview Questions and Answers
EJB Interview Questions and Answers
Spring Interview Questions and Answers