JSP - Introduction in Coding
JSP - Introduction in Coding
Before you continue you should have a basic understanding of the following:
• HTML/XHTML
• JavaScript
What is JSP?
JSP Syntax
JSP code is executed on the server, and the plain HTML result is sent to the browser.
A JSP scripting block always starts with <% and ends with %>. A JSP scripting block can be
placed anywhere in the document.
<%
%>
A JSP file normally contains HTML tags, just like an HTML file, and some JSP scripting code.
Page 1 of 5
Below, we have an example of a simple JSP script which sends the text "Hello World" to the
browser:
<html>
<body>
<%
out.println("Hello World");
%>
</body>
</html>
Each code line in JSP must end with a semicolon. The semicolon is a separator and is used to
distinguish one set of instructions from another.
In the example above we have used the out.println statement to output the text "Hello
World".
Note: The file must have a .jsp extension. If the file has a .html extension, the JSP code will
not be executed.
Comments in JSP
In JSP, we use // to make a single-line comment or /* and */ to make a large comment block.
<html>
<body>
<%
//This is a comment
/*
This is
a comment
block
*/
%>
</body>
</html>
Page 2 of 5
JSP Variables
<%
int intNum1;
int intNum2 = 42;
double dblNum1;
double dblNum2 = 7.22;
%>
Page 3 of 5
The Concatenation Operator
⇒ The concatenation operator (+) is used to put two string values together.
<%
String strMessage = "Hello";
String strName = "Jen";
Hello Jen
If we look at the code above you see that we used the concatenation operator two (2) times.
This is because we had to insert a third string (a space character), to separate the two
strings.
JSP Operators
Arithmetic Operators
Page 4 of 5
Relational Operators
Logical Operators
|| Logical OR
! not
Page 5 of 5