Ads

LightBlog

Latest

Saturday, April 17, 2021

JSP

 JSP


--> JSP technology is used to create web application just like Servlet technology. It can be thought of as an extension to Servlet because it provides more functionality than servlet such as expression language, JSTL, etc. 



--> A JSP page consists of HTML tags and JSP tags. The JSP pages are easier to maintain than Servlet because we can separate designing and development. It provides some additional features such as Expression Language, Custom Tags, etc.







Advantages of JSP over Servlet 

There are many advantages of JSP over the Servlet. They are as follows: 

1) Extension to Servlet 

JSP technology is the extension to Servlet technology. We can use all the features of the Servlet in JSP. In addition to, we can use implicit objects, predefined tags, expression language and Custom tags in JSP, that makes JSP development easy. 

2) Easy to maintain 

JSP can be easily managed because we can easily separate our business logic with presentation logic. In Servlet technology, we mix our business logic with the presentation logic. 

3) Fast Development: 

No need to recompile and redeploy 
If the JSP page is modified, we don't need to recompile and redeploy the project. The Servlet code needs to be updated and recompiled if we have to change the look and feel of the application. 

4) Less code than Servlet 

In JSP, we can use many tags such as action tags, JSTL, custom tags, etc. that reduce the code. Moreover, we can use EL, implicit objects, etc. 

The Lifecycle of a JSP Page 

The JSP pages follow these phases: 

o Translation of JSP Page o Compilation of JSP Page 
o Classloading (the classloader loads class file) 
o Instantiation (Object of the Generated Servlet is created). 
o Initialization ( the container invokes jspInit() method). 
o Request processing ( the container invokes jspService()method). 
o Destroy ( the container invokes jspDestroy() method). 



Creating a simple JSP Page 

To create the first JSP page, write some HTML code as given below, and save it by .jsp extension. We have saved this file as index.jsp. Put it in a folder and paste the folder in the web-apps directory in apache tomcat to run the JSP page. 

index.jsp 

Let's see the simple example of JSP where we are using the scriptlet tag to put Java code in the JSP page. We will learn scriptlet tag later.

1. <html>  
2. <body> 
3. <% out.print(2*5); %>  
4. </body>   
5. </html>   

It will print 10 on the browser. 

How to run a simple JSP Page? 

Follow the following steps to execute this JSP page: 


o Start the server 
o Put the JSP file in a folder and deploy on the server 
o Visit the browser by the URL http://localhost:portno/contextRoot/jspfile, for example, http://localhost:8888/myapplication/index.jsp 




Do I need to follow the directory structure to run a simple JSP?

No, there is no need of directory structure if you don't have class files or TLD files. For example, put JSP files in a folder directly and deploy that folder. It will be running fine. However, if you are using Bean class, Servlet or TLD file, the directory structure is required.
 

The Directory structure of JSP

The directory structure of JSP page is same as Servlet. We contain the JSP page outside the WEB-INF folder or in any directory.



The JSP API

The JSP API consists of two packages:
1.    javax.servlet.jsp
2.    javax.servlet.jsp.tagext

javax.servlet.jsp package

The javax.servlet.jsp package has two interfaces and classes.The two interfaces are as follows:
1.    JspPage
2.    HttpJspPage
The classes are as follows:
  • JspWriter 
  • PageContext 
  •  JspFactory  
  • JspEngineInfo 
  •  JspException 
  •  JspError 

The JspPage interface

According to the JSP specification, all the generated servlet classes must implement the JspPage interface. It extends the Servlet interface. It provides two life cycle methods. 


Methods of JspPage interface

1.    public void jspInit(): It is invoked only once during the life cycle of the JSP when JSP page is requested firstly. It is used to perform initialization. It is same as the init() method of Servlet interface.
2.    public void jspDestroy(): It is invoked only once during the life cycle of the JSP before the JSP page is destroyed. It can be used to perform some clean up operation. 

The HttpJspPage interface 

The HttpJspPage interface provides the one life cycle method of JSP. It extends the JspPage interface. 
Method of HttpJspPage interface:
 1. public void _jspService(): It is invoked each time when request for the JSP page comes to the container. It is used to process the request. The underscore _ signifies that you cannot override this method. 

JSP Scriptlet tag (Scripting elements) 
1. Scripting elements 
2. JSP scriptlet tag 
3. Simple Example of JSP scriptlet tag 
4. Example of JSP scriptlet tag that prints the user name 
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: 
    scriptlet tag  
expression tag 
declaration tag 

JSP scriptlet tag 
A scriptlet tag is used to execute java source code in JSP. Syntax is as follows: 
1. <%  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 that prints the user name 

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. 
File: index.html 
<html>   
<body>   
<form action="welcome.jsp">   
<input type="text" name="uname">   
<input type="submit" value="go"><br/>   </form>   
</body>   
</html>   
File: welcome.jsp 
<html>   
<body>   
<%   Stringname=request.getParameter("uname");   out.print("welcome "+name);   
%>   
</form>  
 </body>  
 </html>   

JSP expression tag 

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. 
Syntax of JSP expression tag 
1. <%=  statement %>   
Example of JSP expression tag 
In this example of jsp expression tag, we are simply displaying a welcome message. 
1. <html>   
2. <body>   
3. <%= "welcome to jsp" %>   
4. </body>   
5. </html>   
Note: Do not end your statement with semicolon in case of expression tag. 
Example of JSP expression tag that prints current time 
To display the current time, we have used the getTime() method of Calendar class. The getTime() is an instance method of Calendar class, so we have called it after getting the instance of Calendar class by the getInstance() method. 
index.jsp 
1. <html>   
2. <body>   
3. Current Time: 
<%= java.util.Calendar.getInstance().getTime() %>   
4. </body>   
5. </html>   
Example of JSP expression tag that prints the user name 
In this example, we are printing the username using the expression tag. The index.html file gets the username and sends the request to the welcome.jsp file, which displays the username. 
File: index.jsp 
<html>   
<body>   
<form action="welcome.jsp">   
<input type="text" name="uname"><br/>   <input type="submit" value="go">   
</form>   
</body>   
</html>   
File: welcome.jsp 
<html>   <body>   <%= "Welcome "+request.getParameter("uname") %>   </body>   </html>   

JSP Declaration Tag 

The JSP declaration tag is used to declare fields and methods. 
The code written inside the jsp declaration tag is placed outside the service() method of auto generated servlet. 
So it doesn't get memory at each request. 
Syntax of JSP declaration tag 
The syntax of the declaration tag is as follows: 
1. <%!  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.
index.jsp
<html>  
<body>  
<%! int data=50; %>  
<%= "Value of the variable is:"+data %>  
</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. index.jsp

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


JSP Implicit Objects

There are 9 jsp implicit objects. These objects are created by the web container that are available to all the jsp pages.
The available implicit objects are out, request, config, session, application etc.
A list of the 9 implicit objects is given below:

Object
Type


out
JspWriter
request
HttpServletRequest
response
HttpServletResponse
config
ServletConfig
application
ServletContext
session
HttpSession
pageContext
PageContext
page
Object
exception
Throwable


JSP directives


The jsp directives are messages that tells the web container how to translate a JSP page into the corresponding servlet.
There are three types of directives:
o page directive 
o include directive 
o taglib directive 

Syntax of JSP Directive
<%@ directive attribute="value" %>   






No comments:

Post a Comment