Sunday, 8 March 2015

jsp Scriptlets

Scriptlet is a set of java statements.

Syntax
<%
statement1
statement2
statementN
%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.util.*"%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Print Even Numbers</title>
    </head>
    <body>
        <h1>Even numbers upto 10 <br />
            <%
              for(int i=0; i<=10; i+=2){
                  out.print(i);
            %>
            <br />
            <%
              }
            %>
        </h1>
    </body>
</html>

Output

All the code in scriptlets goes to _jspService() method. A JSP page can contains many scriptlets. All the code in scriptles is appended in the order they encounterd, to the _jspService() method.

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>
        <table style="width:300px">
            <thead>
                <th> Num1 </th>
                <th> Num2 </th>
                <th> Result </th>
            </thead>
            
            <tbody>
                <%
                    int num =15;
                    int result;
                    for(int i=0; i<10; i++){
                        result = num * i;
                %>
                
                <tr>
                    <td><%=num%></td>
                     <td><%=i%></td>
                      <td><%=result%></td>
                </tr>
                
                <%
                    }
                %>
            </tbody>
        </table>
            
        </h1>
    </body>
</html>

Output








Prevoius                                                 Next                                                 Home

No comments:

Post a Comment