Interface Java Interview Questions – Set 12

What is JSP Implicit Objects

Certain objects that are available for the use in JSP documents without being declared first. These objects are parsed by the JSP engine and inserted into the generated servlet. The implicit objects re listed below:

  • request :It represents the request made by the client. The request implicit object is generally used to get request parameters, request attributes, header information and query string values.
  • response :The JSP implicit response object is an instance of a java class that implements the javax.servlet.http.HttpServletResponse interface. It represents the response to be given to the client. The response implicit object is generally used to set the response content type, add cookie and redirect the response.
  • pageContext : This is used to access page attributes and also to access all the namespaces associated with a JSP page. The lass or the interface name of the object PageContext is jsp.pageContext. The object PageContext is written: Javax.servlet.jsp.pagecontext The PageContext object has a page scope. It is an instance of the javax.servlet.jsp.PageContext class.
  • session :The session object has a scope of an entire HttpSession. It is an instance of the javax.servlet.http.HttpSession class. It represents the session created for the requesting client, and stores objects between client’s requests. The session object views and manipulates session information, such as the session identifier, creation time, and last accessed time. It also binds objects to a session, so that the user information may persist across multiple user connections.
  • application :The application object has an application scope. It is an instance of the javax.servlet.ServletContext class. It represents the context within which the JSP is executing. It defines a set of methods that a servlet uses to communicate with its servlet container.
  • out :The JSP implicit out object is an instance of the javax.servlet.jsp.JspWriter class. It represents the output content to be sent to the client. The out implicit object is used to write the output content.
  • config :The JSP implicit config object is an instance of the java class that implements javax.servlet.ServletConfig interface. It gives facility for a JSP page to obtain the initialization parameters available.
  • page :The Page object denotes the JSP page, used for calling any instance of a Page’s servlet. The class or the interface name of the Page object is jsp.HttpJspPage. The Page object is written: Java.lang.Object.
  • exception :The JSP implicit exception object is an instance of the java.lang.Throwable class. It is available in JSP error pages only. It represents the occured exception that caused the control to pass to the JSP error page.

What is map interface in a java

Map is an object that maps keys to values. A map cannot contain duplicate keys: Each key can map to at most one value. This Map permits null value

What is the difference between Enumeration and Iterator

The functionality of Enumeration interface is duplicated by the Iterator interface. Iterator has a remove() method while Enumeration doesn’t. Enumeration acts as Read-only interface, because it has the methods only to traverse and fetch the objects, where as using Iterator we can manipulate the objects also like adding and removing the objects. So Enumeration is used when ever we want to make Collection objects as Read-only.

What does the following code do? Can the LinkedHashSet be replaced with a HashSet

import java.util.ArrayList;

import java.util.LinkedHashSet;

import java.util.List;

 

public class CollectionFunction {

    public <e> List<e> function (List <e> list) {

          return new ArrayList<e>(new LinkedHashSet<e>(list));

    }

}

The above code removes duplicates from a supplied list by passing it through an implementation of a Set interface. In this case, a LinkedHashSet is used to honor the ordering by implementing a SortedSet interface. If ordering is not required, the LinkedHashSet can be replaced with a HashSet.

Define Serialization? What do you mean by Serialization in Java

Serialization is a mechanism by which you can save or transfer the state of an object by converting it to a byte stream. This can be done in java by implementing Serialiazable interface. Serializable is defined as a marker interface which needs to be implemented for transferring an object over a network or persistence of its state to a file. Since its a marker interface, it does not contain any methods. Implementation of this interface enables the conversion of object into byte stream and thus can be transferred. The object conversion is done by the JVM using its default serialization mechanism.

Is Servlets thread-safe

Servlets are not thread safe. If you want to make it Servlet as Thread safe, you can implement SingleThreadInterface.

There are two different ways of making a servlet thread safe namely

  • By implementing SingleThreadModel:
    • By implementing a SingleThreadModel it will be possible to create a Thread safe servlet.There can only be one user at a given point of time.
  • Synchornize the part of sensitive code:

We can allow a single user at a given point of time by making that part of the code which is sensitive as synchronized

Performance of List interface implementations

LinkedList

Performance of get and remove methods is linear time [ Big O Notation is O(n) ] – Performance of add and Iterator.remove methods is constant-time [ Big O Notation is O(1) ]

ArrayList

The size, isEmpty, get, set, iterator, and listIterator operations run in constant time. [ Big O Notation is O(1) ] – The add operation runs in amortized constant time [ Big O Notation is O(1) ] , but in worst case (since the array must be resized and copied) adding n elements requires linear time [ Big O Notation is O(n) ] – Performance of remove method is linear time [ Big O Notation is O(n) ] – All of the other operations run in linear time [ Big O Notation is O(n) ]. The constant factor is low compared to that for the LinkedList implementation.

Which implementation of the List interface provides for the fastest insertion of a new element into the middle of the list

Vector, ArrayList, LinkedList

ArrayList and Vector both use an array to store the elements of the list. When an element is inserted into the middle of the list the elements that follow the insertion point must be shifted to make room for the new element.

The LinkedList is implemented using a doubly linked list; an insertion requires only the updating of the links at the point of insertion. Therefore, the LinkedList allows for fast insertions and deletions.

What are the rules for object reference assignment and method call conversion

An interface type can only be converted to an interface type or to object. If the

new type is an interface, it must be a superinterface of the old type.

A class type can be converted to a class type or to an interface type. If converting to a

class type the new type should be superclass of the old type. If converting to an

interface type new type the old class must implement the interface.

An array maybe converted to class object, to the interface cloneable, or to an array.

Only an array of object references types may be converted to an array, and the old

element type must be convertible to the new element

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.