MY mENU


Wednesday, 21 March 2012

Jsp Interview Questions-part3

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.

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.

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 --%>

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 .

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; %>

How can I declare methods within my JSP page?
We can declare methods by using JSP declarative tag.

<%!
public int add(inti,intj){
return i+j;
}
%>

What is the difference b/w variable declared inside a declaration and variable declared in scriplet ?
Variable declared inside declaration part is treated as a instance variable and will be placed directly at class level in the generated servlet.

<%! int k = 10; %>

Variable declared in a scriptlet will be placed inside _jspService() method of generated servlet. It acts as local variable.
<%
int k = 10;
%>
What is a Expression?
JSP Expression can be used to print expression to the JSP.
Syntax:

<%= java expression %>

Eg: <%= new java.util.Date() %>

The expression in expression tag should not ends with semi-colon . The expression value will become argument to the out.pritln() method in the generated servlet.

 What is output comment?
The comment which is visible in the source of the response is called output comment.

No comments:

Post a Comment