JSP Tags/ Elements
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
- <% int a=10;
- out.println("square:"+(a*a));%>
- public class first_jsp extends..{
- public void _jspService(-,-){
- int t=30;
- out.println(“Square:”+(t*t));
- }
- }
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:
Browser s/w name:
- <%out.println(request.getHeader("user-agent");%>
- public class first_jsp extends {
- public void _jspService(req,res){
- ....
- out.write("browser s/w name:");
- out.println("\r\n");
- out.println(request.getHeader("user-agent"));
- }
- }
first.jsp
a.)Inner class(class/interface level)
- <%public void m1(){ //error
- .....
- }
- public void _jspService(-,-) throws ServletException, IOException
- {
- public void m1(){
- ....//nested method(gives error)
- }
- }
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
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:
- <% class Test{
- ----
- }%>
- public void_jspService(-,-){
- class Test{
- ....
- }
- }
E.g:
- interface Demo{
- ......
- }%>
In JES class
- public void _jspService(-,-){
- interface Demo{
- .....(error)
- }
- }
- <jsp:scriptlet>
- int a=10;
- int b=20;
- out.println("sum is:"+(a+b));
- </jsp:scriptlet>
E.g(2)
- <jsp:scriptlet>
- int a=10;
- int b=20;
- if(a<b)// gives error
- out.println(b+" is big");
- else
- out.println(a+" is big");
- </jsp:scriptlet>
- <%
- ..
- ..
- %>
Solution (2)(use xml syntac with <![CDATA[..].])
- <jsp:scriptlet>
- <![CDATA[-->it takes the body of the tag as normal text content and doesn't apply on any xml meaning
- ...
- ...
- ]]>
- </jsp:scriptlet>
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:
- <% String s=null;
- out.write(s); //throws NullPointerException
- out.println(s); //shows null
- %>
(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 expressionStandard syntax
b.)The code placed in expression tag goes to _jspService(-,-) method of jes class
- <%=
- ....
- %>
- <jsp:expression>
- .....
- </jsp:expression>
b.)The code placed in expression tag goes to _jspService(-,-) method of jes class
E.g:
- <% int a=10;%>
- Value=<%=a%> <br>
- Square Value=<%=a*a%><br>
- Cube Value=<%=a*a*a%>
- <% int a=10;%>
- Value=<%=a%> <br>
- Square Value=<%=a*a%><br>
- Cube Value=<%=a*a*a%>
- public void _jspService(-,-)throws SE, IOE{
- ...
- int a = 10;
- out.write("Value=");
- out.println(a);
- out.write("Square Value=");
- out.println(a*a);
- ...
- }
- <% int a=10;
- int b=20;%>
- <%=a%>is less than<%=b%>?<%=a<b%>
The code placed in expression tags goes to _jspService(-,-) of jes class, so implicit objects are visible
- Browser name: <%=request.getHeader("user-agent")%>
- public void _jspService(req,res){
- out.write("browser name:");
- out.println(request.getHeader("user-agent"));
- }
- Date and Time: <%=new java.util.Date()%>
- public void _jspService(req,res)throws SE, IOE{
- out.write("Date and Time");
- out.println(new java.util.Date());
- }
E.g:
c.)We can place zero or more expression tags having both standard , xml syntaxes
a.)Global variable declarations
b.)Use defined method definitions
c.)jspInit(), jspDestroy() method definitions
d.)JES class level inner class, inner interfaces
Standard Syntax
- <%String s="teja";%>
- <%=s%>length:<%=s.length()%><br>
- <%=s%>in Upper case:<%=s.toUpperCase()%><br>
- public void _jspService(req,res)throws SE, IOE{
- String s="teja";
- out.println(s);
- out.write("length:");
- out.println(s.length());
- }
- <jsp:expression>
- new java.util.Date()
- </jsp:expression>
- 5<10?
- <jsp:expression>
- <![CDATA[
- 5<10
- ]]>
- </jsp:expression >
- <% int a=10;%>
- Square, Cube values:<%=a*a+" "+a*a*a%>
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 classa.)Global variable declarations
b.)Use defined method definitions
c.)jspInit(), jspDestroy() method definitions
d.)JES class level inner class, inner interfaces
- <%!----%>
- <jsp:declaration>
- -----
- </jsp:declaration>
first.jsp
- <%! public String findBig(int x, int y){
- if(x<y)
- return y+ "is Big";
- else if(y<x)
- return x+" is Big";
- else
- return "both are equal"%>
- Result:<%=findBig(10,20)%>
- public class First_jsp extends{
- public String findBig(int x, int y){
- ......
- }
- public void _jspService(-,-)throws SE, IOE{
- out.write("Result");
- out.println(findBig(10,20));
- }
- }
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
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
b.)Create dynamic web project in Eclipse IDE (JspApp2)
c.)Add First.jsp to "web content" folder of project
JspApp2
Declaration tag
- <%! int a=10;%>
- <%! int b=20;%>
- Global value: <%=this.a%><br>
- Global value: <%=((first_jsp)page).a%><br>
- Local Value:<%=a%>
- <jsp:declaration>
- <%! public String findBig(int a, int b){
- if(a<b)
- return b+ "is big";
- else if(b<a)
- return a+" is big";
- else
- return "both are equal";
- }
- </jsp:declaration>
- Result: <jsp:declaration>findBig(10,20)</jsp:declaration>
- <jsp:declaration>
- public String findBig(int a, int b){
- <![CDATA[
- if(a<b)
- ....
- ]]>
- }
- </jsp:declaration>
- Result: <jsp:declaration>findBig(10,20)</jsp:declaration>
- <%! public void jspInit(){
- System.out.println("jspInit()");
- ....//initialization logic
- }
- public void jspDestroy(){
- System.out.println("jspDestroy()");
- ..//initialization logic
- }
- %>
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 (Q) Can we place servlet life cycle methods in jsp declaration tags ?
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 IDEb.)Create dynamic web project in Eclipse IDE (JspApp2)
c.)Add First.jsp to "web content" folder of project
Declaration tag
- <%!public String generateWishMsg(String name)
- {
- int hour=0;
- String msg=null;
- java.util.calendar calender=null;
- //get system date
- calender=java.util.calender.getInstance();
- //get hour(24hours)
- hour=calendar.get(java.util.calendar.HOUR_OF_DAY);
- //generate wish message
- if(hour<=12)
- msg="Gud morning"+name;
- else if(hour<=16)
- msg="Gud afternoon"+name;
- else if(hour<=20)
- msg="Gud evening"+name
- else
- msg="Gud night"+name;
- return msg;
- }%>
- <h1>welcome</h1>
- <hr>System Date::]<%=new java.util.Date()%>(expression tag)
- (scriptlet)<%String name="teja";%><br>(template text)
- Wish message(template text)::<%=generateWishMsg(name)%>(expresaion tag)
Comments
Post a Comment