Sunday, November 23, 2014

Finding superclasses of an object in Java

In this article, we look into one of the mechanism of an Reflection to find about the super classes of any object in Java with example.  Reflection enables what is commonly referred to as dynamic programming in Java. Reflection in Java is accomplished using the Java Reflection API



Reflection is a mechanism for discovering data about a program at run-time. The most basic thing you usually do when doing reflective programming is to get a Class object. Once you have a Class object instance, you can obtain all sorts of information about the class and even manipulate the class.

Finding Superclasses
The ancestors of a given class are referred to as that class’s superclasses. Using reflection, you can determine all of the ancestors of a given class. After you've obtained a Class object, you can use the getSuperclass() method to get the class’s superclass if one exists. If a superclass exists, a Class object will be returned. If there is not a superclass, this method will return null. Remember that Java supports only single inheritance, so for any given class, there can be only one superclass.  Actually to be clear, there can be only one direct superclass.Technically, all ancestor classes are considered to be superclasses of a given class.To retrieve all ancestor superclasses, you would recursively call the getSuperclass() method on each Class object that is returned.

Here is an example, that explain the above concept 

Example 2 In this example, we try to find the super classes of BigDecimal class. 

If you know anyone who has started learning java, why not help them out! Just share this post with them. Thanks for studying today!...

Saturday, November 1, 2014

Implicit Objects In JSP

JavaServer Pages (JSP) technology allows you to easily create web content that has both static and dynamic components. JSP technology makes available all the dynamic capabilities of Java Servlet technology but provides a more natural approach to creating static content. In this article, we look into the 9 implicit object of JSP with explanation and examples.

In a web application, multiple web components collaborate with each other and share information by means of objects that are maintained as attributes of four scope objects. You access these attributes by using the getAttribute and setAttribute methods of the class representing the scope

Note In addition to the standard servlet request, session, and application scopes, JSP adds a fourth scope called page scope.


A JSP page can access some specific objects through scripting variables. These objects are provided by the JSP container and are called implicit objects. These implicit objects can be accessed in scriptlets, in expressions, or as part of the EL expressions.



  • application
    The implicit application object provides a reference to the javax.servlet.ServletContext interface. The ServletContext interface is used to provide access to any context-initialization parameters that have been configured for the JSP page via the deployment descriptor of the web application. The ServletContext object and parameters stored in them by the web container are available to the entire web application. The application object provides the developer of the JSP page with access to the ServletContext object.
    In short, application provides access to web context. Its use is similar to that of ServletContext.
  • config
    Similar to the application object, the config object provides a reference to the ServletConfig interface of the web application. The ServletConfig interface is used to provide access to any initialization parameters that have been configured for the JSP page via the deployment descriptor of the web application. The config object provides the JSP developer with access to the ServletConfig object.
    In short, config provides initialization information used by the JSP container. Its use is similar to that of the class ServletConfig.
  • exception
    The implicit exception object is available to JSP to handle the error conditions and report the runtime exceptions using the errorPage page directive. Exception represents an instance of the Throwable object and contains error information. This variable is available only from the JSP error page
  • out
    The implicit out object represents an instance of the JspWriter class that is used to write character data to the response stream. out represents the output write stream JspWriter. This variable points at the same object as HttpServletResponse.getWriter() in servlets. For example, a simplest JSP that returns a result from a Java class called CurrencyConverter might look like this
    <html>
       <body>
         <% out.println(CurrencyConverter.getDollarRate()); %>
       </body>
    </html>
  • page
    The JSP implicit page object is an instance of the Object class and represents the current JSP page. In short, page represents the instance of the JSP’s servlet.
  • pageContext
    A pageContext provides context information by providing access to all the namespaces associated with a JSP page and to several page attributes. Also, it contains the reference to implicit objects. pageContext represents the JSP context and is used with Tag Libraries
  • request
    The request object is an instance of the javax.servlet.http.HttpServletRequest interface. It represents the client request. The request implicit object is generally used to get request parameters, request attributes, header information, and query string values.
  • response
    The implicit response object is an instance of the javax.servlet.http.HttpServletResponse interface and represents the response to be given to the client. The implicit response object is generally used to set the response content type, add cookies, and redirect the response.
  • session
    The JSP implicit session object is an instance of a Java class that implements the javax.servlet.http.HttpSession interface. It is used to store session state for a client.



Examples of Common Implicit Objects

getServletContext().setAttribute("blog", JavaLatte);
This sets the book attribute in a ServletContext without using implicit objects.

request.setAttribute("blog", JavaLatte);
This sets the book attribute in a request object. The request object is also an implicit object in JSP. Hence, setting the attribute in a servlet is similar to setting the attribute in a JSP page.

request.getSession().setAttribute("book", book);
This sets the book attribute in the session without using implicit objects

application.setAttribute("book" book);
This sets the book attribute in the ServletContext using the application implicit object.

request.setAttribute("book" book);
This sets the book attribute in a request object. request is also an implicit object in JSP. Hence, setting the attribute in JSP is similar to setting the attribute in a servlet.

session.setAttribute("book" book);
This sets the book attribute in the session using a session implicit object

pageContext.setAttribute("book" book);
This sets the book attribute in PageContext using a pageContext implicit object. There is no equivalent of pageContext in a servlet. A PageContext instance provides access to all the namespaces associated with a JSP page, provides access to several page attributes, and provides a layer above the implementation details. Implicit objects are added to the pageContext automatically.



If you know anyone who has started learning java, why not help them out! Just share this post with them. Thanks for studying today!...