JSP & Servlets
1.0.0
1.0.0
  • Setting up
  • Pre-requisites
  • Servlet Part 1
  • Servlet Part 2
  • Understanding the servlet
  • Servlet XML Configuration
  • POST method and passing parameters
  • Passing more parameters
  • Understanding GET and POST
  • Request, Session and Context Part -1
  • Request , Session and Context Part - 2
  • Understanding init, service and ServletConfig
  • Hello JSP
  • Understanding JSP
  • JSP Page directives
  • HttpServletRequest Path Decoding
  • Scopes in JSP and the PageContext Object
  • Understanding MVC pattern
  • PART -1 Writing an MVC app
  • JSTL
    • For-each
  • CRUD-1
  • Project Work
    • Create a Simple Java Web Application Using Servlet, JSP/JSTL and JDBC/Transaction
  • Locale Filter
  • Action Plan
    • Schedule
    • Hit webservice
Powered by GitBook
On this page
  • Need:
  • Writing java code:

Was this helpful?

Hello JSP

PreviousUnderstanding init, service and ServletConfigNextUnderstanding JSP

Last updated 6 years ago

Was this helpful?

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>