Monday, May 30, 2016

JSP Interview Questions and Answers

1. Can you tell me JSP life cycle methods and JSP life cycle phases ?

JSP Life Cycle methods
 
Jspinit()      - It will be invoked when the JSP page is initialized.

_jspService()  - It corresponds to the body of the jsp page and it will be handle for requests  
                 and response for all seven HTTP methods ie. GET, POST, DELETE etc.

jspDestroy()   - CleanUp activity will be handled by this method.

Refer for flow diagram

JSP Life Cycle Phases

1. Translation phase
2. Compilation phase
3  Instantiation phase
4  Initialization phase
5  Servicing phase
6  Destruction phase

2. What are the Directives in Jsp?

jsp engine will use this as a translation time instruction.

1. Page directive
2. Include directive
3. Taglib directive

3. What are the Jsp Implicit Objects?

Implicit objects will be created by container and developer no need to create it explicitly. Totally 9 implicit objects are available in jsp.

    1. out
    2. request
    3. response
    4. config
    5. application
    6. session
    7. pagecontext
    8. page
    9. exception

4. Difference between include directive and include action?

include directive -
<%@ include>

1. It will be processed at translation time.

2. It wil be static
<%@ include file="header.html" %>

3. You can not pass parameters.


include action (jsp:include )

1. It will be processed at run time.

2. It will be dynamic.

             <jsp:include page="header.jsp" />

              header.jsp will be addded at request time.

3. We can pass parameters by using below.

             <jsp:include page="header.jsp">
                   <jsp param name="menu" value="obj1"/>
             </jsp:include>

5.  Is there anyway to disable session in JSP ? 

Yes,  by using page directive

<%@ page session="false" %>

6.  How to handle exception in JSP and explain?

Two ways we can handle exception in JSP

1. By using isErrorPage and errorPage of page directive attributes.
2. By using <error-page> tag in Deployment Descriptor. (web.xml)


1.1 isErrorPage

<%@ page isErrorPage="true" %>
<html>
<body>
<b> Sorry! This requested page not availbale </b>
<img src="ErrorImage.jpg"/>
</body>
</html>

The above file (Error.jsp) will be executed if any error occurred. Error will be identified by using page directive attribute of isErrorPage. Remember it will be developed only to display error.

1.2 errorPage

It will tells to the Web Container that if any exception occurred at particular page, then forward the request to an error page. So both are totally different logic, but for error purpose only. 

<%@ page errorPage="error.jsp">

<html>
<body>

<!-- Your logical code and any exception occurred while executing this -->

</body>
</html>

 2. <error-page> tag in Deployment Descriptor

It will give some good option to handle error. You should define this web.xml

For all type of Exception

<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/error.jsp</location>
</error-page>

For particular exception
<error-page>
<exception-type>java.lang.ArithmeticException</exception-type>
<location>/error.jsp</location>
</error-page>

For HTTP Status code
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>

<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>

7. What are the action tags in JSP ?

Action Tag Tag Description
jsp:forwardforward the request to a new page
jsp:getProperty    retrieve a property from a JavaBean instance.

jsp:include
include the runtime response of a JSP page.
jsp:plugin
Generates client browser-specific construct that makes an OBJECT or EMBED tag for the Java Applets
jsp:useBean
instantiates a JavaBean
jsp:param
Adds parameters to the request
jsp:elementDefines XML elements dynamically
jsp:textUse to write template text in JSP pages and documents.
jsp:setProperty
store data in JavaBeans instance.
jsp:fallbackSupplies alternate text if java applet is unavailable on the client
jsp:bodyUsed within standard or custom tags to supply the tag body.
jsp:attributedefines dynamically defined XML element's attribute

8.Differentiate between response.sendRedirect(url) and <jsp:forward page > ?

jsp:forward - It will forward one request object to another , which is to be HTML, servlet or JSP.  But important point here is, it should be within application context. (Application).

sendRedirect (url) - It will transer one request to another resource , which is to be different domain or different server.

9.  How you can implement thread safe in jsp ?

To make JSP as thread-safe, you can implement the SingleThreadModel interface , which prevents two threads from accessing the service method at the same time.

The JSP page by default is not thread safe, if you want to make it thread safe then you should add following directive in your JSP page declaration:

<%@ page isThreadSafe="false" %>

10. Is JSP technology extensible?

Yes, JSP is easily extensible witht the help of modification of tags, custom actions etc.,

11.  How will you pass information from one JSP to another JSP?

<Jsp:param> allows us to pass information between multiple Jsp’s.

12. Why does _jspService() start with an ‘_’ but other lifecycle methods does not start ?

The reason is, we should not override _jspservice method, since it will be executed by container. But remaining two methods we can override. 


So '_'  representing here, we should should not override. If you override compiler will give an error.

13.  How to avoid session created automatically in JSP?

 <%@ page session=”false”  %>

14.  What are the various scope values for <jsp:useBean> ?

1)application

2)request

3)page

4)session


15. How to disable java code or scripting in JSP page?

We should add below code in deployment descriptor(web.xml)
   

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern
        <scripting-invalid>true</scripting-invalid>
    </jsp-property-group>
</jsp-config>
 
Here, it will disable all jsp page , but you want to allow to any special jsp then you should add that JSP file name manually.

No comments:

Post a Comment