Search This Blog

Jsp Elements


Q:- what happens if jsp equivalent servlet generated by webserver is modified directly?
A:- After modification of jsp equivalent servlet. If servlet is compiled and source code of jsp is not modified then modification done directly in jsp equivalent servlet will be effected.
- Other wise if jsp equivalent servlet is not compiled manually (or) the original source code of jsp is modified the direct modification done in jsp equivalent servlet will not be effected.
----------------
Note:-To compile jsp equivalent servlet manually and explicitly which is generated by tomcat page compiler(jasper) the following two jar files are required in the class path.
1.jsp_API.jar
2.Jasper_Runtime.jar

Jsp Elements
1. Scripting Elements:-
The tags of Jsp's which take java code are called scripting Elements.
a)Scriptlet
(<%java code%>)
b)Declaration
(<%!java code%>)
c)Expression
(<%= java code%>)

2. Jsp Comments:-
(<%-- java code --%>

3. Directive Tags:-
- Directive tags of jsps perform global operations related to jsp's like importing packages and etc.,
a) Page Directive(<%@Page Attributes%>)
b) Include Directive(<%@include Attributes%>)
c) TagLib Directive(<%@taglib Attributes%>)

4. Standard Action Tags:-
- These tag's perform operations at Runtime
<jsp:include>
<jsp:forward>
<jsp:usebean>
<jsp:setProperty>
<jsp:getProperty>
<jsp:param>
<jsp:params>
<jsp:plugin>
<jsp:fallback>

- All these tags follows xml syntax.
- All above are the predefined standard tags in jsp along with these tags we can work with Custom Action Tags (user defined) and JSTL Tags(Jsp Standard Tag Library Tags).

1.1 Scriptlet
Standard syntax:
<%
java code that comes in _jspService(req,res)
%>
xml syntax:
<jsp:scriptlet>
java code
</jsp:scriptlet>
Ex1:-
<%
int a=10;
int b=20;
int c=a+b;
out.println("sum is"+c);
%>
- This java code of scriptlet comes as it is in _jspService(req,res) of jsp equivalent servlet.
Ex2:-
<jsp:scriptlet>
java.util.Date d= new java.util.Date();
String s=d.toString();
out.println("date is"+s);
</jsp:scriptlet>
Note1:- If you declare variables in scriptlet they come as local variables of _jspService(req,res)
Note2:- do not place method definitions in scriptlet as java doesn't support nested methods.
1.2. Declarative
Standard Syntax:-
<%! Declaration of Instance variables
Implementation of userdefined methods
Implementation of jspinit() and jspDestory() Life cycle methods
%>
Xml Syntax:-
<jsp:declaration>
--------
--------
</jsp:declaration>
Ex1:-
<%! int a=10;%>
"a" comes as an instance variable in jsp equivalent servlet.
Ex2:-
<%! public int sum(int x, int y)
{
return x+y;
}
%>
Ex3:-
<%!
public void jspInit()
{
//initialization code
}%>
<%! public void jspDestory()
{
//uninitialization code
}%>
Ex4:-
<jsp:declaration>
int xyz=10;
</jsp:declaration>
- All the variables declared using declaration tag comes as instance variables of jsp equivalent servlet.
Question:- what is the difference between variable declared in scriptlet and variable declared in declaration tag and how to differentiate one from another of both have got same name ?
Answer:- Variables declared in scriptlet come as local variable in jsp equivalent servlet(means _jspService). Variables declared in declaration tag will come as instance variables of jsp equivalent servlet.  when both variables have got same name to differentiate them use either this keyword or implicit object page as shown below.
<%! int a=20;%>
<% int a=10;
out.println("square a is"+(a' this.a));%>
Q:- How many types of objects are there in jsp?
A:- Jsp allows two types of objects
1. Explicit objects
-  These objects are created by programmer manually like string class object.  Date class object and etc.,
2. Implicit objects
-  There are total 9 ready made objects in jsp like out,page,request,response  and etc.,

1.3 Expression Tag:-
Standard Syntax:-
<%= java expression %>
Xml Syntax
<jsp:expression>
java expression
</jsp:expression>
- The expression tag evaluates given expression and displays the generated result on browser window.

Ex1:-
<%= a+b %>
-- Evaluates a+b expression and writes result to browser
<%=a+b,c+d%>
-- This is invalid code (because two expressions are there) and an expression can evaluate only one expression at a time.

Ex2:-
<%=a%>
- writes "a" variable value to browser.

Ex3:-
<%= sum(10,20)%>
- calls method and writes result to browser.

Ex4:-
<%=new java.util.Date()%>
-->it creates object and writes result to the browser.
<%=Integer.parseInt("30")%>

- In order to call both userdefined and predefined methods and to evaluate java expression then to write their results to the browser window. we take the support of Expression Tag.
- The equivalent java code of Expression Tag shown _jspService() jsp equivalent servlet.

Ex5:-
<jsp:expression>
new java.util.Date()
</jsp:expression>