JSP elements can be categorized into six categories. Like other languages, this is also well defined scripting language.
Directives provide general information about the JSP page that you create. This can be divide into three categories.
- Page directive
- Include directive
- Taglib directive
This is used to inform about the properties of the JSP page to JSP engine. Look at these following examples.
<%@ page language="java" %>
<%@ page contentType="text/html" pageEncoding="UTF-8" %>
<%@ page errorPage="loginError.jsp" %>
<%@ page import="java.util.* " %>
Likewise you can use many attributes with page directive. We talk more about these attributes later.
This is used to include another file into JSP page. This tells the JSP engine to include these files.
<%@ include file="copyright.html" %>
Like XML, JSP allows you to create custom tags that can be used in your JSP pages. Taglib is used to connect with tag library with a prefix.
<%@taglib uri="WEB-INF/tlds/tagLib.tld" prefix="first" %>
You will learn further more in later posts about directives. All three directive has more attributes. There are some rules of directives.
- A directive should be start with <%@ and end with %>
- All tag names, attributes and their values are case sensitive.
- The value must be enclosed with single/double quotes.( ' ' / " " )
- No space between equal sign(=) and the value.
DeclarationsThis is used to declare and define variables and methods in JSP page.
- Declaration statement should be start with <%! and closed with %>
- It can contain many declarations within a single tag.
<%! int x = 10; String name = "easyjavase"; %>
- Or you can use many declaration within many declaration tags.
<%! int x = 10; %> <%! String name = "easyjavase"; %>
Scriptlets are simple java codes that can be used in the JSP page. It can contain any Java code. Normally we use scriptlets to embed HTML code to JSP page. It starts with <% and closed with %>. Look at the following example.
<% out.print("<html>"); out.print("<body>"); out.print("<title>" + "This is the title" + "</title>"); out.print("Your IP address is " + request.getRemoteAddr()); out.print("</body>"); out.print("</html>"); %>
This is used to express Java expressions in JSP pages. You can use scriptlets for some codes without using JSP expressions. But it is easy to use expressions instead of using only scriptlets.
- It starts with <%= and end with %>.
- Expressions are not ended with semicolon.
- You cannot define anything inside an expression.
<%@page language="java"%> <html> <body> <%!int count = 0; %> Welcome ! <br> You are the <%= count++ %> visitor; </body> </html>
Actions are commands sent to the JSP engine to perform certain task. There are six predefined JSP actions.
- Custom tags
include / forward
These two actions are use to enable other web compornants in JSP pages.
useBean / setProperty / getProperty
These things are used to use JavaBeans in JSP pages.
This is used to embed client side components into JSP pages such as Applets.
You can create user defined actions called Custom tags.
Comments<%-- JSP comments --%>
<% //Java comments %>