Hello JSP

Need:

Java code inside HTML.

Step : Run on server

Writing java code:

Way 1: using <% . .... %>

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>Hello</h1>
	<%
		int i = 3;
		int j = 8;
		int k = 3 + 8;
		out.println("Value of k is " + k);
	%>

</body>
</html>

Way 2: using <%=%>

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>Hello</h1>
	<%
		int i = 3;
		int j = 8;
		int k = i + j;
		out.println("Value of k is " + k);
	%>

	<%
		int l = 3;
		int m = 12;
		int n = l + m;
	%>
	Value of n is
	<%=n%>

</body>
</html>

Way 3 : <%!...%>

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>Hello</h1>
	<%!public int sum(int a, int b) {
		return a + b;
	}%>

	Value of sum of 67 and 89 is
	<%=sum(67, 89)%>
	<br>
	<%
		int i = 3;
		int j = 8;
		int k = i + j;
		out.println("Value of k is " + k);
	%>
	<br>

	<%
		int l = 3;
		int m = 12;
		int n = l + m;
	%>
	Value of n is
	<%=n%>

</body>
</html>

Last updated

Was this helpful?