JSP Java Interview Questions – Set 09

What types of comments are available in the JSP?

There are two types of comments that are allowed in the JSP. They are hidden and output comments.

A hidden comment does not appear in the generated HTML output, while output comments appear in the generated output.

Example of hidden comment:

< % – – This is a hidden comment – – % >

Example of output comment:

< ! – – This is an output comment – – >

In the Servlet 2.4 specification SingleThreadModel has been deprecated, why

Because it is not practical to have such model. Whether you set isThreadSafe to true or false, you should take care of concurrent client requests to the JSP page by synchronizing access to any shared objects defined at the page level.

Difference between the request attribute and request parameter

  • Request parameters are the result of submitting an HTTP request with a query string that specifies the name/value pairs, or of submitting an HTML form that specifies the name/value pairs. The name and the values are always strings. For example, when you do a post from html, data can be automatically retrieved by using getParameter(). Parameters are Strings, and generally can be retrieved, but not set.
  • Let’s take a real example, you have one html page named htmland JSP page named Register.jsp. The RegisterForm.html contains a form with few parameters for user registration on web application and those parameters you want to store in database.

<html>

<body>

<form name=”regform” method=”post” action=”../Register.jsp”>

<table ID=”Table1″>

<tr>

<td>

First Name : <input type=”text” name=”FIRSTNAME” size=”25″ value=””>

</td>

</tr>

<tr>

<td>

<input type=”Submit” NAME=”Submit” value=”Submit” >

</td>

</tr>

</table>

</form>

</body>

</html>

When user will enter first name in the text filed and press submit button, it will callRegister.jsp page. Now you want the value entered in text field by user in jsp/servlet so there you use request.getParameter() method.

<%

String lFirstName = request.getParameter(“FIRSTNAME”);

……………….

%>

On the server side, the request.getParameter() will retrieve a value that the client has submitted in the First Name text field. Using this method you can retrive only one value. This method i.e. getParameter method is in ServletRequest interface which is part of javax.servlet package.

Request attributes (more correctly called “request-scoped variables”) are objects of any type that are explicitly placed on the request object via a call to the setAttribute() method. They are retrieved in Java code via the getAttribute() method and in JSP pages with Expression Language references. Always use request.getAttribute() to get an object added to the request scope on the serverside i.e. using request.setAttribute().

Attributes are objects, and can be placed in the request, session, or context objects. Because they can be any object, not just a String, they are much more flexible. You can also set attributes programaticly and retrieve them later. This is very useful in the MVC pattern. For example, you want to take values from database in one jsp/servlet and display them in another jsp. Now you have resultset filled with data ready in servlet then you use setAttributemethod and send this resultset to another jsp where it can be extracted by using getAttributemethod.

Once a servlet gets a request, it can add additional attributes, then forward the request off to another servlet for processing. Attributes allow servlets to communicate with one another.

What happens when a page is statically included in another JSP page

An include directive tells the JSP engine to include the contents of another file (HTML, JSP, etc.) in the current page. This process of including a file is also called as static include.

Life cycle methods in JSP

Life-cycle methods of the JSP are:

  • jspInit(): The container calls the jspInit() to initialize the servlet instance. It is called before any other method, and is called only once for a servlet instance.
  • _jspService():The container calls the _jspservice() for each request and it passes the request and the response objects. _jspService() method cann’t be overridden.

The container calls this when its instance is about to destroyed.The jspInit() and jspDestroy() methods can be overridden within a JSP page.

Can you make use of a ServletOutputStream object from within a JSP page

No. You are supposed to make use of only a JSPWriter object (given to you in the form of the implicit object out) for replying to clients.

A JSPWriter can be viewed as a buffered version of the stream object returned by response.getWriter(), although from an implementational perspective, it is not.

How can I declare methods within my JSP page

You can declare methods for use within your JSP page as declarations. The methods can then be invoked within any other methods you declare, or within JSP scriptlets and expressions.

Do note that you do not have direct access to any of the JSP implicit objects like request, response, session and so forth from within JSP methods. However, you should be able to pass any of the implicit JSP variables as parameters to the methods you declare.

For example:

file1.jsp:

file2.jsp

<%test(out);% >

What is JSP tag handler

A Java programming language object that implements the behavior of a custom tag.
If you have any questions that you want answer for – please leave a comment on this page and I will answer them

What is JSP expression

A scripting element that contains a valid scripting language expression that is evaluated, converted to a String, and placed into the implicit out object.

What is JSP custom action

A user-defined action described in a portable manner by a tag library descriptor and imported into a JSP page by a taglib directive. Custom actions are used to encapsulate recurring tasks in writing JSP pages.