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
Drop Us A Query

Trending Courses
  • Python and Django Online Training
  • Python is a dynamic interrupted language which is used in wide varieties of applications. It is very interactive object oriented and high-level programming language.

  • Tableau Online Training
  • Tableau is a Software company that caters interactive data visualization products that provide Business Intelligence services. The company’s Head Quarters is in Seattle, USA.

  • MicroStrategy Online Training
  • Micro Strategy is one of the few independent and publicly trading Business Intelligence software provider in the market. The firm is operational in 27 Countries around the globe.

  • PEGA (PRPC) 7.2 Certification Online Training
  • Pega Systems Inc. is a Cambridge, Massachusetts based Software Company. It is known for developing software for Customer Relationship Management (CRM) and Business process Management (BPM).

  • Workday Online Training
  • Workday specialises in providing Human Capital Management, Financial Management and payroll in online domain.It is a major web based ERP software vendor.

  • Power BI Online Training
  • Power BI is business analytics service by Microsoft. With Power BI, end users can develop reports and dashboards without depending on IT staff or Database Administrator.


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