JSP Directives

JSP Directives:

jsp mandates are messages that advises the web container how to make an interpretation of a JSP page into the relating servlet.

  • page directive
  • include directive
  • taglib directive
Syntax of Directive :
<%@ directive attribute=”value” %>
JSP page directive:

 Attributes that performed to an entire JSP page.

Syntax :
<%@ page attribute=”value” %>
Attributes of JSP page directive
  • import
  • contentType
  • extends
  • info
  • buffer
  • language
  • isELIgnored
  • isThreadSafe
  • autoFlush
  • session
  • pageEncoding
  • errorPage
  • isErrorPage

1)import:

The import ascribe is utilized to import class, interface or every one of the individuals from a package.It is like import watchword in java class or interface.

Case of import property :

<html>

<body>

<%@ page import=”java.util.Date” %>

Today is: <%= new Date() %>

</body>

</html>

2)contentType

The contentType property characterizes the MIME(Multipurpose Internet Mail Extension) kind of the HTTP response. The default esteem is “content/html;charset=ISO-8859-1”.

Case of contentType property :

<html>

<body>

<%@ page contentType=application/msword %>

Today is: <%= new java.util.Date() %>

</body>

</html>

3)extends :

The broadens quality characterizes the parent class that will be acquired by the created servlet. It is once in a while utilized.

JAVA Tutorial Video

4)info :

This property just sets the data of the JSP page which is recovered later by utilizing getServletInfo() strategy for Servlet interface.

Case of information trait :

<html>

<body>

<%@ page info=”composed by Sonoo Jaiswal” %>

Today is: <%= new java.util.Date() %>

</body>

</html>

The web compartment will make a strategy getServletInfo() in the subsequent servlet.For illustration:

open String getServletInfo() {

return “created by Sonoo Jaiswal”;

}

 5)buffer :

The cradle trait sets the support measure in kilobytes to deal with yield created by the JSP page.The default size of the cushion is 8Kb.

Case of cushion quality :

<html>

<body>

<%@ page buffer=”16kb” %>

Today is: <%= new java.util.Date() %>

</body>

</html>

6)language

The dialect characteristic indicates the scripting dialect utilized as a part of the JSP page. The default esteem is “java”.

7)isELIgnored

We can disregard the Expression Language (EL) in jsp by the isELIgnored quality. As a matter of course its esteem is false i.e. Articulation Language is empowered as a matter of course. We see Expression Language later.

<%@ page isELIgnored=”true” %>

8)isThreadSafe

Servlet and JSP both are multi threaded. If you need to control this conduct of JSP page, you can utilize is ThreadSafe property of page directive. The estimation of is ThreadSafe esteem is true.  If you make it false, the web holder will serialize the various solicitations, i.e. it will hold up until the JSP gets done with reacting to a demand before passing another demand to it. If you make the estimation of is ThreadSafe quality like

<%@ page isThreadSafe=”false” %>

The web compartment in such a case, will create the servlet as:

open class SimplePage_jsp expands HttpJspBase

actualizes SingleThreadModel{

}

9)errorPage :

The errorPage credit is utilized to characterize the blunder page, if special case happens in the present page, it will be diverted to the mistake page.

Case of errorPage characteristic :

/index.jsp

<html>

<body>

<%@ page errorPage=”myerrorpage.jsp” %>

<%= 100/0 %>

</body>

</html>

10)isErrorPage

The isErrorPage credit is utilized to proclaim that the present page is the blunder page.

 Example:

/myerrorpage.jsp

<html>

<body>

<%@ page isErrorPage=”true” %>

Sorry an exception occured!<br/>

The exception is: <%= exception %>

</body>

</html>

Jsp Include Directive:

The incorporate mandate is utilized to incorporate the substance of any asset it might be jsp record, html document or content document. The incorporate mandate incorporates the first substance of the included asset at page interpretation time (the jsp page is deciphered just once so it will be ideal to incorporate static asset).

Syntax

<%@ include file=”resourceName” %>

 Example:

<html>

<body>

<%@ include file=”header.html” %>

Today is: <%= java.util.Calendar.getInstance().getTime() %>

</body>

</html>

 JSP Taglib directive

The JSP taglib order is utilized to characterize a label library that characterizes many labels. We utilize the TLD (Tag Library Descriptor) record to characterize the labels. In the custom label segment we will utilize this label so it will be ideal to learn it in custom tag.

Syntax
  1. <%@ taglib uri=”uriofthetaglibrary”prefix=”prefixoftaglibrary” %>

Example of JSP Taglib directive:

In this example, we are using our tag named currentDate. To use this tag we must specify the taglib directive so the container may get information about the tag.

<html>

<body>

<%@ taglib uri=”http://www.javatpoint.com/tags” prefix=”mytag” %>

<mytag:currentDate/>

</body>

</html>

Exception Handling:

The special case is regularly a protest that is tossed at runtime. Special case Handling is the procedure to deal with the runtime mistakes. There may happen special case whenever in your web application. So dealing with special cases is a more secure side for the web engineer. In JSP, there are two approaches to perform exemption taking care of

  1. By errorPageand isErrorPage attributes of page directive
  2. By <error-page>element in web.xml file.

They are 3 files as shown below

  • index.jsp for input values
  • process.jsp for dividing the two numbers and displaying the result
  • error.jsp for handling the exception
 index.jsp

<form action=”process.jsp”>

No1:<input type=”text” name=”n1″ /><br/><br/>

No1:<input type=”text” name=”n2″ /><br/><br/>

<input type=”submit” value=”divide”/>

</form>

 process.jsp

<%@ page errorPage=”error.jsp” %>

<%

String num1=request.getParameter(“n1”);

String num2=request.getParameter(“n2”);

int a=Integer.parseInt(num1);

int b=Integer.parseInt(num2);

int c=a/b;

out.print(“division of numbers is: “+c);

%>

error.jsp

<%@ page isErrorPage=”true” %>

<h3>Sorry an exception occured!</h3>

Exception is: <%= exception %>

Error-page element in web.xml file

This approach is better since you don’t have to determine the errorPage quality in each jsp page. Determining the single section in the web.xml record will deal with the special case. For this situation, either indicate special case sort or blunder code with the area component. In the event that you need to deal with all the exemption, you should determine the java.lang.Exception in the special case sort component. 

web.xml file if you want to handle any exception

<web-app>

<error-page>

<exception-type>java.lang.Exception</exception-type>

<location>/error.jsp</location>

</error-page>

</web-app>

 web.xml file if you want to handle the exception for a specific error code:

<web-app>

<error-page>

<error-code>500</error-code>

<location>/error.jsp</location>

</error-page>

</web-app>

2) index.jsp file is same as in the above example
3) process.jsp

<%  <%@ page errorPage=”error.jsp” %>

String num1=request.getParameter(“n1”);

String num2=request.getParameter(“n2”);

int a=Integer.parseInt(num1);

int b=Integer.parseInt(num2);

int c=a/b;

out.print(“division of numbers is: “+c);

%>

4) error.jsp file is same as in the above example

Subscribe
to our newsletter

Drop Us A Query

Trending Courses
  • Selenium with python
    Selenium with Python Training
  • As we know, that Selenium with Python Web Browser Selenium Automation is Gaining Popularity Day by Day. So many Frameworks and Tools Have arisen to get Services to Developers.

  • Deep learning course
    Deep Learning Course
  • Artificial Intelligence, Deep mastering (DL) is completely about, many levels of Representation and sort of abstraction. That guide to design a sense of Information like Images, sound and text format.

  • machine learning with python
    Machine Learning with Python Training
  • Over last few years, Big Data and analysis have come up, with Exponential and modified Direction of Business. That operate Python, emerged with a fast and strong Contender for going with Predictive Analysis.

  • Data science with R
    Data Science With R Training
  • Understanding and using Linear, non-linear regression Models and Classifying techniques for stats analysis. Hypothesis testing sample methods, to get business decisions.

  • data science with python
    Data Science with Python Training
  • Everyone starts Somewhere, first you learn basics of Every Scripting concept. Here you need complete Introduction to Data Science python libraries Concepts.

  • devops with azure
    Devops with Azure Training
  • As we Know Azure DevOps is a Bunch of Services, in guiding Developers. It contains CI/CD, pipelines, code Repositories, Visual Reporting Tools and more code management with version control.


100% Secure Payments. All major credit & debit cards accepted.