- Java EE Tutorials
- JSP Tutorials
- Recommended Books for Java Server Pages (JSP)
This tutorial explains the basic elements used in the JSP pages. Following table lists these JSP elements with syntax.
JSP Tag | Description | Syntax |
---|---|---|
Directive | Specifies translation time instruction to JSP engine. There are there types of directives: page,include and taglib directives. | <%@Directives%>, <%@page..%>, <%@include..%>, <%@taglib..%> |
Declaration | It is used to declare and define variables and methods. Variables or methods must be declared before you use it in the JSP file. | <%!some java code> |
Scriptlets | Tags are used to embed java code in JSP page. Allows to write free-form java code in JSP page. | <%count++%> |
Expression | Used to print value in the output html file. The value of expression element is converted to String. The expression element can contain any expression that is valid according to the Java Language Specification but you cannot use a semicolon to end an expression. | <%=an expression%> |
Action | Provides request time instructions to the JSP engine. | <jsp:actionname/> |
Comment | Used for documentation and contains texts or statements that JSP container ignores. | <%–Any text–%> |
The following example shows use of Declaration,Comment,Expression,Scriptlets tags at one place:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%!int fontSize=10;%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <%---- addition Program --%><br> <% int a = 5; int b = 3; int result = a+b;%><br> <font color="green" size="<%= fontSize %>"><br> <% out.print(" Total=" +result); %><br> </font> </body> </html>
Details of the above code:
- <%!int fontSize=10;%> is a declaration of variable fontSize
- <%—- addition Program –%> is a comment
- <% int a = 5;int b = 3; int result = a+b;%> is a Scriptlet.
- <%= fontSize %> is an Expression.
Execute the above code in Eclipse and the output would be:
Previous Tutorial : Create JSP Page in Eclipse || Next Tutorial : JSP Actions