Monday, June 29, 2009

JSP Interview Questions

1) What is JSP? Describe its concept
  • JSP is a technology that combines HTML/XML markup languages and elements of Java programming Language to return dynamic content to the Web client.

2) What is client-side vs. server-side validation?
client-side validation (client-tier)
  • Java Script is used for client-side validation.
  • Validation takes place in client-side within your browser.
  • Java Script can be used to submit your form data after successful validation.
  • No extra network trip is required when there are validation errors because form does not have to be submitted.
server-side validation (presentation-tier)
  • Form data is submitted to the server and validation is carried out in the server.
  • Extra network round trip is required when there arevalidation errors because validation errors need to be reported back to the client and the form data has to be resubmitted.

3) What is JSP translation phase or compilation phase?
  • JSPs have a translation or a compilation process where the JSP engine translates and compiles a JSP file into a JSP Servlet.
  • The translated and compiled JSP Servlet moves to the execution phase (run time) where they can handle requests and send responses.
  • The JSPs can be compiled ahead of time (i.e. precompiled) using application server tools/settings or by writing our own script.

4) Explain the life cycle methods of a JSP?
  • Pre-translated: Before the JSP file has been translated and compiled into the Servlet.
  • Translated: The JSP file has been translated and compiled as a Servlet.
  • Initialized: Prior to handling the requests in the service method the container calls the jspInit() to initialize the Servlet. Called only once per Servlet instance.
  • Servicing: Services the client requests. Container calls the _jspService() method for each request.
  • Out of service: The Servlet instance is out of service. The container calls the jspDestroy() method.

5) What are context initialization parameters
  • Context initialization parameters are specified by the in the web.xml file, these are initialization parameter for the whole application and not specific to any servlet or JSP.

6) What is an output comment
  • A comment that is sent to the client in the viewable page source. The JSP engine handles an output comment as un-interpreted HTML text, returning the comment in the HTML output sent to the client. You can see the comment by viewing the page source from your Web browser. JSP Syntax


7) What is a Hidden Comment
  • A comment that documents the JSP page but is not sent to the client.
  • The JSP engine ignores a hidden comment, and does not process any code within hidden comment tags.
  • JSP Syntax

8) What is an 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.
  • Because the value of an expression is converted to a String, you can use an expression within text in a JSP file.
  • LikeYou cannot use a semicolon to end an expression

9) What is a Declaration
  • It declares one or more variables or methods for use later in the JSP source file.
  • A declaration must contain at least one complete declarative statement.
  • You can declare any number of variables or methods within one declaration tag, as long as semicolons separate them.
  • The declaration must be valid in the scripting language used in the JSP file.

10) What is a Scriptlet
  • A scriptlet can contain any number of language statements, variable or method declarations, or expressions that are valid in the page scripting language.
  • Within scriptlet tags, you can declare variables or methods to use later in the file, write expressions valid in the page scripting language.
  • Use any of the JSP implicit objects or any object declared with a tag.
  • You must write plain text, HTML-encoded text, or other JSP tags outside the scriptlet.
  • Scriptlets are executed at request time, when the JSP engine processes the client request.
  • If the scriptlet produces output, the output is stored in the out object, from which you can display it.

11) What are the implicit objects
  • Certain objects that are available for the use in JSP documents without being declared first.
  • These objects are parsed by the JSP engine and inserted into the generated servlet.
  • The implicit objects are:
  • request response pageContext session application out config page exception

12) What is the difference between forward and sendRedirect
  • When you invoke a forward request, the request is sent to another resource on the server, without the client being informed that a different resource is going to process the request.
  • This process occurs completely with in the web container And then returns to the calling method.
  • When a sendRedirect method is invoked, it causes the web container to return to the browser indicating that a new URL should be requested.
  • Because the browser issues a completely new request any object that are stored as request attributes before the redirect occurs will be lost. This extra round trip a redirect is slower than forward.

13) What are the different scope values
  • The different scope values for are:page request session application

14) Why are JSP pages the preferred API for creating a web-based client program
  • Because no plug-ins or security policy files are needed on the client systems(applet does).
  • Also, JSP pages enable cleaner and more module application design because they provide a way to separate applications programming from web page design.
  • This means personnel involved in web page design do not need to understand Java programming language syntax to do their jobs.

15) Is JSP technology extensible
  • Yes, it is. JSP technology is extensible through the development of custom actions, or tags, which are encapsulated in tag libraries.

16) What is difference between custom JSP tags and beans
  • Custom JSP tag is a tag you defined. You define how a tag, its attributes and its body are interpreted, and then group your tags into collections called tag libraries that can be used in any number of JSP files.
  • Custom tags and beans accomplish the same goals of encapsulating complex behavior into simple and accessible forms.
  • There are several differences:
  • Custom tags can manipulate JSP content; beans cannot.
  • Complex operations can be reduced to a significantly simpler form with custom tags than with beans.
  • Custom tags require quite a bit more work to set up than do beans.  Custom tags usually define relatively self-contained behavior, whereas beans are often defined in one servlet and used in a different servlet or JSP page.
  • Custom tags are available only in JSP 1.1 and later, but beans can be used in all JSP 1.x versions.

17) How does JSP handle run-time exceptions
  • You can use the errorPage attribute of the page directive to have uncaught run-time exceptions automatically forwarded to an error processing page.
  • For example: redirects the browser to the JSP page error.jsp if an uncaught exception is encountered during request processing.
  • Within error.jsp, if you indicate that it is an error-processing page, via the directive: Throwable object describing the exception may be accessed within the error page via the exception implicit object.
  • Note: You must always use a relative URL as the value for the errorPage attribute.

18) What is the difference between ServletContext and PageContext
ServletContext: Gives the information about the container.
PageContext: Gives the information about the Request.


19) What is the difference in using request.getRequestDispatcher() and context.getRequestDispatcher()
  • request.getRequestDispatcher(path): In order to create it we need to give the relative path of the resource,
  • context.getRequestDispatcher(path): In order to create it we need to give the absolute path of the resource.

20) How to pass information from JSP to included JSP
  • Using The is used for including non-JSP files.

23) What is the difference between directive include and jsp include
  • <%@ include>: Used to include static resources during translation time.
  • JSP include: Used to include dynamic content or static content during runtime.

References:
  1. Java/J2EE Job Interview Companion by Arulkumaran.
  2. http://www.allapplabs.com/index.html
  3. http://www.java-interview.com/index.html
  4. Headfirst Servlet and Jsp by Bryan Basham, Kathy Sierra, Bert Bates




2 comments:

  1. Very good collection. Can you put java script interview questions which may ask affen?

    ReplyDelete
  2. Ya sure, I'm working on preparing interview questions on frame works. I will post java script soon.

    ReplyDelete