You are on page 1of 5

University of Makati

J.P. Rizal Extension, West Rembo, Makati City

Java Servlet Pages (JSP)

What You Should Already Know

Before you continue you should have a basic understanding of the following:

• HTML/XHTML
• JavaScript

What is JSP?

⇒ JSP stands for Java Servlet Pages


⇒ JSP is a server-side scripting language, like PHP
⇒ JSP scripts are executed on the server
⇒ JSP supports many database with the use of JDBC.

What is a JSP File?

⇒ JSP files can contain text, HTML tags and scripts


⇒ JSP files are returned to the browser as plain HTML
⇒ JSP files have a file extension of ".jsp"

What is MySQL?

⇒ MySQL is a database application


⇒ MySQL is ideal for both small and large applications
⇒ MySQL supports standard SQL
⇒ MySQL compiles on a number of platforms
⇒ MySQL is free to download and use

JSP Syntax

JSP code is executed on the server, and the plain HTML result is sent to the browser.

Basic JSP Syntax

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.

There are two basic statements to output text with JSP:


• out.println(“text here”);
• <%=”text here” %>

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

⇒ A variable is used to store information.

The Java programming language defines eight primitive data types.

1. boolean (for logical)


2. char (for textual)
3. byte
4. short
5. int
6. long (integral)
7. double
8. float (floating point).
9. String – represents a data type that contains multiple characters. It is not a
primitive data type, it is a class.

The correct way of declaring a variable in PHP:

<data type> <name> [=initial value];

Let's try creating more

<%
int intNum1;
int intNum2 = 42;
double dblNum1;
double dblNum2 = 7.22;
%>

Naming Rules for Variables

⇒ A variable name must start with a letter or an underscore "_"


⇒ A variable name can only contain alpha-numeric characters and underscores (a-z, A-
Z, 0-9,
and _)
⇒ A variable name should not contain spaces. If a variable name is more than one
word, it should be separated with an underscore (my_string), or with capitalization
(myString).
⇒ It is advisable to use prefixes to identify the data type of the variable throughout the
program.

Page 3 of 5
The Concatenation Operator

⇒ The concatenation operator (+) is used to put two string values together.

To concatenate two string variables together, use the concatenation operator:

<%
String strMessage = "Hello";
String strName = "Jen";

out.println (strMessage + " " + strName);


%>

The output of the code above will be:

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

⇒ Operators are used to operate on values.

Arithmetic Operators

Operator Description Example Result


+ Addition x=2 4
x+2
- Subtraction x=2 3
5-x
* Multiplication x=4 20
x*5
/ Division 15/5 3
5/2 2.5
% Modulus (division remainder) 5%2 1
10%8 2
10%2 0
++ Increment x=5 x=6
x++
-- Decrement x=5 x=4
x--

Page 4 of 5
Relational Operators

Operator Description Example


== is equal to 5==8 returns false
!= is not equal 5!=8 returns true
> is greater than 5>8 returns false
< is less than 5<8 returns true
>= is greater than or equal to 5>=8 returns false
<= is less than or equal to 5<=8 returns true

Logical Operators

Operator Description Example


&& Logical AND

& Boolean Logical AND

|| Logical OR

| Boolean logical inclusive OR

^ Boolean logical exclusive OR

! not

Page 5 of 5

You might also like