Tuesday, 29 March 2016

JSP ACTION TAGS

There are many JSP action tags or elements. Each JSP action tag is used to perform some specific tasks.
The action tags are used to control the flow between pages and to use Java Bean. The Jsp action tags are given below.
JSP Action Tags
Description
jsp:forward
forwards the request and response to another resource.
jsp:include
includes another resource.
jsp:useBean
creates or locates bean object.
jsp:setProperty
sets the value of property in bean object.
jsp:getProperty
prints the value of property of the bean.
jsp:plugin
embeds another components such as applet.
jsp:param
sets the parameter value. It is used in forward and include mostly.
jsp:fallback
can be used to print the message if plugin is working. It is used in jsp:plugin.

The jsp:useBean, jsp:setProperty and jsp:getProperty tags are used for bean development. So we will see these tags in bean developement.


jsp:forward action tag

The jsp:forward action tag is used to forward the request to another resource it may be jsp, html or another resource.

Syntax of jsp:forward action tag without parameter


  1. <jsp:forward page="relativeURL | <%= expression %>" />  

Syntax of jsp:forward action tag with parameter


  1. <jsp:forward page="relativeURL | <%= expression %>">  
  2. <jsp:param name="parametername" value="parametervalue | <%=expression%>" />  
  3. </jsp:forward>  


Example of jsp:forward action tag without parameter

In this example, we are simply forwarding the request to the printdate.jsp file.

index.jsp


  1. <html>  
  2. <body>  
  3. <h2>this is index page</h2>  
  4.   
  5. <jsp:forward page="printdate.jsp" />  
  6. </body>  
  7. </html>  

printdate.jsp


  1. <html>  
  2. <body>  
  3. <% out.print("Today is:"+java.util.Calendar.getInstance().getTime()); %>  
  4. </body>  
  5. </html>  


Example of jsp:forward action tag with parameter

In this example, we are forwarding the request to the printdate.jsp file with parameter and printdate.jsp file prints the parameter value with date and time.

index.jsp


  1. <html>  
  2. <body>  
  3. <h2>this is index page</h2>  
  4.   
  5. <jsp:forward page="printdate.jsp" >  
  6. <jsp:param name="name" value="javatpoint.com" />  
  7. </jsp:forward>  
  8.   
  9. </body>  
  10. </html>  

printdate.jsp


  1. <html>  
  2. <body>  
  3.   
  4. <% out.print("Today is:"+java.util.Calendar.getInstance().getTime()); %>  
  5. <%= request.getParameter("name") %>  
  6.   
  7. </body>  
  8. </html>  

jsp:include action tag

The jsp:include action tag is used to include the content of another resource it may be jsp, html or servlet.
The jsp include action tag includes the resource at request time so it is better for dynamic pages because there might be changes in future.
The jsp:include tag can be used to include static as well as dynamic pages.

Advantage of jsp:include action tag

Code reusability : We can use a page many times such as including header and footer pages in all pages. So it saves a lot of time.

Difference between jsp include directive and include action

JSP include directive
JSP include action
includes resource at translation time.
includes resource at request time.
better for static pages.
better for dynamic pages.
includes the original content in the generated servlet.
calls the include method.


Syntax of jsp:include action tag without parameter


  1. <jsp:include page="relativeURL | <%= expression %>" />  

Syntax of jsp:include action tag with parameter


  1. <jsp:include page="relativeURL | <%= expression %>">  
  2. <jsp:param name="parametername" value="parametervalue | <%=expression%>" />  
  3. </jsp:include>  


Example of jsp:include action tag without parameter

In this example, index.jsp file includes the content of the printdate.jsp file.
File: index.jsp

  1. <h2>this is index page</h2>  
  2.   
  3. <jsp:include page="printdate.jsp" />  
  4.   
  5. <h2>end section of index page</h2>  
File: printdate.jsp

  1. <% out.print("Today is:"+java.util.Calendar.getInstance().getTime()); %> 

Java Bean

A Java Bean is a java class that should follow following conventions:
  • It should have a no-arg constructor.
  • It should be Serializable.
  • It should provide methods to set and get the values of the properties, known as getter and setter methods.

Why use Java Bean?

According to Java white paper, it is a reusable software component. A bean encapsulates many objects into one object, so we can access this object from multiple places. Moreover, it provides the easy maintenance.

Simple example of java bean class


  1. //Employee.java  
  2.   
  3. package mypack;  
  4. public class Employee implements java.io.Serializable{  
  5. private int id;  
  6. private String name;  
  7.   
  8. public Employee(){}  
  9.   
  10. public void setId(int id){this.id=id;}  
  11.   
  12. public int getId(){return id;}  
  13.   
  14. public void setName(String name){this.name=name;}  
  15.   
  16. public String getName(){return name;}  
  17.   
  18. }  

How to access the java bean class?

To access the java bean class, we should use getter and setter methods.

  1. package mypack;  
  2. public class Test{  
  3. public static void main(String args[]){  
  4.   
  5. Employee e=new Employee();//object is created  
  6.   
  7. e.setName("Arjun");//setting value to the object  
  8.   
  9. System.out.println(e.getName());  
  10.   
  11. }}  

Monday, 28 March 2016

EXCEPTION HANDLING IN JSP

The exception is normally an object that is thrown at runtime. Exception Handling is the process to handle the runtime errors. There may occur exception any time in your web application. So handling exceptions is a safer side for the web developer. In JSP, there are two ways to perform exception handling:
  1. By errorPage and isErrorPage attributes of page directive
  2. By <error-page> element in web.xml file


Example of exception handling in jsp by the elements of page directive

In this case, you must define and create a page to handle the exceptions, as in the error.jsp page. The pages where may occur exception, define the errorPage attribute of page directive, as in the process.jsp page.
There are 3 files:
  • index.jsp for input values
  • process.jsp for dividing the two numbers and displaying the result
  • error.jsp for handling the exception

index.jsp


  1. <form action="process.jsp">  
  2. No1:<input type="text" name="n1" /><br/><br/>  
  3. No1:<input type="text" name="n2" /><br/><br/>  
  4. <input type="submit" value="divide"/>  
  5. </form>  

process.jsp


  1. <%@ page errorPage="error.jsp" %>  
  2. <%  
  3.   
  4. String num1=request.getParameter("n1");  
  5. String num2=request.getParameter("n2");  
  6.   
  7. int a=Integer.parseInt(num1);  
  8. int b=Integer.parseInt(num2);  
  9. int c=a/b;  
  10. out.print("division of numbers is: "+c);  
  11.   
  12. %>  

error.jsp


  1. <%@ page isErrorPage="true" %>  
  2.   
  3. <h3>Sorry an exception occured!</h3>  
  4.   
  5. Exception is: <%= exception %>  

Output of this example:

exception handling in jsp exception handling in jsp exception handling in jsp 

Expression Language (EL) in JSP

The Expression Language (EL) simplifies the accessibility of data stored in the Java Bean component, and other objects like request, session, application etc.
There are many implicit objects, operators and reserve words in EL.
It is the newly added feature in JSP technology version 2.0.

Syntax for Expression Language (EL)


  1. ${ expression }  

Implicit Objects in Expression Language (EL)

There are many implicit objects in the Expression Language. They are as follows:
Implicit Objects
Usage
pageScope
it maps the given attribute name with the value set in the page scope
requestScope
it maps the given attribute name with the value set in the request scope
sessionScope
it maps the given attribute name with the value set in the session scope
applicationScope
it maps the given attribute name with the value set in the application scope
param
it maps the request parameter to the single value
paramValues
it maps the request parameter to an array of values
header
it maps the request header name to the single value
headerValues
it maps the request header name to an array of values
cookie
it maps the given cookie name to the cookie value
initParam
it maps the initialization parameter
pageContext
it provides access to many objects request, session etc.

Simple example of Expression Language that prints the name of the user

In this example, we have created two files index.jsp and process.jsp. The index.jsp file gets input from the user and sends the request to the process.jsp which in turn prints the name of the user using EL.

index.jsp


  1. <form action="process.jsp">  
  2. Enter Name:<input type="text" name="name" /><br/><br/>  
  3. <input type="submit" value="go"/>  
  4. </form>  

process.jsp


  1. Welcome, ${ param.name }  


Example of Expression Language that prints the value set in the session scope

In this example, we printing the data stored in the session scope using EL. For this purpose, we have used sessionScope object.

index.jsp


  1. <h3>welcome to index page</h3>  
  2. <%  
  3. session.setAttribute("user","sonoo");  
  4. %>  
  5.   
  6. <a href="process.jsp">visit</a>  

process.jsp


  1. Value is ${ sessionScope.user }  


Precedence of Operators in EL

There are many operators that have been provided in the Expression Language. Their precedence are as follows:
[] .
()
-(unary) not ! empty
* / div % mod
+ - (binary)
< <= > >= lt le gt ge
== != eq ne
&& and
|| or
?:

Reserve words in EL

There are many reserve words in the Expression Language. They are as follows:
lt
le
gt
ge
eq
ne
true
false
and
or
not
instanceof
div
mod
empty
null