JSP scriptlet

The JSP scriptlet contains the statements of Java or declarations of variables or methods that will be used in the JSP page. The _jspService method can be implemented by scriplet tage by writing the java code.

The syntax of scriptlet is as follows:

<% code %>

 

Example of scriptlet:

Consider the following example in which we have inserted a scriptlet tag to print the value of n:

CODE:

< html >

< head >

< title > JSP Page < /title >

< /head >

< %

       int n = 1;

   % >

< body >

        Value of n:

< % out. println (++n); % >

< /body >

< /html >

In the above example if we remove the scriptlet tag and then if we run it as a JSP page then all the things will appear as it is on the browser. This is because we removed the scriptlet tag and the content will be considered as an HTML code. Whatever written in the scriptlet tag is considered and compiled as java code.

 

Example of JSP scriptlet tag:

Consider the following example in which we have created a JSP page and it will use the request parameter to get the user name from an HTML page and the HTML page will get the name of the user from the user.

HTML page:

In the HTML page we will be creating a form to get the name of the user:

< form method = “POST” action = “asd. jsp” >

    Username  < input type = “text” name = “name” >

< input type = “submit” value = “Enter” >

</ form >

The input type of the form is text and when the user enters his name he will submit the form by clicking on the enter button. The values will then be passed to asd.jsp when the enter button is clicked by making a post request as we have used the post method that is method = “POST”.

JSP page:

<html>

<head>

<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>

<title> JSP Page </title>

</head>

<% String user = request. getParameter(“name”); %>

<body>

Name of the user: , <% out.println (name); %>

</body>

</html>

In the above example the request is considered as the HTTP request because when the JSP code is translated to servlet the _jspService method is executed and in this method we have HTTPServletRequest and an argument that is HTTPServletResponse. In this way the username can be easily get by using the getParameter () method. In the above code the HTTP request contains the parameters that are sent from the form in the html page. The getParameter () method has parameter “name” which will get the value from the html page.

 

Mixing Scriptlet tag and HTML:

Consider the following example in which we are creating a table by using both the scriptlet tag and the html part:

CODE:

< table border = 1>

<%

for ( int i = 0; i < 5; i++ ) {

%>

<tr>

<td> Digits </td>

<td><%= i+1 %></td>

</tr>

<%

}

%>

</table>

In the above example we have used scriptlet tag and html code, it can be seen that the java code is inserted into the scriptlet tag and the html part is outside of the scriptlet tag. In this way we have created a table in html and inside the table we have dynamic values. It should be noted here that the above code will be written inside the body tag of the JSP file. We can do a lot of things like the same by inserting the java code inside the scriptlet tag and with the html part.

Now consider the following example in which we have inserted the if else condition of Java programming language and we will be printing YouarePassed if the marks are greater than 40 else You are fail will be printed:

<% ! int marks = 54 %>

<% if ( marks > 40 ) { %>

<p > You are passed < /p >

<%  } else {  %>

<p> You are failed</p>

<% } %>

In the above example we have declared and initialized the variable “marks” by using the declaration tag. As if else is a java code therefore, it will be kept inside the scriptlet tag and the html part is outside of the scriptlet tag. In the above code the condition will be checked and corresponding text will appear. This code will be written inside the body tag of the html page.