Home Java TutorialJSP TAGS in JSP

TAGS in JSP

by anupmaurya

Writing a program in JSP is nothing but making use of various tags which are available in JSP. In JSP we have three categories of tags, they are as follows

  1. scriptlet tag
  2. directives
  3. standard actions.

SCRIPTING ELEMENTS:

Scripting elements are basically used to develop preliminary programming in JSP such as, declaration of variables, expressions and writing the java code. Scripting elements are divided into three types; they are declaration tag, expression tag and scriplet.

Declaration tag: Whenever we use any variables as a part of JSP we have to use those variables in the form of declaration tag i.e., declaration tag is used for declaring the variables in JSP page.

Syntax:

<%! Variable declaration or method definition %>

When we declare any variable as a part of declaration tag those variables will be available as data members in the servlet and they can be accessed through out the entire servlet. When we use any methods definition as a part of declaration tag they will be available as member methods in servlet and it will be called automatically by the servlet container as a part of service method.

Example of Declaration tag:

<%!
    int count() {
        return (a * b);
    }
%>

Expression tag: Expression tags are used for writing the java valid expressions as a part of JSP page.

Syntax:

<%= java valid expression %>

Example of Expression tag:

<html>  
<body>  
<%= "welcome to JavaServer Pages" %>  
</body>  
</html> 

Whatever the expression we write as a part of expression tags that will be given as a response to client by the servlet container. All the expression we write in expression tag they will be placed automatically in out.println () method and this method is available as a part of service method.

Note: Expressions in the expression tag should not be terminated by semi-colon (;) .

Scriplet tag: Scriplets are basically used to write a pure java code. Whatever the java code we write as a part of scriplet, that code will be available as a part of service () method of servlet.

Syntax:

<%  java source code %>  

Example of Scriplet tag:

<html>  
<body>  
<% out.print("welcome to JavaServer Pages"); %>  
</body>  
</html>  

Write a JSP page to print 1 to 10 numbers?

<html>
    <title>Print Numbers 1-10</title>
    <head>Numbers 1-10</head><br>
    <body>
        <%
            for (int i = 1; i <= 10; i++) {
                out.println (i);
            }
        %>
    </body>
</html>

This article is contributed by Mansi Yadav. If you like Techarge and would like to contribute, you can also write an article using https://techarge.in/start-blogging-with-us/ or mail your article to techarge.in@gmail.com . See your article appearing on the Techarge Home page and help others to learn. 🙂

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.