×
☰ See All Chapters

Scriptlet tag in JSP

JSP Scripting elements allow you to insert Java code in your JSP pages. There are three types of scripting elements:

  1. Scriptlets 

  2. Declarations 

  3. Expressions 

Scriptlets

  • Scriptlets are written inside <% %> 

  • Scriptlets will be used to write any valid java statement like local variables or any other java code. We have to consider like we are writing a code inside some method when we are writing code inside scriptlets. 

  • After conversion of JSP into servlets, all the statements inside the scriptlet will be placed inside the _jspService () method. Hence method declarations are not allowed inside a scriptlet. (we cannot declare a method inside method) 

  • All the statements inside the scriptlet must be terminated with semi colon. 

JSP Scriptlet tag Example

Project directory structure

jsp-scriptlet-0
 

scripletsDemo.jsp

<%@ page session="false"%>

<%@ page import="java.sql.*"%>

<%

        try {

                //Class.forName("com.mysql.jdbc.Driver"); //for mysql versions lesser than 8.0

        Class.forName("com.mysql.cj.jdbc.Driver");//for mysql version 8.0, should use java 8 and above

        } catch (ClassNotFoundException e) {

                System.out.println(e.toString());

        }

%>

<html>

<head>

<title>Employee List</title>

</head>

<body>

        <center>

                <br>

                <h2>Employee List</h2>

                <br> <br>

                <table>

                        <tr>

                                <th>Emp ID</th>

                                <th>Name</th>

                                <th>Salary</th>

                        </tr>

                        <%

                                String sql = "select * from emp";

                                try {

                                        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/study", "root", "root");

                                        Statement s = con.createStatement();

                                        ResultSet rs = s.executeQuery(sql);

                                        while (rs.next()) {

                                                out.println("<tr>");

                                                out.println("<td>" + rs.getInt(1) + "</td>");

                                                out.println("<td>" + rs.getString(2) + "</td>");

                                                out.println("<td>" + rs.getString(3) + "</td>");

                                                out.println("</tr>");

                                        }

                                        rs.close();

                                        s.close();

                                        con.close();

                                } catch (SQLException e) {

                                } catch (Exception e) {

                                }

                        %>

                </table>

        </center>

</body>

</html>

Database script (MySQL DB)

CREATE TABLE EMP(EMP_ID INT(5), NAME VARCHAR(30), SALARY INT(6));

INSERT INTO EMP (EMP_ID,NAME,SALARY) VALUES (103,'Manu',3000);

INSERT INTO EMP (EMP_ID,NAME,SALARY) VALUES(104,'Tyagraj',4000);

INSERT INTO EMP (EMP_ID,NAME,SALARY) VALUES(105,'Likitha',5000);

INSERT INTO EMP (EMP_ID,NAME,SALARY) VALUES(106,'Advith',6000);

COMMIT;

Output

jsp-scriptlet-1
 

 


All Chapters
Author