Searching...
Wednesday, 3 October 2012

JSP Scripting Example

12:12

JSP Scripting Example


JSP Scripting

Scripting is a JSP mechanism for directly embedding Java code fragments into an HTML page. Three scripting language components are involved in JSP scripting. Each component has its appropriate location in the generated servlet. This section examines these components.

Declarations

JSP declarations are used to define Java variables and methods in a JSP. A JSP declaration must be a complete declarative statement.
<%! int a=10 %>

Expressions

JSP expressions are JSP components whose text, upon evaluation by the container, is replaced with the resulting value of the container evaluation.
<%= expression %>

Scriptlets

Scriptlets are the JSP components that bring all the JSP elements together. They can contain almost any coding statements that are valid for the language referenced in the language directive. When JSP scriptlet code is converted into servlet code, it is placed into the generated servlet’s service()
<% scriptlet source %>

Example

<!--  Directive tag  -->
<%@ page language="java"import="java.util.Date"
       contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
       <!--  Scriplet Example Which will execute in the service method  -->
       <%
              Date date = new Date();
              out.println(date);
              out.println("Value of a from scriplet "+ a);
              out.println(test());
       %>
       <!--  Expression tag  -->
       <%
              for (int i = 0; i <= 5; i++) {
       %><br>
       <font size=<%=i%>> Hello world size defined in the expression tag </font>
       <%
              }
       %>
       <!--  Declaration tag  -->
       <%!int a = 10;
       String test()
       {
              return "Hello from declaration";
       }
       
       %>
</body>
</html>

Output


clip_image002











































0 comments: