Wednesday, October 7, 2015

JSP Interview Questions and Answers

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.

No comments: