Tag JSP

JSP(Java Server Page) tag terdiri atas 3 macam yaitu declaration, scriplet dan expression. Masing- masing tag tersebut meiliki fungsi yang berbeda dalam sebuah page jsp.

Declaration
<%! [SOURCE_CODE] %>

Declaration berfungsi untuk mendeklarasikan variabel- variabel atau method dalam page JSP.

Scriplet
<% [SOURCE_CODE] %>

Scriplet berfungsi untuk mendefinisikan script atau alur program page JSP.

Expression
<%= [SOURCE_CODE] %>

Expression berfungsi untuk menampilkan nilai dari suatu variabel.

Berikut contoh penggunaan tag JSP :

<%@ page language="java" 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>Arithmetic</title>
</head>
<body>
	<%!
		public int multiply(int a, int b){
			return a*b;
		}
	%>
	
	<%
		int a=10;
		int b=2;
		int result = multiply(a, b);
		out.println("Multiply a + b = "+multiply(a, b));
	%>
	The value of Result is <%=result %>
	<%
		for(int c=0; c<10; c++){
			out.println("<br /> c = "+c);
		}
	%>
</body>
</html>

Output :

Tagged: , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: