Skip to main content

Scripting Tags

JSP Tags/ Elements



(1)Scriptlet

    Standard syn:-
  1.         <%.........%> 
   xml syn:-
  1.        <jsp:scriptlet>...</jsp:scriptlet>
Note: All scripting tags allows us to place script code(java code)
1.)The code placed in scriptlet go to _jspService(-,-) of JES class

2.)In a jsp page we can have zero or more scriptlets

3.)We place request processing logics in scriptlets

4.)Variables declared in scriptlets becomes the local variables in _jspService(-,-) of JES class in first.jsp

In first.jsp:
  1. <% int a=10; 
  2. out.println("square:"+(a*a));%>
In first_jsp.java(JES):
  1. public class first_jsp extends..{ 
  2.    public void _jspService(-,-){ 
  3.       int t=30; 
  4.       out.println(“Square:”+(t*t)); 
  5.    } 
  6. }
The code placed in scriptlet can use implicit objects of jsp because implicit objects and the code placed scriptlet goes to _jspService(-,-) method of JES class

first.jsp
Browser s/w name:
  1. <%out.println(request.getHeader("user-agent");%>
first_jsp.java
  1. public class first_jsp extends { 
  2.     public void _jspService(req,res){ 
  3.        .... 
  4.     out.write("browser s/w name:"); 
  5.     out.println("\r\n"); 
  6.     out.println(request.getHeader("user-agent")); 
  7.    } 
  8. }
We can't place method definitions in scriptlet place because it becomes nested method of _jspService(-,-) method and java doesn't support nested methods

first.jsp
  1. <%public void m1(){ //error 
  2. ..... 
  3. }
first_jsp.java(JES class)
  1. public void _jspService(-,-) throws ServletException, IOException 
  2.    public void m1(){ 
  3.       ....//nested method(gives error) 
  4.     } 
  5. }
In java we can take 4 types of inner classes
a.)Inner class(class/interface level)
b.)Nested inner class(static inner class)
c.)Method level inner class
d.)Anonymous inner class
-->In java we can have only one type of inner interface(class/interface level)
-->We can place class definition in scriptlet because java supports method level inner class
E.g:
  1. <% class Test{ 
  2. ---- 
  3. }%>
In JES class
  1. public void_jspService(-,-){ 
  2. class Test{ 
  3. .... 
  4. }
We can't place interface definition in scriptlet
E.g:
  1. interface Demo{ 
  2. ...... 
  3. }%>
In JES class
  1. public void _jspService(-,-){ 
  2. interface Demo{ 
  3. .....(error) 
  4. }
Using XML Syntax
  1. <jsp:scriptlet> 
  2. int a=10; 
  3. int b=20; 
  4. out.println("sum is:"+(a+b)); 
  5. </jsp:scriptlet>
While working with XML syntax based scriptlet we need to take care of less than(<) sysmbol problem because it will not be taken as java's conditional operator. It will be taken as xml meaning based sub tag symbol

E.g(2)
  1. <jsp:scriptlet> 
  2.    int a=10; 
  3.    int b=20; 
  4.    if(a<b)// gives error 
  5.       out.println(b+" is big"); 
  6.    else 
  7.       out.println(a+" is big"); 
  8. </jsp:scriptlet>
Solution(1)( use standard syntax)
  1. <% 
  2.     .. 
  3.     .. 
  4. %>
It is like escaping from the problem

Solution (2)(use xml syntac with <![CDATA[..].])
  1. <jsp:scriptlet> 
  2. <![CDATA[-->it takes the body of the tag as normal text content and doesn't apply on any xml meaning 
  3. ... 
  4. ... 
  5. ]]> 
  6. </jsp:scriptlet>
We can place zero or more scriptlets in one jsp page having both xml, standard syntaxes

(Q)What is the difference between out.write() and out.print() methods ?

a.)out.write() can't write null values to browser because it throws NullPointerException

b.)out.print() can write null values to browser

c.)JES class uses out.write() to write template text content and out.print() method to write results of java expressions/objects

E.g:
  1. <% String s=null; 
  2.     out.write(s); //throws NullPointerException 
  3.     out.println(s); //shows null 
  4. %>

(2)Expression Tags

Evaluates the given expression and shows the generated output on to browser, it internally uses out.println() method to display results of the evaluated expression
Standard syntax
  1. <%= 
  2.     .... 
  3. %>
XML syntax
  1. <jsp:expression> 
  2. ..... 
  3. </jsp:expression>
a.)The expression are like variables, arithmetic, operations, logical operations methods calls that return values, Instantiation and etc...

b.)The code placed in expression tag goes to _jspService(-,-) method of jes class
E.g:
  1. <% int a=10;%> 
  2. Value=<%=a%> <br> 
  3. Square Value=<%=a*a%><br> 
  4. Cube Value=<%=a*a*a%>
first.jsp
  1. <% int a=10;%> 
  2. Value=<%=a%> <br> 
  3. Square Value=<%=a*a%><br> 
  4. Cube Value=<%=a*a*a%>
In JES class first_jsp
  1. public void _jspService(-,-)throws SE, IOE{ 
  2.      ... 
  3.    int a = 10; 
  4.    out.write("Value="); 
  5.    out.println(a); 
  6.    out.write("Square Value="); 
  7.    out.println(a*a); 
  8.   ... 
  9. }
If we use expression tag effectively, there is no need of out.println()

e.g:Expression Tag performing logical operation
  1. <% int a=10; 
  2. int b=20;%> 
  3. <%=a%>is less than<%=b%>?<%=a<b%>
The code placed in expression tags goes to _jspService(-,-) of jes class, so implicit objects are visible

In first.jsp 
  1. Browser name: <%=request.getHeader("user-agent")%>
In JES class
  1. public void _jspService(req,res){ 
  2.   out.write("browser name:"); 
  3.   out.println(request.getHeader("user-agent")); 
  4. }
We can use expression tag for instantiation and to display the default data of the object

In first.jsp 
  1. Date and Time: <%=new java.util.Date()%>
In JES class
  1. public void _jspService(req,res)throws SE, IOE{ 
  2.   out.write("Date and Time"); 
  3.   out.println(new java.util.Date()); 
  4. }
We can use expression tag to call methods which returns the value i.e, method return type must be other than void 
E.g:
  1. <%String s="teja";%> 
  2. <%=s%>length:<%=s.length()%><br> 
  3. <%=s%>in Upper case:<%=s.toUpperCase()%><br>
In JES class
  1. public void _jspService(req,res)throws SE, IOE{ 
  2.   String s="teja"; 
  3.   out.println(s); 
  4.   out.write("length:"); 
  5.   out.println(s.length()); 
  6. }
Using XML syntax
  1. <jsp:expression> 
  2.    new java.util.Date() 
  3. </jsp:expression> 
  4.    5<10? 
  5. <jsp:expression> 
  6.   <![CDATA[ 
  7.    5<10 
  8.     ]]> 
  9. </jsp:expression >
a.)While working with xml syntax based expression tag less than "<" symbol problem will rise and there is no solution for these problem even with CDATA
  1. <% int a=10;%> 
  2. Square, Cube values:<%=a*a+" "+a*a*a%> 
b.)We can place expression tag to evaluate only one expression at a time. To evaluate more than one expression using single expression tag we need to go for concatenation of expressions as shown above...

c.)
We can place zero or more expression tags having both standard , xml syntaxes

(3)Declaration Tag

The code placed in declaration tag goes outside of _jspService(-,-) method of JES class, so we can place following items in JES class

a.)Global variable declarations
b.)Use defined method definitions
c.)jspInit(), jspDestroy() method definitions
d.)JES class level inner class, inner interfaces

Standard Syntax
  1. <%!----%>
Xml Syntax
  1. <jsp:declaration> 
  2. ----- 
  3. </jsp:declaration>
E.g:Writing method definitions
first.jsp

  1. <%! public String findBig(int x, int y){ 
  2. if(x<y) 
  3. return y+ "is Big"; 
  4. else if(y<x) 
  5. return x+" is Big"; 
  6. else 
  7. return "both are equal"%> 
  8. Result:<%=findBig(10,20)%>
In JES class
  1. public class First_jsp extends{ 
  2.   public String findBig(int x, int y){ 
  3.     ...... 
  4.    } 
  5.  public void _jspService(-,-)throws SE, IOE{ 
  6.    out.write("Result"); 
  7.    out.println(findBig(10,20)); 
  8.   } 
  9. }
a.)The JES class name changes server to server/container to container 
b.)Declaration tag variables becomes Global variables in JES class where as scriptlet tag variable becomws local variables in jes class if both have got same name we can differentiate them in scriptlet either using this operator or using implicit object page as shown below 
  1. <%! int a=10;%> 
  2. <%! int b=20;%> 
  3. Global value: <%=this.a%><br> 
  4. Global value: <%=((first_jsp)page).a%><br> 
  5. Local Value:<%=a%>
Note: Working with implicit object page is not good practise because we need to typecast with jes class and jes class name changes server to server
  1. <jsp:declaration> 
  2. <%! public String findBig(int a, int b){ 
  3. if(a<b) 
  4. return b+ "is big"; 
  5. else if(b<a) 
  6. return a+" is big"; 
  7. else 
  8. return "both are equal"; 
  9. </jsp:declaration> 
  10. Result: <jsp:declaration>findBig(10,20)</jsp:declaration>
a.)While working xml syntax based declaration tag we get less than "<" symbol problem and we need to solve that problem by using CDATA
  1. <jsp:declaration> 
  2. public String findBig(int a, int b){ 
  3. <![CDATA[ 
  4. if(a<b) 
  5. .... 
  6. ]]> 
  7. </jsp:declaration> 
  8. Result: <jsp:declaration>findBig(10,20)</jsp:declaration>
b.)We can use declaration tag to place jspInit(), jspDestroy() method definitions
  1. <%! public void jspInit(){ 
  2.   System.out.println("jspInit()"); 
  3.    ....//initialization logic 
  4.    } 
  5.   public void jspDestroy(){ 
  6.     System.out.println("jspDestroy()"); 
  7.       ..//initialization logic 
  8.    } 
  9. %>
(Q)Can we place _jspInit(), _jspDestroy() & _jspService() methods in declaration tag ?
Not possible because they become duplicate methods in jes class

(Q) Can we place servlet life cycle methods in jsp declaration tags ?
a.)We can place only public service(-,-) method but not recommended because we will loose the opportunity to work with implicit objects, implicit exception handling and etc..., which are there in _jspService(-,-) method

b.)Servlets life cycle methods init(ServletConfig ), destroy() method definitions can't be placed in jsp declaration tags because they are given as final methods in the super class of jes class, in case of tomcat this is HttpJspBase

Procedure to JSP component based web application using Eclipse IDE:

a.)Configure Tomcat server with Eclipse IDE
b.)Create dynamic web project in Eclipse IDE (JspApp2)
c.)Add First.jsp to "web content" folder of project

JspApp2
Declaration tag

  1. <%!public String generateWishMsg(String name) 
  2.   int hour=0; 
  3.   String msg=null; 
  4.   java.util.calendar calender=null; 
  5. //get system date 
  6.    calender=java.util.calender.getInstance(); 
  7. //get hour(24hours) 
  8.   hour=calendar.get(java.util.calendar.HOUR_OF_DAY); 
  9. //generate wish message 
  10.  if(hour<=12) 
  11.    msg="Gud morning"+name; 
  12.  else if(hour<=16) 
  13.    msg="Gud afternoon"+name; 
  14.  else if(hour<=20) 
  15.    msg="Gud evening"+name 
  16.  else 
  17.    msg="Gud night"+name; 
  18.  return msg; 
  19. }%>
Template Text
  1. <h1>welcome</h1> 
  2. <hr>System Date::]<%=new java.util.Date()%>(expression tag) 
  3. (scriptlet)<%String name="teja";%><br>(template text) 
  4. Wish message(template text)::<%=generateWishMsg(name)%>(expresaion tag)
d.)Run the project

Comments

Popular posts from this blog

JSP Comments

Comments in JSP:- -->Compiler /Interpreter doesn't take commented code for compilation /Interpretation, so the commented code doesn't participate in execution JSP supports 3 types of comments:- a.)HTML comments /Template text comments/ Output comments Syn :- <!---text---> -->Recognized by html interpreter of browser b.)JSP comments /Hidden comments Syn :- <%--text--%> -->JSP page compiler recognize these comments c.)Java comments /Scripting comments Syn :- //-->For single line /*-- -----*/-->For multiple line -->Java compiler(java) recognize these comments -->In Eclipse IDE JES class for first.jsp comes in our(workspace)folder/.metadata/.plugins/.org eclipse Comments (1)JSP comment<%--%> (2)Java comment(// or /*--*/) (3)HTML comments(<!---> JES Source Code (1)No (2)Yes (3)Yes In JES compiled code (1)No (2)No (3)Yes In the code going to browser (1)No (2)No (3)Yes Output (1)No (2)No (3)No -->Jsp comments are not visible in any phase

Project Architecture's

1.) Functional flow/ Architecture a.) Only for 6+/7+years b.) Explain the flow of process/business 2.) Technical Flow/Architecture a.) Upto 5+ years b.) Explain technologies /components that are involved in the project Servlet & JSP Project Architecture:- 1.)What is need of Business Deligate to convert VO class object to DTO class object ? a.) If Business Deligate is not there servlet component directly prepares DTO class object by receiving and translating form data and passes to service class, if service class excepts certain input in other format like numeric employee number as string employee number then we need to modify servlet component code i.e For the changes that happened in business tier service component we have to modify servlet component of presentation tier b.) If Business Deligate is taken then it gets original form data as VO class object having string inputs and converts VO class object to DTO class object to pass to service class c.) If service class excepts