Monday, April 18, 2011

JSP Interview Questions

The following are some questions you might encounter with respect to Java Server Pages or JSPs in any Interview. JSPs are an integral part of almost all J2EE Web Applications and any interviewer who is selecting people for his J2EE project would want a person who is confident & comfortable using them.

Apart from the questions below, there are a few articles that I have put up (as part of the SCWCD Certification series) on JSPs that you might find useful. You can use them to revise/review your understanding of Servlets.

They are:

Introduction to JSPs
JSP Life Cycle
JSP Implicit Objects


1. What is JSP?

JavaServer Pages (JSP) technology is the Java platform technology for delivering dynamic content to web applications in a portable, secure and well-defined way. The JSP Technology allows us to use HTML, Java, JavaScript and XML in a single file to create high quality and fully functionaly User Interface components for Web Applications.

2. What do you understand by JSP Actions?

JSP actions are XML tags that direct the server to use existing components or control the behavior of the JSP engine. JSP Actions consist of a typical (XML-based) prefix of "jsp" followed by a colon, followed by the action name followed by one or more attribute parameters.

There are six JSP Actions:

< jsp : include / >

< jsp : forward / >

< jsp : plugin / >

< jsp : usebean / >

< jsp : setProperty / >

< jsp : getProperty / >

3. What is the difference between < jsp : include page = ... > and < % @ include file = ... >?

Both the tags include information from one JSP page in another. The differences are:

< jsp : include page = ... >

This is like a function call from one jsp to another jsp. It is executed ( the included page is executed and the generated html content is included in the content of calling jsp) each time the client page is accessed by the client. This approach is useful while modularizing a web application. If the included file changes then the new content will be included in the output automatically.

< % @ include file = ... >

In this case the content of the included file is textually embedded in the page that have < % @ include file=".."> directive. In this case when the included file changes, the changed content will not get included automatically in the output. This approach is used when the code from one jsp file required to include in multiple jsp files.

4. What is the difference between < jsp : forward page = ... > and response.sendRedirect(url)?

The element forwards the request object containing the client request information from one JSP file to another file. The target file can be an HTML file, another JSP file, or a servlet, as long as it is in the same application context as the forwarding JSP file.

sendRedirect sends HTTP temporary redirect response to the browser, and browser creates a new request to go the redirected page. The response.sendRedirect also kills the session variables.

5. Name one advantage of JSP over Servlets?

Can contain HTML, JavaScript, XML and Java Code whereas Servlets can contain only Java Code, making JSPs more flexible and powerful than Servlets.

However, Servlets have their own place in a J2EE application and cannot be ignored altogether. They have their strengths too which cannot be overseen.

6. What are implicit Objects available to the JSP Page?

Implicit objects are the objects available to the JSP page. These objects are created by Web container and contain information related to a particular request, page, or application. The JSP implicit objects are:

response and

7. What are all the different scope values for the < jsp : useBean > tag?

< jsp : useBean > tag is used to use any java object in the jsp page. Here are the scope values for < jsp : useBean > tag:

a) page
b) request
c) session and
d) application

8. What is JSP Output Comments?

JSP Output Comments are the comments that can be viewed in the HTML source file. They are comments that are enclosed within the < ! - - Your Comments Here - - >

9. What is expression in JSP?

Expression tag is used to insert Java values directly into the output.

Syntax for the Expression tag is: < %= expression % >

An expression tag contains a scripting language expression that is evaluated, converted to a String, and inserted where the expression appears in the JSP file. The most commonly used language is regular Java.

10. What types of comments are available in the JSP?

There are two types of comments that are allowed in the JSP. They are hidden and output comments.

A hidden comment does not appear in the generated HTML output, while output comments appear in the generated output.

Example of hidden comment:
< % - - This is a hidden comment - - % >

Example of output comment:
< ! - - This is an output comment - - >

11. What is a JSP Scriptlet?

JSP Scriptlets is a term used to refer to pieces of Java code that can be embedded in a JSP PAge. Scriptlets begins with <% tag and ends with %> tag. Java code written inside scriptlet executes every time the JSP is invoked.

12. What are the life-cycle methods of JSP?

Life-cycle methods of the JSP are:

a) jspInit(): The container calls the jspInit() to initialize the servlet instance. It is called before any other method, and is called only once for a servlet instance.

b)_jspService(): The container calls the _jspservice() for each request and it passes the request and the response objects. _jspService() method cann't be overridden.

c) jspDestroy(): The container calls this when its instance is about to destroyed.
The jspInit() and jspDestroy() methods can be overridden within a JSP page.

13. What are JSP Custom tags?

JSP Custom tags are user defined JSP language elements. JSP custom tags are user defined tags that can encapsulate common functionality. For example you can write your own tag to access the database and performing database operations. You can also write custom tags to encapsulate both simple and complex behaviors in an easy to use syntax. The use of custom tags greatly enhances the functionality and simplifies the readability of JSP pages.

14. What is the role of JSP in MVC Model?

JSP is mostly used to develop the user interface, It plays are role of View in the MVC Model.

15. What do you understand by context initialization parameters?

The context-param element contains the declaration of a web application's servlet context initialization parameters.

< context - param >
< param - name > name < / param - name > < param - value > value < / param - value >
< / context-param >

The Context Parameters page lets you manage parameters that are accessed through the ServletContext.getInitParameterNames and ServletContext.getInitParameter methods.

16. Can you extend JSP technology?

Yes. JSP technology lets the programmer to extend the jsp to make the programming more easier. JSP can be extended and custom actions & tag libraries can be developed to enhance/extend its features.

17. What do you understand by JSP translation?

JSP translation is an action that refers to the convertion of the JSP Page into a Java Servlet. This class is essentially a servlet class wrapped with features for JSP functionality.

18. How can you prevent the Browser from Caching data of the Pages you visit?

By setting properties that prevent caching in your JSP Page. They are:

<% response.setHeader("pragma","no-cache");//HTTP 1.1 response.setHeader("Cache-Control","no-cache"); response.setHeader("Cache-Control","no-store"); response.addDateHeader("Expires", -1); response.setDateHeader("max-age", 0); //response.setIntHeader ("Expires", -1); //prevents caching at the proxy server response.addHeader("cache-Control", "private"); %>

19. How will you handle the runtime exception in your jsp page?

The errorPage attribute of the page directive can be used to catch run-time exceptions automatically and then forwarded to an error processing page. You can define the error page to which you want the request forwarded to, in case of an exception, in each JSP Page. Also, there should be another JSP that plays the role of the error page which has the flag isErrorPage set to True.

20. What is JavaServer Pages Standard Tag Library (JSTL) ?

A tag library that encapsulates core functionality common to many JSP applications. JSTL has support for common, structural tasks such as iteration and conditionals, tags for manipulating XML documents, internationalization and locale-specific formatting tags, SQL tags, and functions.

21. What is JSP container ?

A container that provides the same services as a servlet container and an engine that interprets and processes JSP pages into a servlet.

22. What is JSP custom action ?

A user-defined action described in a portable manner by a tag library descriptor and imported into a JSP page by a taglib directive. Custom actions are used to encapsulate recurring tasks in writing JSP pages.

23. What is JSP custom tag ?

A tag that references a JSP custom action.

24. What is JSP declaration ?

A JSP scripting element that declares methods, variables, or both in a JSP page.

25. What is JSP directive ?

A JSP element that gives an instruction to the JSP container and is interpreted at translation time.

26. What is JSP document ?

A JSP page written in XML syntax and subject to the constraints of XML documents.

27. What is JSP element ?

A portion of a JSP page that is recognized by a JSP translator. An element can be a directive, an action, or a scripting element.

28. What is JSP expression ?

A scripting element that contains a valid scripting language expression that is evaluated, converted to a String, and placed into the implicit out object.

29. What is JSP expression language ?

A language used to write expressions that access the properties of JavaBeans components. EL expressions can be used in static text and in any standard or custom tag attribute that can accept an expression.

30. What is JSP page ?

A text-based document containing static text and JSP elements that describes how to process a request to create a response. A JSP page is translated into and handles requests as a servlet.

31. What is JSP scripting element ?

A JSP declaration, scriptlet, or expression whose syntax is defined by the JSP specification and whose content is written according to the scripting language used in the JSP page. The JSP specification describes the syntax and semantics for the case where the language page attribute is "java".

32. What is JSP scriptlet ?

A JSP scripting element containing any code fragment that is valid in the scripting language used in the JSP page. The JSP specification describes what is a valid scriptlet for the case where the language page attribute is "java".

33. What is JSP tag file ?

A source file containing a reusable fragment of JSP code that is translated into a tag handler when a JSP page is translated into a servlet.

34. What is JSP tag handler ?

A Java programming language object that implements the behavior of a custom tag.
If you have any questions that you want answer for - please leave a comment on this page and I will answer them.

If you have any more questions on JSPs that you have faced during your interviews and wish to add them to this collection - pls drop a note to and I shall be glad to add them to this list.

More Java & J2EE Interview Questions with Answers - Click Here


  1. Replies
    1. Thank you Javin. You have got a nice collection too!!!

  2. 1. How can we declare third party classes and interfaces inside our jsp page?
    2. How can we declare and definr global variables and methods inside our jsp page?
    3. If we are declaring global variables and methods inside the jsp page.Where the global variables are going into generated servlet?
    4. Can we print the messages by using the expressions in case of jsp?
    5. Is it possible to incorporate local members using scriplets(Y/N)?
    6. While developing any jsp page, server can develop a .java file for a particular jsp page or not?
    7. What are the different types of directives in jsp?
    8. While developing any jsp page inside the tomcat server where the server generates the generated servlet?
    9. If server is generating the servlet for every particular jsp page ,that generated servlet is final (Y/N)?
    10. Every generated servlet by the server for a particular jsp is subclass to HttpJspBase?
    11. Every generated servlet by the server for a particular jsp is subclass to HttpJspBase.Here HttpJspBase is from which package?
    12. HttpJspBase is subclass to HttpServlet(Y/N)?
    13. All over rided methods from the HttpServlet to the HttpJspBase are final methods (Y/N)?
    14. In case of jsp what could be the default executable method inside the generated servlet?
    15. If we are developing jsp pages inside eclipse in which location server is generating the servlets for particular jsp pages?
    16. If we want to develop any jsp pages using eclipse where we can develop under web application?
    17. Is it Possible to include text file to jsp pages(Y/N)?
    18. Is it Possible to include html pages to jsp pages(Y/N)?

    1. Answers to your Questions:

      1. The third party classes/interfaces can be placed inside a JAR, place the JAR in the class-path of the project and refer to the fully qualified path of the class/interface inside the jsp just like any other class

      2 & 3. Variable declared inside declaration part is treated as a global variable.that means after convertion jsp file into servlet that variable will be in outside of service method or it will be declared as instance variable.And the scope is available to complete jsp and to complete in the converted servlet class.where as if u declare a variable inside a scriplet that variable will be declared inside a service method and the scope is with in the service method.

      4. By print - if you are talking about on the browser - Yes. You can have expressions/conditions and based on the output, certain text can be printed on screen in the browser

      5. If by members you are referring to variables - Yes

      6. Server does not develop. It translates the jsp file into a servlet file which is a .java extension file. So, Yes
      Refer to JSP Lifecycle about the translation -

      7. There are 3 directives - Page, Include and Taglib.
      Refer to the chapters below:
      Page Directive -
      JSP Tags & xml -

      8. It depends on the configurations your server is set up with. Usually it will be somewhere inside WEB-INF

      9. There is no such thing as a final servlet. Servlets are not regular java classes which you can modify/alter at runtime. So, for all practical purposes the servlet can be considered final because it doesnt get regenerated everytime you run the program. A JSP is translated/converted into a servlet once and used throughout the application instance

      10. Yes. Every servlet extends GenericServlet which inturn extends this class

      11. javax.servlet.jsp.HttpJspPage

      12. No.

      13. You cannot override a final method. So, I am not sure if this question is valid

      14. There is no such thing as a default executable method. The methods that get invoked while form submission is usually the doGet(), doPost and so on for a servlet. In JSPs the method that get invoked depend on what methods you call from within the code.

      15. Same as Q8. Depends on your local set up. Usually it will be somewhere inside WEB-INF

      16. It depends from company to company. Every company has a standard for web app development & code organization. Usually we create a folder named jsp and place all jsp files inside.

      17. Yes. Use the Include directive

      18. Yes. Use the Include directive

  3. Hi,
    There is no syntax in the syntax for jsp expressions in Q no. 9.


    1. Thanks for pointing out mate. Fixed it now.


  4. The answer to question 15 seems incomplete. Can you check? and thanks for the questions & answers. they are very useful


    1. Thank you for pointing it out. I have updated the page. The syntax got lost because the blogger editor treated the code literally as code and processed it. So, had to add spaces. Pls check now


  5. Thanks for pointing out mate. Fixed it now.


  6. The answer to question 18 is incomplete:
    By setting properties that prevent caching in your JSP Page. They are:

    1. Anonymous - I can see answer for Q 18. Can you check again??


  7. How to do pagination in jsp? pls tell me this is asked in recent interviews.

    1. Pagination is not something I can explain in one paragraph. check out these links:


© 2013 by All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.


Google+ Followers