JSP and Servlet Interview Questions Part-3
1} Why is _jspServic e () start with ‘_’?
Ans: _jspService() method will be written by the container hence any methods which are not to be overridden bythe end user are typically written starting with a '_'. T his is the reason why we don't override _jspService() method in any JSP page.
2} How to pre-c ompile jsp?
Ans: Add jsp_precompile as a request parameter and send a request to the JSP file. T his will make the jsp pre-compile. http://localhost:8080/jsp1/test.jsp?jsp_precompile=true
It causes execution of JSP life cycle until jspInit() method without executing _jspService() method.
3} What is the benefit of pre-c ompile jsp page?
Ans: It removes the start-up lag that occurs when a container must translate a JSP page upon receipt of the first request.
4} What is the difference between variable declared inside the declaration tag and variable declared in scriptlet?
Ans: Variable declared inside declaration part is treated as a instance variable and will be placed directly at class level in the generated servlet. Variable declared in a scriptlet will be placed inside _jspService () method of generated servlet. It acts as local variable.
5} What are the three kind of c omment in jsp?
Ans: These are the three types of commenst in jsp:
JSP Comment: <%-- this is jsp comment -- %>
HT ML Comment: <!-- this is HT Ml comment -- >
Java Comments: <% // sing le line java comment /* this is multiline comment */ %>
6} What is the output comment?
Ans: The comment which is visible in the source of the response is called output comment. <!-- this is HT Ml
comment -- >
7} What is a hidden comment?
Ans: T his is also known as JSP comment and it is visible only in the JSP and in rest of phases of JSP life cycle it is
not visible. <%-- this is jsp comment -- %>
8} How does jsp handle the run time exception?
Ans: You can use the errorpage attribute of the page directive to have uncaug ht run-time exceptions automatically
forwarded to an error processing page.
9} How can I implement the thread safe jsp page?
Ans: You can make your JSPs thread-safe by having them implement the Sing leT hreadModel interface. T his is
done by adding the directive in the JSP. <%@ page isThreadSafe="false" %>
10} Is there a way to referenc e the “this” variable within the jsp?
Ans: Yes, there is. The page implicit object is equivalent to "this", and returns a reference to the g enerated
servlet.
11} Can you make the use of servletO utputStream objec t within jsp?
Ans: Yes. By using g etOutputStream () method on response implicit object we can g et it.
12} What is autoflush?
Ans: T his command is used to autoflush the contents. If a value of true is used it indicates to flush the buffer whenever it is full. In case of false it indicates that an exception should be thrown whenever the buffer is full. If you are trying to access the page at the time of conversion of a JSP into servlet will result in error.
13} What is the different sc ope available in jsp?
Ans:The different scopes are:
page: Within the same page.
Request: After forward or include also you will get the request scope data.
Session: After sendRedirect also you will get the session scope data. All data stored in session is available to end user till session closed or browser closed. Application: Data will be available throughout the application. One user can store data in application
scope and other can get the data from application scope.
14} When to use application scope?
Ans: If we want to make our data available to the entire application then we have to use application scope.
15} Can a jsp page instantiate a serialized bean?
Ans: No problem! The use Bean action specifies the bean Name attribute, which can be used for indicating a
serialized bean.
16} In which situation we can use the static include and dynamic include?
Ans: If the target resource won’t change frequently, then it is recommended to use include directives. If the target
resource will change frequently, then it is recommended to use include action.
Ans: _jspService() method will be written by the container hence any methods which are not to be overridden bythe end user are typically written starting with a '_'. T his is the reason why we don't override _jspService() method in any JSP page.
2} How to pre-c ompile jsp?
Ans: Add jsp_precompile as a request parameter and send a request to the JSP file. T his will make the jsp pre-compile. http://localhost:8080/jsp1/test.jsp?jsp_precompile=true
It causes execution of JSP life cycle until jspInit() method without executing _jspService() method.
3} What is the benefit of pre-c ompile jsp page?
Ans: It removes the start-up lag that occurs when a container must translate a JSP page upon receipt of the first request.
4} What is the difference between variable declared inside the declaration tag and variable declared in scriptlet?
Ans: Variable declared inside declaration part is treated as a instance variable and will be placed directly at class level in the generated servlet. Variable declared in a scriptlet will be placed inside _jspService () method of generated servlet. It acts as local variable.
5} What are the three kind of c omment in jsp?
Ans: These are the three types of commenst in jsp:
JSP Comment: <%-- this is jsp comment -- %>
HT ML Comment: <!-- this is HT Ml comment -- >
Java Comments: <% // sing le line java comment /* this is multiline comment */ %>
6} What is the output comment?
Ans: The comment which is visible in the source of the response is called output comment. <!-- this is HT Ml
comment -- >
7} What is a hidden comment?
Ans: T his is also known as JSP comment and it is visible only in the JSP and in rest of phases of JSP life cycle it is
not visible. <%-- this is jsp comment -- %>
8} How does jsp handle the run time exception?
Ans: You can use the errorpage attribute of the page directive to have uncaug ht run-time exceptions automatically
forwarded to an error processing page.
9} How can I implement the thread safe jsp page?
Ans: You can make your JSPs thread-safe by having them implement the Sing leT hreadModel interface. T his is
done by adding the directive in the JSP. <%@ page isThreadSafe="false" %>
10} Is there a way to referenc e the “this” variable within the jsp?
Ans: Yes, there is. The page implicit object is equivalent to "this", and returns a reference to the g enerated
servlet.
11} Can you make the use of servletO utputStream objec t within jsp?
Ans: Yes. By using g etOutputStream () method on response implicit object we can g et it.
12} What is autoflush?
Ans: T his command is used to autoflush the contents. If a value of true is used it indicates to flush the buffer whenever it is full. In case of false it indicates that an exception should be thrown whenever the buffer is full. If you are trying to access the page at the time of conversion of a JSP into servlet will result in error.
13} What is the different sc ope available in jsp?
Ans:The different scopes are:
page: Within the same page.
Request: After forward or include also you will get the request scope data.
Session: After sendRedirect also you will get the session scope data. All data stored in session is available to end user till session closed or browser closed. Application: Data will be available throughout the application. One user can store data in application
scope and other can get the data from application scope.
14} When to use application scope?
Ans: If we want to make our data available to the entire application then we have to use application scope.
15} Can a jsp page instantiate a serialized bean?
Ans: No problem! The use Bean action specifies the bean Name attribute, which can be used for indicating a
serialized bean.
16} In which situation we can use the static include and dynamic include?
Ans: If the target resource won’t change frequently, then it is recommended to use include directives. If the target
resource will change frequently, then it is recommended to use include action.