JSP Interview Question
  1. What is JSP?

    JSP stands for Java Server Pages, it is a server side technology which is used for creating dynamic web pages. It is the extension of servlets.

    It’s goal is to simplify management and creation of dynamic web pages. It is platform-independent, secure, and it makes use of Java as a server side scripting language.

  2. What are the advantages of JSP over Servlet?

    The advantage of JSP is that they are document-centric. Servlets, on the other hand, look and act like programs.

    A Java Server Page can contain Java program fragments that instantiate and execute Java classes, but these occur inside an HTML template file and are primarily used to generate dynamic content. Some of the JSP functionality can be achieved for the client, using JavaScript.

    The power of JSP is that it is server-based and provides a framework for Web application development.

  3. What are differences between JSP and Servlet?

    Refer our article on Difference between JSP and Servlets

  4. What is the life-cycle of JSP?

    When a request is mapped to a JSP page for the first time, it translates the JSP page into a servlet class and compiles the class. It is this servlet that services the client requests. JSP life cycle phases are:

    • Translation: JSP container checks the JSP page code and parse it to generate the servlet source code.
    • Compilation: JSP container compiles the jsp class source code and produce a class file in this phase.
    • Class Loading: Container loads the class into memory in this phase.
    • Instantiation: Container invokes the no-args constructor of generated class to load it into memory and instantiate it.
    • Initialization(jspInit()): Container invokes the init method of JSP class object and initializes the servlet config with init params configured in the deployment descriptor. After this phase, JSP is ready to handle client requests. Usually from translation to initialization of JSP happens when the first request for JSP comes but we can configure it to be loaded and initialized at the time of deployment like servlets using load-on-startup element.
    • Request Processing(_jspService()): This is the longest life cycle of JSP page and JSP page processes the client requests. The processing is multi-threaded and similar to servlets and for every request a new thread is spawned and ServletRequest and ServletResponse object is created and the JSP service method is invoked.
    • Destroy(jspDestroy()): This is the last phase of JSP life cycle where JSP class is unloaded from memory. Usually it happens when application is undeployed or the server is shut down.
  5. What is the jspInit() method?

    The jspInit() method of the javax.servlet.jsp.JspPage interface is similar to the 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.

  6. What is the _jspService() method?

    The _jspService() method of the javax.servlet.jsp.HttpJspPage interface is invoked every time a new request comes to a JSP page. This method takes the HttpServletRequest and HttpServletResponse objects as its arguments.

    A page author cannot override this method, as its implementation is provided by the container.

  7. What is the jspDestroy() method?

    The jspDestroy() method of the javax.servlet.jsp.JspPage interface is invoked by the container when a JSP page is about to be destroyed. This method is similar to the destroy() method of servlets.

    It can be overridden by a page author to perform any cleanup operation such as closing a database connection.

  8. Which JSP lifecycle methods can be overridden?

    We can override jspInit() and jspDestroy() methods using JSP declaration scripting element. We should override jspInit() methods to create common resources that we would like to use in JSP service method and override jspDestroy() method to release the common resources.

  9. What is Scriptlet, Expression and Declaration in JSP?

    Scriptlets, Expression and Declaration are scripting elements in JSP page using which we can add java code in the JSP pages.

    A scriptlet tag starts with <% and ends with %>. Any code written inside the scriptlet tags go into the _jspService() method. For example;

    Since most of the times we print dynamic data in JSP page using out.print() method, there is a shortcut to do this through JSP Expressions. JSP Expression starts with <%= and ends with %>.

    <% out.print(“codeNuclear”); %> can be written using JSP Expression as <%= “codeNuclear” %>

    Notice that anything between <%= %> is sent as parameter to out.print() method. Also notice that scriptlets can contain multiple java statements and always ends with semicolon (;) but expression doesn’t end with semicolon.

    JSP Declarations are used to declare member methods and variables of servlet class. JSP Declarations starts with <%! and ends with %>.

    For example we can create an int variable in JSP at class level as <%! public static int count=0; %>.

  10. What are implicit objects in JSP?

    Implicit objects in JSP are the Java objects that the JSP Container makes available to developers in each page. These objects need not be declared or instantiated by the JSP author.

    They are automatically instantiated by the container and are accessed using standard variables; hence, they are called implicit objects.The implicit objects available in JSP are as follows:

    • request
    • response
    • pageContext
    • session
    • application
    • out
    • config
    • page
    • exception

    The implicit objects are parsed by the container and inserted into the generated servlet code. They are available only within the jspService method and not in any declaration.

  11. Is it possible for one JSP to extend another java class if yes how?

    Yes. It is possible we can extends another JSP using this <%@ include page extends=”classname” %> it’s a perfectly correct because when JSP is converted to servlet its implements javax.servlet.jsp.HttpJspPage interface, so for jsp page its possible to extend another java class.

    It is not advisable to write java code in jsp instead better to use expression language and tag library.

  12. What are the attributes of page directive?

    There are thirteen attributes defined for a page directive of which the important attributes are as follows:

    • import: It specifies the packages that are to be imported.
    • session: It specifies whether a session data is available to the JSP page.
    • contentType: It allows a user to set the content-type for a page.
    • isELIgnored: It specifies whether the EL expressions are ignored when a JSP is translated to a servlet.
  13. What are the JSP standard actions?

    The JSP standard actions affect the overall runtime behavior of a JSP page and also the response sent back to the client.

    They can be used to include a file at the request time, to find or instantiate a JavaBean, to forward a request to a new page, to generate a browser-specific code, etc.

  14. Which implicit object is not available in normal JSP pages?

    JSP exception implicit object is not available in normal JSP pages and it’s used in JSP error pages only to catch the exception thrown by the JSP pages and provide useful message to the client.

  15. How can you pass information form one jsp to included jsp?

    Using <jsp: param> tag we can pass parameter from main jsp to included jsp page

    Example:

  16. How can we disable java code or scripting in JSP page?

    We can disable scripting elements in JSP pages through deployment descriptor configuration like below.

    Above url-pattern will disable scripting for all the JSP pages but if we want to disable it only for specific page, we can give the JSP file name itself.

  17. What are the types of JSTL tags?

    Based on the JSTL functions, they are categorized into five types.

    1. Core Tags: Core tags provide support for iteration, conditional logic, catch exception, url, forward or redirect response etc.
    2. Formatting and Localization Tags: These tags are provided for formatting of Numbers, Dates and i18n support through locales and resource bundles.
    3. SQL Tags: JSTL SQL Tags provide support for interaction with relational databases such as Oracle, MySql etc.
    4. XML Tags: XML tags are used to work with XML documents such as parsing XML, transforming XML data and XPath expressions evaluation.
    5. JSTL Functions Tags: JSTL tags provide a number of functions that we can use to perform common operation, most of them are for String manipulation such as String Concatenation, Split String etc.

  18. How to ignore the EL expression evaluation in a JSP?

    We can ignore EL evaluation in JSP page by two ways.

    1. Using page directive as <%@ page isELIgnored=”true” %>
    2. Configuring in web.xml – better approach when you want to disable EL evaluation for many JSP pages.
  19. How can we prevent implicit session creation in JSP?

  20. What is a TLD file?

    TLD stands for Tag Library Descriptor. TLD file is an XML file containing details of all the tags being created. It should be placed in WEB-INF directory.

  21. What is JSTL?

    The Java Server pages Standard Tag Library (JSTL) is a collection of useful tags which encapsulates some useful functionality widely used in many JSP applications.

  22. What are the types of JSTL tags?

    Based on the functionality there are five categories of JSTL tags:

    • Core tags
    • Formatting tags
    • SQL tags
    • XML tags
    • JSTL functions
  23. How can we implement a thread-safe JSP page?

    You can make your JSPs thread-safe by having them implement the SingleThreadModel interface. This is done by adding the directive <%@ page isThreadSafe=”false” %> within your JSP page.

  24. What are the difference between GET Method and POST Method?

    Refer our article on Difference between JSP and Servlets

It's good to share...Share on FacebookTweet about this on TwitterShare on LinkedInPin on PinterestShare on Google+Email this to someone

Leave a Reply

Your email address will not be published. Required fields are marked *