Java Blog

This is my blog on java, servlets, JSP and design patterns. This might help you to keep your java knowledge updated and also to prepare for your java interview, sun java certification. Its all there, it is left to you how to use it! I started this blog with a selfish reason, just to keep my java knowledge updated and to keep log of the questions that arise in my mind. Now it has turned out to be a full blown java tutorial site with lots of java interview questions and articles. Good day!

Recent Java Articles:

Difference between JSP include directive and JSP include action

<%@ include file=”filename” %> is the JSP include directive. At JSP page translation time, the content of the file given in the include directive is ‘pasted’ as it is, in the place where the JSP include directive is used. Then the source JSP page is converted into a java servlet class. The included file can be [...]

May 9th, 2008

Difference between ServletConfig and ServletContext

Signature: public interface ServletConfig ServletConfig is implemented by the servlet container to initialize a single servlet using init(). That is, you can pass initialization parameters to the servlet using the web.xml deployment descriptor. For understanding, this is similar to a constructor in a java class. Example code: <servlet> <servlet-name>ServletConfigTest</servlet-name> <servlet-class>com.javapapers.ServletConfigTest</servlet-class> <init-param> <param-name>topic</param-name> <param-value>Difference between ServletConfig and [...]

May 6th, 2008

Difference between ServletRequest.getRequestDispatcher and ServletContext.getRequestDispatcher

request.getRequestDispatcher(“url”) means the dispatch is relative to the current HTTP request. Example code: RequestDispatcher reqDispObj = request.getRequestDispatcher("/home.jsp"); getServletContext().getRequestDispatcher(“url”) means the dispatch is relative to the root of the ServletContext. Example code:RequestDispatcher reqDispObj = getServletContext().getRequestDispatcher("/ContextRoot/home.jsp");

May 6th, 2008

ServletRequest and ServletResponse – Explain

ServletRequest and ServletResponse are two interfaces that serve as the backbone of servlet technology implementation. They belong to the javax.servlet package. Signature: public interface ServletRequest Blueprint of an object to provide client request information to a servlet. The servlet container creates a ServletRequest object and sends it as an argument to the servlet’s service method. Signature: public interface [...]

May 6th, 2008

Why not declare a constructor in servlet?

Technically you can define constructors in servlet. But, the declared constructor cannot access the ServletConfig object or throw a ServletException. Then why is it not customary to declare a constructor in a servlet? Because the init() method is used to perform servlet initialization. In JDK 1.0 (servlet were written in this version), constructors for dynamically loaded Java classes [...]

May 6th, 2008

Servlet Life Cycle – Explain

Servlet Life Cycle: The interface javax.servlet.Servlet defines the following three methods known as servlet life cycle methods.  public void init(ServletConfig config) throws ServletException  public void service(ServletRequest req, ServletResponse res) throws ServletException, java.io.IOException  public void destroy() Creation and intialization The container first creates the servlet instance and then executes the init() method.  init() can be called only once in its life cycle by [...]

May 5th, 2008

Difference between _jspService() and other life cycle methods.

JSP contains three life cycle methods namely jspInit( ), _jspService() and jspDestroy(). In these, jspInit() and jspDestroy() can be overridden and we cannot override _jspService(). The contents what we write in the JSP will go into _jspService() because we are implicitly implementing it. If we again try to override it explicitly, JSP compliler will giver error [...]

May 5th, 2008

JSP Life Cycle – explain.

JSP’s life cycle can be grouped into following phases. 1. JSP Page Translation: A java servlet file is generated from the JSP source file. This is the first step in its tedious multiple phase life cycle. In the translation phase, the container validates the syntactic correctness of the JSP pages and tag files. The container interprets the [...]

May 5th, 2008

When does the finally clause in java exception block never executes?

The finally clause in the try-catch exeception block always executes, irrespective of the occurence of exeception. This is applicable for the normal java program flow. If the execution flow is stopped irreversibly before the finally clause, then the finally block will not be executed. How can the user achieve that in Java? Include “System.exit(1);” before the [...]

May 4th, 2008

Java pass by value and pass by reference.

Pass by value means passing a copy of the value to be passed. Pass by reference means the passing the address itself. In Java the arguments are always passed by value. Java only supports pass by value. With Java objects, the object reference itself is passed by value and so both the original reference and [...]

April 29th, 2008