.


Divya

JSP scripting element


The JSP scripting elements are used to execute java code inside the JSP file.

There are three different type of scripting element

  • Scriptlet tag
  • Expression tag
  • Declaration tag

JSP Scriptlet tag

To execute java source code in jsp file scriptlet tag is used.

Syntax

<% Java source code %>

In this example, we are displaying a “Welcome to JSP Tutorial" message.

Example

<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<% out.print("Welcome to JSP Tutorial"); %>
</body>
</html>

Example

In this example, we have created two files index.html and home.jsp. The index.html file gets the first name and last name from the user and the home.jsp file prints the First name and last name with hello message.

index.html

<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<form action="home.jsp">
<input type="text" placeholder="first name" name="fname"> <br><br>
<input type="text" placeholder="last name" name="lname"> <br> <br>
<input type="submit" value="submit"><br>
</form>
</body>
</html>

home.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Scriptlet tag</title>
</head>
<body>
<%
String fname=request.getParameter("fname");
String lname=request.getParameter("lname");
out.print("Hello "+fname +" " +lname);
%>
</body>
</html>

Output

JSP Scriptlet
JSP Scriptlet tag

.