Showing posts with label JSP. Show all posts
Showing posts with label JSP. Show all posts

Wednesday, 21 February 2018

JSP Scriptlet tag

DashZin
In JSP, java code can be written inside the jsp page using the scriptlet tag. Let's see what are the scripting elements first.

JSP Scripting elements

The scripting elements provides the ability to insert java code inside the jsp. There are three types of scripting elements:

  1.      scriptlet tag
  1.     expression tag
  1.      declaration tag

JSP scriptlet tag

A scriptlet tag is used to execute java source code in JSP

Syntax:

       <%  java source code %>  

Example of JSP scriptlet tag

In this example, we are displaying a welcome message.
         <html>  
         <body>  
          <% out.print("welcome to jsp"); %>  
          </body>  
          </html>  

Example of JSP scriptlet tag 

In this example, we have created two files index.html and welcome.jsp. The index.html file gets the username from the user and the welcome.jsp file prints the username with the welcome message.

index.html
      <html>    
       <body>  
       <form action="welcome.jsp">  
       <input type="text" name="uname">  
       <input type="submit" value="go"><br/>  
      </form>  
      </body>  
      </html>  

welcome.jsp
     <html>  
     <body>  
      <%  
      String name=request.getParameter("uname");  
       out.print("welcome "+name);  
       %>  
       </form>  
       </body>  
       </html>  






JSP Expression Tag – JSP Tutorial

DashZin
The code placed within JSP expression tag is written to the output stream of the response. So you need not write out.print() to write data. It is mainly used to print the values of variable or method.

Synatx : 
                       <%=  statement %> 

JSP Expression Tag Example

Example 1: Expression of values
Here we are simply passing the expression of values inside expression tag.
 <html>
  <head>
   <title>JSP expression tag example1</title>
 </head>
 <body>
   <%= 2+4*10 %>
 </body>
 </html>

Example 2: Expression that print user name
In this Example, we are going to print Username using Expression tag
         <html>
         <body>
        <form action="welcome.jsp">
         <input type="text" name="uname"><br/>
          <input type="submit" value="go">
        </form>
        </body>
       </html>






JSP Declaration tag – JSP Tutorial

DashZin
Declaration tag is a block of java code for declaring class wide variables, methods and classes. Whatever placed inside these tags gets initialized during JSP initialization phase and has class scope. JSP container keeps this code outside of the service method (_jspService()) to make them class level variables and methods.

Syntax of declaration tag:
              <%!  field or method declaration %>     
Difference between JSP Scriptlet tag and Declaration tag
Jsp Scriptlet Tag
Jsp Declaration Tag
The jsp scriptlet tag can only declare variables not methods.
The jsp declaration tag can declare variables as well as methods.
The declaration of scriptlet tag is placed inside the _jspService() method.
The declaration of jsp declaration tag is placed outside the _jspService() method.

Example of JSP declaration tag that declares field

In this example of JSP declaration tag, we are declaring the field and printing the value of the declared field using the jsp expression tag.
    
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Declaration tag</title>
</head>
<body>

<!--declaration of name variable....  -->
<%! String name="DashZin"%>
<%="Welcome to : "+name %>
</body>
</html>

Example of JSP declaration tag that declares method

In this example of JSP declaration tag, we are defining the method which returns the cube of given number and calling this method from the jsp expression tag. But we can also use jsp scriptlet tag to call the declared method.

       <html> 
       <body>
        <%!
        int cube(int n){
        return n*n*n*;
           }
          %>
         <%= "Cube of 3 is:"+cube(3) %>
        </body>
       </html>










Friday, 2 February 2018

Custom tag Example

DashZin
For creating any custom tag, we need to follow following steps:
ü  Create the Tag handler class and perform action at the start or at the end of the tag.
ü  Create the Tag Library Descriptor (TLD) file and define tags
ü  Create the JSP file that uses the Custom tag defined in the TLD file

Example to understand the custom tag

Create the Tag handler class
  • To create the Tag Handler, we are inheriting the TagSupport class and overriding its method doStartTag().To write data for the jsp, we need to use the JspWriter class.
  • The PageContext class provides getOut() method that returns the instance of JspWriter class. TagSupport class provides instance of pageContext bydefault.

File: DashzinTagHandler.java

package com.dashzin.jsp;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.jasper.el.JspELException;

public class DashzinTagHandler extends TagSupport{
     public int doStartTag() throws JspELException{
          JspWriter out=pageContext.getOut();
          try{
              out.print("welcome to custome tag");
          }catch(Exception e){
              e.printStackTrace();
          }
         
          return SKIP_BODY; //will not evaluate the body content of the tag ;
     }

}

Create the TLD file
Tag Library Descriptor (TLD) file contains information of tag and Tag Hander classes. It must be contained inside the WEB-INFdirectory.

File: Dashzintags.tld

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<!DOCTYPE taglib 
        PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" 
    "http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd"> 
  <taglib>
  <tlib-version>1.0</tlib-version> 
  <jsp-version>1.2</jsp-version> 
  <short-name>simple</short-name> 
  <uri>http://tomcat.apache.org/example-taglib</uri> 
  <tag>
  <name>cus</name> 
  <tag-class>com.dashzin.jsp.DashzinTagHandler</tag-class> 
  </tag>
  </taglib>


Create the JSP file
  • Let's use the tag in our jsp file. Here, we are specifying the path of tld file directly. But it is recommended to use the uri name instead of full path of tld file. We will learn about uri later.
  • It uses taglib directive to use the tags defined in the tld file.

File: index.jsp

<%@ taglib uri="WEB-INF/Dashzintags.tld" prefix="m" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Check custome tag in jsp</title>
</head>
<body>

Try to print welcome massage: <m:cus/>

</body>
</html>

Output:-

Try to print welcome massage: welcome to custome tag