• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

JSP Syntax

January 16, 2014 //  by Krishna Srinivasan//  Leave a Comment

  • 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:

  1. <%!int fontSize=10;%> is a declaration of variable fontSize
  2. <%—- addition Program –%> is a comment
  3. <% int a = 5;int b = 3; int result = a+b;%> is a Scriptlet.
  4. <%= fontSize %> is an Expression.

Execute the above code in Eclipse and the output would be:

jsp_syntaxexample_demo

Previous Tutorial :  Create JSP Page in Eclipse   ||  Next Tutorial :  JSP Actions

Category: Java EETag: JSP Tutorials

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Bootstrap Vs Foundation
Next Post: JSP Actions »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact