Monday, 25 June 2012

JSP Interview Questions and Answers

Jsp Interview Questions-->1

1. What is JSP  ? Describe its concept?

Java Server Pages (JSP) is a server side component for the generation of dynamic information as the response. Best suitable to implement view components (presentation layer components). It is part of SUN’s J2EE platform.

2 . Explain the benefits of JSP?

These are some of the benefits due to the usage of JSP they are:
Portability, reusability and logic components of the language can be used across various platforms.
Memory and exception management.
Has wide range of API which increases the output functionality.
Low maintenance and easy deployment.
Robust performance on multiple requests.

3. 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.

4 .Can we implement an interface in a JSP?

No

5 What are the advantages of JSP over Servlet?

1.     Best suitable for view components
2.     we can separate presentation and business logic
3.     The JSP author not required to have strong java knowledge
4.     If we are performing any changes to the JSP, then not required to recompile and reload explicitly
5.     We can reduce development time.

6. Differences between Servlets and JSP?

Servlets
JSP
1. Best suitable for processing logic
1. Best suitable for presentation logic
2. we cannot separate business and presentation logic
2. Separation of presentation and business logic is possible
3. Servlet developer should have strong knowledge in Java
3.JSP author is not required to have strong knowledge in Java
4. For source code changes ,we have to perform explicitly compilation
4. For source code changes ,it is not required to perform explicit compilation
5. Relatively development time is more
5. Relatively development time is less

7 . Explain the differences between ASP and JSP?

The big difference between both of these technologies lies with the design of the software. JSP technology is server and platform independent whereas ASP relies primarily on Microsoft technologies.

8 . Can I stop JSP execution while in the midst of processing a request?

Yes. Preemptive termination of request processing on an error condition is a good way to maximize the throughput of a high-volume JSP engine. The trick (assuming Java is your scripting language) is to use the return statement when we want to terminate further processing.

9. How to Protect JSPs from direct access ?

If the JSP is secured resource then we can place inside WEB-INF folder so that end user is not allowed to access directly by the name. We can provide the url pattern by configuring in web.xml
<web-app>
    <servlet>
          <servlet-name>Demo JSP</servlet-name>
           <jsp-file>/WEB-INF/test.jsp</jsp-file>
    <sevlet>
        <servlet-mapping>
             <servlet-name>Demo JSP</servlet-name>
             <url-pattern>/test</url-pattern>
        </servlet-mapping>
            
..
</web-app>

10. Explain JSP API ?

The JSP API contains only one package : javax.servlet.jsp
 It contains the following 2 interfaces:
1.     JspPage:
         This interface defines the two life cycle methods jspInit() and jspDestroy().
1.     HttpJspPage:
This interface  defines only one life cyle method _jspService() method.
      Every generated servlet for the jsps should implement either JspPage or HttpJspPage interface either directly or indirectly.

11.  What are the lifecycle phases of a JSP?

Life cycle of JSP contains the following phases:
1.    Page translation:-converting from .jsp file to .java file
2.    Page compilation:- converting .java to .
class file
3.    Page loading:- This class file is loaded.
4.    Create an instance:- Instance of servlet is created
5.    jspInit() method is called
6.    _jspService() is called to handle service calls
7.    jspDestroy() is called to destroy it when the servlet is not required.

  
12.  Explain the life-cycle mehtods in JSP?



The jspInit()- The container calls the jspInit() to initialize te servlet instance.It is called before any other method, and is called only once for a servlet instance.
The _jspservice()- The container calls the _jspservice() for each request, passing it the request and the response objects.
The jspDestroy()- The container calls this when it decides take the instance out of service. It is the last method called n the servlet instance
.

13. Difference between _jspService() and other life cycle methods.

JSP contains three life cycle methods namely jspInit( ), _jspService() and jspDestroy(). In these, jspInit() and jspDestroy() can be overridden and we cannot override _jspService().
Webcontainer always generate _jspService() method with JSP content. If we are writing _jspService() method , then generated servlet contains 2 _jspService() methods which will cause compile time error.
To show this difference _jspService() method is prefixed with ‘_’ by the JSP container and the other two methods jspInit() and jspDestroy() has no special prefixes.


14. What is the jspInit() method?


The jspInit() method of the javax.servlet.jsp.JspPage interface is similar to the init() method of servlets. This method is invoked by the container only once when a JSP page is initialized. It can be overridden by a page author to initialize resources such as database and network connections, and to allow a JSP page to read persistent configuration data.

15. What is the _jspService() method?


SThe _jspService() method of the javax.servlet.jsp.HttpJspPage interface is invoked every time a new request comes to a JSP page. This method takes the HttpServletRequest and HttpServletResponse objects as its arguments. A page author cannot override this method, as its implementation is provided by the container.

16.  What is the jspDestroy() method?


The jspDestroy() method of the javax.servlet.jsp.JspPage interface is invoked by the container when a JSP page is about to be destroyed. This method is similar to the destroy() method of servlets. It can be overridden by a page author to perform any cleanup operation such as closing a database connection.

17.  What JSP lifecycle methods can I override?


We can override jspInit() and jspDestroy() methods but we cannot override _jspService() method.

18.  How can I override the jspInit() and jspDestroy() methods within a JSP page?


By using JSP declation tag
<%!
        public void jspInit() {
                . . .
        }
%>
<%!    
        public void jspDestroy() {
                . . .  
        }
%>


19 . Explain about translation and execution of Java Server pages?

A java server page is executed within a Java container. A Java container converts a Java file into a servlet. Conversion happens only once when the application is deployed onto the web server. During the process of compilation Java compiler checks for modifications if any modifications are present it would modify and then execute it.



20 . Why is _jspService() method starting with an '_' while other life cycle methods do not?

_jspService() method will be written by the container hence any methods which are not to be overridden by the end user are typically written starting with an '_'. This is the reason why we don't override _jspService() method in any JSP page.


21.  How to pre-compile JSP?

Add jsp_precompile as a request parameter and send a request to the JSP file. This will make the jsp pre-compile.

http://localhost:8080/jsp1/test.jsp?jsp_precompile=true

It causes excution of JSP life cycle until jspInit() method without executing _jspService() method.

22. The benefits of pre-compiling a JSP page?

It removes the start-up lag that occurs when a container must translate a JSP page upon receipt of the first request.

23.How many JSP scripting elements and explain them?

 Inside JSP four types of scripting elements are allowed.
  1. Scriptlet     <%  any java code    %>
     Can be used to place java code. 
  2. declarative     <%! Java declaration  %>
    Can be used to declare class level variables and methods
  3. expression:   <%=  java expression %>
      To print java expressions in the JSP
  4. comment   <%--   jsp comment  --%>

24. What is a Scriptlet?

JSP scriptlet can be used to place java code.
Syntax:
<%
    Any java code
%>
The java code present in the scriptlet will be placed directly inside _jspService() method .

25. What is a JSP declarative?

JSP declarations are used to declare class variables and methods (both instance and static)  in a JSP page. These declations will be placed directly at class level in the generated servlet and these are available to the entire JSP.

Syntax:
      <%!    This is my declarative  %>
  
       Eg:    <%!  int j = 10;  %>



Reactions:

0 comments:

Post a Comment