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

JSP declaration tag

A JSP declaration is used to declare variables and methods in a page’s scripting language.

The syntax for a declaration is as follows:
<%! scripting-language-declaration %>

Example for declaring Variable:
index.jsp
 <html>   
 <body>   
 <%! int a=5; %>   
 <%= "Value of Integer a is "+ (++a) %>   
 </body>   
 </html>   

Example for declaring method : 
index.jsp
 <HTML>  
 <BODY>  
 <%!  
   void getName()  
   {  
     System.out.println( "LoveJavaBytes" );  
   }  
 %>  
 Name is : <%= getName() %>  
 </BODY>  
 </HTML>  

JSP expression tag

A JSP expression is used to insert the value of a scripting language expression, converted into a string, into the data stream returned to the client using response object.

The syntax for an expression is as follows:
<%= scripting-language-expression %>

Example of JSP expression Tag :
On index.jsp, user enters name that send as request to user.jsp which prints username as output.

index.jsp
   <html>   
   <body>   
      <form action="user.jsp">   
        <input type="text" name="username">   
        <input type="submit" value="Submit"><br/>   
      </form>   
   </body>   
   </html>   

user.jsp
   <html>   
   <body>   
       <% ="Welcome! "+ request.getParameter("username") %>     
   </body>   
   </html>   

Scripting elements in JSP Pages

We can use Java Programming Language statements in JSP pages using scripting elements. There are three types of scripting elements used in JSP.

1. Scriplet Tag
2. Expression Tag
3. Declaration Tag

There are three ways to create and use objects in scripting elements:
  • Instance and class variables of the JSP page’s servlet class are created in declarations and accessed in scriptlets and expressions.
  • Local variables of the JSP page’s servlet class are created and used in scriptlets and expressions.
  • Attributes of scope objects are created and used in scriptlets and expressions.

JSP Scriptlets
A JSP scriptlet is used to contain any code fragment that is valid for the scripting language used in a page. The syntax for a scriptlet is as follows:
<%  scripting-language-statements or Java code  %>

Example of JSP scriplet Tag :
On index.jsp, user enters name that send as request to user.jsp which prints username as output.

index.jsp
   <html>   
   <body>   
      <form action="user.jsp">   
        <input type="text" name="username">   
        <input type="submit" value="Submit"><br/>   
      </form>   
   </body>   
   </html>   

user.jsp
   <html>   
   <body>   
       <%   
          String uname=request.getParameter("username");   
          out.print("Welcome! "+uname);   
        %>     
   </body>   
   </html>   

Create first JSP page in Eclipse IDE with Apache Tomcat Server

Following steps you need to follow to create your first JSP page in Eclipse IDE.

Step 1: Create Server by following steps :
Click on servers and then click on link to create a server.
 
Select Tomcat version in Apache folder which is installed in your system.

Browse the Tomcat Server folder and Click Finish.

Step 2: File > New > Dynamic Web Project

Give name HelloJSP then click Next > Next > Finish





Step 3: Create JSP File
WebContent > New > JSP File

Give name index.jsp then click Next > Finish

Write some code inside it.

Step 4: Deploy it over the Server
Right Click on Project Name > Run As > Run on Server

Click on Finish.

Output of your First JSP page on Web Browser.

JavaServer Pages (JSP) API Packages

JavaServer Pages API has the following packages :
  1. javax.servlet.jsp
  2. javax.servlet.jsp.tagext
Package javax.servlet.jsp 

Following are the interfaces include by javax.servlet.jsp package.

1. HttpJspPage : The HttpJspPage interface describes the interaction that a JSP Page Implementation Class must satisfy when using the HTTP protocol.

Method of HttpJspPage interface: (a) public void _jspService()

(a) public void _jspService() : The _jspService()method corresponds to the body of the JSP page. This method is defined automatically by the JSP container and should never be defined by the JSP page author. _ (underscore) stands that this method can not be override.

2. JspPage : The JspPage interface describes the generic interaction that a JSP Page Implementation class must satisfy; pages that use the HTTP protocol are described by the HttpJspPage interface.

Methods of JspPage interface: (a) public void jspInit () (b) public void jspDestroy()

(a) public void jspInit () : The jspInit() method is invoked when the JSP page is initialized. It is the responsibility of the JSP implementation that at this point invocations to the getServletConfig() method will return the desired value. A JSP page can override this method by including a definition for it in a declaration element. A JSP page should redefine the init() method from Servlet. 

(b) public void jspDestroy() : The jspDestroy() method is invoked when the JSP page is about to be destroyed. A JSP page can override this method by including a definition for it in a declaration element. A JSP page should redefine the destroy() method from Servlet.

Following are the classes include by javax.servlet.jsp package.

1. JspEngineInfo : The JspEngineInfo is an abstract class that provides information on the current JSP engine.

2. JspFactory : The JspFactory is an abstract class that defines a number of factory methods available to a JSP page at runtime for the purposes of creating instances of various interfaces and classes used to support the JSP implementation.

3. JspWriter : The actions and template data in a JSP page is written using the JspWriter object that is referenced by the implicit variable out which is initialized automatically using methods in the PageContext object.

4. PageContext : A PageContext instance provides access to all the namespaces associated with a JSP page, provides access to several page attributes, as well as a layer above the implementation details.

Following are the exceptions include by javax.servlet.jsp package.

1. JspException : A generic exception known to the JSP engine; uncaught JspExceptions will result in an invocation of the errorpage machinery.

2. JspTagException : Exception to be used by a Tag Handler to indicate some unrecoverable error.

 

Package javax.servlet.jsp.tagext 

Following are the interfaces include by javax.servlet.jsp.tagext package.

1. BodyTag : The BodyTag interface extends IterationTag by defining additional methods that let a tag handler manipulate the content of evaluating its body.

2. IterationTag : The IterationTag interface extends Tag by defining one additional method that controls the reevaluation of its body.

3. Tag : The interface of a simple tag handler that does not want to manipulate its body.

4. TryCatchFinally : The auxiliary interface of a Tag, IterationTag or BodyTag tag handler that wants additional hooks for managing resources.

Following are the classes include by javax.servlet.jsp.tagext package.

1. BodyContent : An encapsulation of the evaluation of the body of an action so it is available to a tag handler.

2. BodyTagSupport : A base class for defining tag handlers implementing BodyTag.

3. PageData : Translation-time information on a JSP page.

4. TagAttributeInfo : Information on the attributes of a Tag, available at translation time.

5. TagData : The (translation-time only) attribute/value information for a tag instance.

6. TagExtraInfo : Optional class provided by the tag library author to describe additional translation-time information not described in the TLD.

7. TagInfo : Tag information for a tag in a Tag Library; This class is instantiated from the Tag Library Descriptor file (TLD) and is available only at translation time.

8. TagLibraryInfo : Translation-time information associated with a taglib directive, and its underlying TLD file.

9. TagLibraryValidator : Translation-time validator class for a JSP page.

10. TagSupport : A base class for defining new tag handlers implementing Tag.

11. TagVariableInfo : Variable information for a tag in a Tag Library; This class is instantiated from the Tag Library Descriptor file (TLD) and is available only at translation time.

12. ValidationMessage : A validation message from a TagLibraryValidator.

13. VariableInfo : Information on the scripting variables that are created/modified by a tag (at run-time).

Life cycle of JavaServer Pages (JSP)

A JSP page services requests as a servlet. Thus, the life cycle and many of the capabilities of JSP pages (in particular the dynamic aspects) are determined by Java Servlet technology.

When a request is mapped to a JSP page, the web container first checks whether the JSP page’s servlet is older than the JSP page. If the servlet is older, the web container translates the JSP page into a servlet class and compiles the class. During development, one of the advantages of JSP pages over servlets is that the build process is performed automatically.

The steps in the life cycle of jsp page are:
 
1. Translation of JSP page into Servlet
2. Compilation of Translated JSP pages (which creates Class file)
3. Loading of Class file
4. Instantiation the servlet class
5. Initialization by invoking jspInit() method (To initialize the instance of servlet class)
6. RequestProcessing by invoking _jspService() method
7. Destruction by invoking jspDestroy() methods (To destroy the instance of servlet class)

Introduction to JavaServer Pages (JSP)

JSP stands for JavaServer Pages released in 1999 by Sun Microsystems. It is a technology to create dynamic web pages same as Servlet but it is extension to Servlet technology because it has more features than Servlet like HTML tags, JSP tags, JSTL, Expression Language, Custom tags etc.

Previous to JSP, we have to use Servlet to create dynamic web application but it consists presentation logic and business logic on the same page and JSP made it easier by separating designing and development code so JSP is much easier to maintain and use comparison to Servlet.

Lets understand JSP with a simple example :

jspFirst.jsp
JSP page uses .jsp as an extension like .html for html page. In the example for static code html tags are being used and for dynamic code scriplet tag <% %> is being used which will be discussed in the upcoming tutorials.
 <html>   
 <body>   
   <% out.print(“My first JSP page”); %>   
 </body>   
 </html>   
This code will give “My first JSP page” as an output on web browser.


Advantages of JSP :
1. JSP provides implicit objects, Action tags, JSTL, expression language, custom tags which helps programmer to write less code than Servlet.
2. JSP separates Presentation and development code which makes it easy to read, maintain and use.
3. JSP can use all the addition features with all the servlet features.