Wednesday, March 17, 2010

JSP FAQs - 3

 

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

26. 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;
}
%>

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

28.What are the three  kinds of comments in JSP and what's the difference between them?


Three types of comments are allowed in JSP

    1. JSP Comment:

<%--  this is jsp comment  --%>

This is also known as hidden comment and it is visible only in the JSP and in rest of phases of JSP life cycle it is not visible.

    1. HTML Comment:

<!--  this is HTMl comment -- >

This is also known as template text comment or output comment. It is visible in all phases of JSP including source code of generated response.

    1. Java Comments.

With in the script lets we can use even java comments .

<%
 // single line java comment
/* this is multiline comment  */

%>

This type of comments also known as scripting comments and these are visible in the generated servlet also.

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

   <!--  this is HTMl comment -- >

30. What is a Hidden Comment?
    
 <%--  this is jsp comment  --%>

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

 

No comments:

Post a Comment