JSP interview questions and answers

  1. What is JSP?
    Answer: JSP stands for JavaServer Pages. JSP is java server side technology to create dynamic web pages. JSP is extension of Servlet technology to help developers create dynamic pages with HTML like syntax.
  2. Explain lifecycle of a JSP.
    Answer: Whenever a JSP page is requested for the first time or the JSP page has been modified, then JSP page is translated into a servlet class and the servlet class is compiled. This servlet class is equivalent to any servlet class but few changes in the names of the methods. A JSP Lifecycle consists of following steps:
    • Translation
    • Compilation
    • Loading the class
    • Instantiating the class
    • jspInit() invocation
    • _jspService() invocation
    • jspDestroy() invocation
    "jspInit()" method is similar to init() method of servlets. This method is invoked by the container only once when a JSP page is initialized. It can be overridden by a page author to initialize resources such as database and network connections, and to allow a JSP page to read persistent configuration data. "_jspService()" is the JSP method that gets invoked by JSP container for each client request by passing request and response object. Notice that method name starts with underscore to distinguish it from other lifecycle methods because we can’t override this method. All the JSP code goes inside this method and it’s overridden by default. A page author cannot override this method, as its implementation is provided by the container. "jspDestroy()" method is called by container when JSP is unloaded from memory such as shutting down application or container. This method is called only once in JSP lifecycle and we should override this method to release any resources created in JSP init method.
  3. Explain about implicit or well known variables in JSP
    Answer: According to the jsp specification we can use the following variables as part of the scriptlets without declaring the variables in the jsp page. These variables are called as "well known variables" or "implicit variables". There are 9 implicit variables:
    • pageContextis a convenient class which provides the methods like forward, include, getSession, getServletConfig, getServletContext, getRequest, getResponse, setAttribute, getAttribute, removeAttribute etc.
    • requestobject is an instance of a java class that implements the javax.servlet.http.HttpServletRequest interface. The request object can be used to get header information, parameters, attributes etc.
    • responseobject is an instance of a java class that implements the javax.servlet.http.HttpServletResponse interface. Using the "response" object we can set the response content type, add cookie and redirect the response.
    • sessionobject is an instance of a java class that implements the javax.servlet.http.HttpSession interface. Using the "session" object we can store session state for a single user.
    • applicationobject is an instance of a java class that implements the javax.servlet.ServletContext interface. It gives facility for a JSP page to obtain and set information about the web application in which it is running.
    • outobject is an instance of the javax.servlet.jsp.JspWriter class. Using the "out" object we can send some content to the client. In other words we can write the output content.
    • configobject is instance of javax.servlet.ServletConfig implementation and used to get the JSP init params configured in deployment descriptor.
    • exceptionobject is instance of java.lang.Throwable class and used to provide exception details in JSP error pages.
    • pageobject is instance of java.lang.Object class and represents the current JSP page. page object provide reference to the generated servlet class. This object is very rarely used.
  4. What is a scriptlet?
    Answer: A scriptlet contains Java code that is written between <% and %> tags, which gets executed whenever a JSP page is invoked. The scriptlet code goes into service() method when the JSP page is translated into a servlet. For example: <% out.println("Hello World!"); %>
  5. What is a JSP declaration?
    Answer: A declaration declares one or more variables or methods that you can use in Java code later in the JSP file. A declaration block is enclosed between the <%! and %> tags. For example: <%! Date theDate = new Date(); %>
  6. What is a JSP expression?
    Answer: An expression is written between the <%= and %> tags. An expression is evaluated, converted to a String, and inserted where the expression appears in the JSP file. It is not required to end the expression with a semicolon, as it implicitly adds a semicolon to all the expressions within the expression tags. For example: Date: <%= new java.util.Date()%>
  7. What are JSP directives?
    Answer: JSP directives are placed between <%@ and %>. JSP directives are used to give special instructions to the container for the translation of JSP to servlet code. These directives affects the overall structure of the servlet class. Following are the directives that JSP provides:
    • page directive: page directive provide attributes that gets applied to the entire JSP page. They define page dependent attributes. Some of the page attributes are as follows: <%@ page import="java.util.Date,java.io.*" %>
      <%@ page buffer="20kb" %>
      <%@ page errorPage="errorPage.jsp" %>
    • include directive: The JSP include directive is used to include the contents of another file to the current JSP page. The files are included during the translation phase. The files to be included can be JSP, HTML etc. An example of include directive is: <%@ include file="two.jsp" %>
    • taglib directive: The JSP taglib directive is used to define a tag library with prefix that we can use in JSP. Declares a tag library, containing custom actions, used in the page. <%@ taglib uri="/WEB-INF/c.tld" prefix="c"%>
  8. Explain about JSP standard action tags.
    Answer: JSP provides with some standard action tags that affect the overall runtime behavior of the JSP. These action tags can be used to include a file, to forward a request to a new page, work with java bean objects. Some of the action tags available are as follows:
    • jsp:include - It includes a response from a servlet or a JSP page into the current page.
    • jsp:forward - To forward the request to another resource.
    • jsp:useBean - To get the java bean object and make it available for the page.
    • jsp:getProperty - It gets the value of a property from a JavaBean and adds it to the response.
    • jsp:setProperty - It sets the properties for a JavaBean.
  9. What is difference between include directive and jsp:include action?
    Answer: The differences between include directive and jsp:include action are as follows:
    1. The include directive is processed at the translation time while the include action is processed at the run time.
    2. The include action always includes the contents of the specified file irrespective of whether the specified file is a static resource or a dynamic resource whereas include action executed a specified dynamic resource and only the result gets included in the response of the calling JSP.
    3. In JSP include action, we can pass params to be used in the included resource with jsp:param action element but in JSP include directive we can’t pass any params.
  10. What is the difference between ServletContext and PageContext?
    Answer: ServletContext gives the information about the container whereas PageContext gives the information about the Request.
  11. Explain about JSP buffer.
    Answer: The JSP buffer is explained with example at ... "Click here"
  12. How do you handle errors in JSP?
    Answer: To handle errors in JSP, we need to set the attribute isErrorPage="true" and also set the ErrorPage to a page which need to be displayed when error occurs. To know more about the error page with an example ... Click here
  13. What are the scopes available in jsp:useBean tag?
    Answer: jsp:useBean tag has the following scopes available:
    1. page: This is the default scope. It specifies that we can use this bean within the JSP page.
    2. request: This scope has wider scope than page. It specifies that we can use this bean from any JSP page that processes the same request.
    3. session: This scope has wider scope than request. It specifies that we can use this bean from any JSP page in the same session whether processes the same request or not.
    4. application: This scope has wider scope than session. It specifies that we can use this bean from any JSP page for the entire application.
  14. What is the difference in using request.getRequestDispatcher() and context.getRequestDispatcher()?
    Answer: request.getRequestDispatcher(path) takes relative path of the resource whereas context.getRequestDispatcher(path) takes the absolute path of the resource.

No comments:

Post a Comment