JSP Interview Questions and Answers

    1 Votes

Top 75+ JSP Interview Questions and answers for freshers and 2-6 year experienced java programmers and web developers on basic and advanced topics.

1. What is a JSP?

JSP files are HTML files with special tags that contain java source code that provide dynamic content. Google search engine, the search page is dynamic will display the search results based on the user’s search request.

2. Compare between Servlet and JSP?
  • In servlets HTML is written inside java code using print statements. In JSP java code is embedded inside HTML.
  • JSP pages are converted to servlets by the web container so actually it can do the same thing as java servlets.
  • JSP are easier for creating HTML content but servlets are easier for writing the java code.
  • A combination of JSP and servlet can be used to separate the presentation(HTML) and logic (java code) and taking the advantage of both.
3. Explain JSP life cycle phases?
JSP life cycle phases are:
  • Translation and compilation: The JSP will be translated into servlet java file and compiled to servlet class by the web container.
  • Instantiation: The web container creates an instance of the servlet class.
  • Initialization: The web container instantiates the servlet and makes it ready for servicing the request.
  • Service: This is the phase during which the JSP services the user requests.
  • Destroy: JSP is destroyed by the web container when the application is uninstalled.
4. Explain JSP life cycle methods?
JSP life cycle methods are:
  • jspInit() – The web container calls the jspInit() to initialize the servlet instance generated. It is invoked before servicing the client request and invoke only once for a servlet instance.
  • _jspService() – The web container invokes the jspService() for each user request, passing it the request and the response objects.
  • jspDestroy() - The web container invokes this when it decides to take the instance out of service.
5. What are the types of elements in JSP?
There are four types of elements in JSP:
  • Directives
  • Actions
  • Scripting
  • Comments 
6. What is a scripting element?

Scripting element are used to embed java code in JSP files. Contents of JSP scriptlet goes into _jspService() method during the translation phase.

7. What are the types of scripting elements?

There are three types of scripting elements:

  • Scriptlets
  • Declaration
  • Expression

8. How to create a scriptlet?

Scriptlets are embedded between <% and %> delimiters.

Syntax: <% java code goes in here %>

9. What is declaration element?

  • Declarations are used to declare, define methods and instance variables.
  • Declaration tags does not produce any output that is sent to client.
  • The methods and classes declared will be translated as class level variables and methods during translation.

10. How to declare methods or variables in JSP?

Methods or variables are declared using <%! and %> delimiters.

Syntax: <%! variable=0; %>

11. What is expression element?

Expression element are used to write dynamic content back to the client browser. It is used in place of out.print() method. Only expressions are supported inside the tag. Declarations of methods and variables is not possible inside this tag.

12. What happens to expression element during translation?

During translation the return type of expression goes as argument into out.print() method.

13. How to use expression element?

Expressions are embedded in <%= and %> delimiters.

Syntax: <% expression 1 %>

14. What are the two types of comments supported by JSP.

There are two types of comments supported by JSP:

HTML comment - <!—This is a comment -->
JSP comment - <%-- This is a comment --%>

15. What is the difference between HTML and JSP comments?

HTML comments are passed during the translation phase and hence can be viewed in the page source in the browser. JSP comments are converted to normal java comments during the translation process and will not appear in the output page source.

Page 1 of 5

Popular Videos

How to speak to people

How to speak so that people want to listen.

Got a tip or Question?
Let us know

Related Articles

Servlet Interview Questions and Answers
Advanced Servlet Interview Questions and Answers
EJB Interview Questions and Answers
Spring Interview Questions and Answers