Wednesday, July 17, 2013

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: 
  1. Introduction to JSPs 
  2. JSP Life Cycle
  3. JSP Implicit Objects 

Questions: 


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:

application
config
exception
out
page
pageContext
request
response and 
session

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 questions that you want answer for, please leave a comment on this page OR drop a note to Snehal[at]TechProceed[dot]com and I will answer them.