JSP-JavaServer Pages, is a server side programming language. It is an object oriented language that uses the servlets technology of Java. JSP provides the flexibility to handle large amount of dynamic data, databases with performance and stability.
It has the ability to integerate with HTML very easily to enhance the presentation of a page. JSP pages helps to differentiate the design from the programming logic of a web page. Since it is plaform independent it can be used with any server.
To use Java Server Pages both JDK and Tomcat server has to be installed. Next we will see how to install JDK and Tomcat.
JDK or Java Development Kit is a combination of software, tools required to compile, debug, and run applets and applications written using Java language.
-
Just follow the steps for Linux
- Say, java is installed at /usr/local/jdk1.4.2
- Use the following commands for the "Bash Shell", a common one for Linux.
- export JAVA_HOME=/usr/local/jdk1.4.2
- export PATH=$JAVA_HOME/bin:$PATH
- echo $PATH
- /usr/local/jdk1.4.2/bin/usr/local/bin:/bin:/usr/bin
- echo $JAVA_HOME
Installing Tomcat on Windows
- Download the binary distribution of the Tomcat Server 6.0 from http://tomcat.apache.org.
- Usually this would be a zip file, unzip the file just run the executable file.
- Specify the location to be installed
- Provide the Port number, Admin Username, Password
- Specify the path where JRE is installed
- After installing the tomcat server, the CATILINA_HOME environment variable should be set to point the Tomcat installation directory.
- C:\Jakarta> set CATALINA_HOME=C:\Jakarta\jakarta-tomcat-5.0.12
Installing Tomcat on Linux
- Download the jakarta-tomcat-5.0.12.tar.gz file for the instance to /usr/local
- If GNU tar is not installed use the following command
- gunzip -c jakarta-tomcat-5.0.12 | tar xvf
- This will create a directory with sub directories.
- Now unpack the file with following commands.
- cd /usr/local
- tar xzvf jakarta-tomcat-5.0.12.tar.gz.
- Now set the CATALINA_HOME environment variable to point to the installed directory.
- export CATALINA_HOME=/usr/local/jakarta-tomcat-5.0.12.
- To start the server in background use- ./startup.sh
After the installation is completed, run tomcat server. If tomcat is installed properly, by typing "http://localhost:8080" in your bowser you will get the default home page of tomcat server.
Simple Program in JSP
To give an example for a JSP code, first we are going to print the text "Hello World". Try the the following syntax code.
Example:
Example
<html>
<body>
<!-- This is a JSP fIle-->
<%
out.println ("Hello World");
%>
</body>
</html>
|
Result:
HelloWorld
A JSP file looks just like a html file with some java code inside some tags. The server recognizes the code by the file extension "*.jsp" by which it is saved, now save the file as "first.jsp" in the "test" folder. Subsequent requests for the same jsp page will use the same object just created.
Pre-defined Variables
To make things simple, there are some predefined variables in JSP that need not be declared. These variables can be used with servlets and Java beans. Following are the list of predefined variables.
request:
This variable specifies the data included in a http request. This variable takes value from the clients' browser to pass it over to the server.
reponse:
This variable specifies the data included in the http response. It is used with cookies and also in http headers.
out:
This variable specifies the output stream otherwise known as printwriter in a page context.
session:
This variable specifies the data associated with httpsession object with a specific session of a user. The main purpose of this object is to use the session information to maintain multiple page requests.
application:
This variable is used to share data with all application pages.
Config:
This variable refers the java servlet configuration value.
pagecontext:
This variable is used to store the environment for the page like page attributes, access to the request, response and session objects.
page:
This variable is used to store the instance of the generated java servlet.
Syntax of Tags
JSP Tags or Action Elements represent dynamic actions that occur at runtime. JSP Tags are used to write text into a file, send an email, retreive data from a database and many more. Tags are grouped into libraries called tag libraries. Tag libraries are specified in a jsp file by a uri.
Following is the syntax for tags used in JSP.
Syntax:
<prefix:action_name attr1="value1" attr2="value2">action_body<prefix:action_name>
|
Following is the syntax of a JSP tag without a body.
Syntax:
<prefix:action_name attr1="value1" attr2="value2" />
|
In the above syntax the closing tag is with a "/" slash at the end as it does not have a body. Usually the "prefix" attribute refers to the library the action belongs to and it enables action in different library to have the same name. The "uri" attribute identifies the tag library used for the action.
Example:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"% >
<p >Tag Library </p >
<c:out value="${1+2}"/ >
Result:
In the above example "c" is the prefix which refers the tag library "core" and "out" is the action. We use the JSTL tag c:out to display the result of adding two numbers. Here ${1+2} in an expression.
JSP Expression Language (EL)
Simpler form for JSP Expressions came with JSTL 1.0, in the form of Expression Language. EL is used to set action attribute values from different sources at runtime. JSP Expression Language is also used without parameters but as actions directly in a webpage.
JSP EL always starts with a "$" followed by a "{" and ends with a "}". The expression is specified inside the brackets. A set of implicit variables provide access to the requested data.
Example:
${2+3+4}
JSP EL supports literal numbers, the boolean value "true,false", "null", the strings enclosed in single and double quotes.
Arithmetic Operator in JSP EL
Arithmetic operators are used for basic arithmetic operations like addition, subtraction, multiplication , division and modulus in JSP Expression Language.
Logic | Expression | Result |
Addition | ${2+3} | 5 |
Addition | ${2.5+3} | 5.5 |
Addition | ${3.3E4 + 2.5} | 33002.5 |
Subraction | ${-2-3} | -5 |
Multiplication | ${2*3} | 6 |
Division | ${6/2} | 3 |
Division | ${6 div 2} | 3 |
Division | ${6/0} | Infinity |
Modulus | ${7%3} | 1 |
Modulus | ${7 mod 3} | 1 |
Example:
The Modulus of 7 and 3 is: "${7%3}"
Result:
The Modulus of 7 and 3 is: "1"
In the above example we have used the "%" or the "modulus" operator to find the remainder of x and y, which has values "7" and "3". The result is 1 as the remainder after dividing "7" by "3" is 1.
EL Logical Operator
JSP Expression Language Logical operators are used with two operands that returns either a "true" or "false" value.
Logic | Expression | Result |
And | ${true and true} | true |
And | ${true && false} | false |
The result of using NOT operator with (3>1) is: ${b}
Result:
Not Operator
The result of using NOT operator with (3>1) is: false
In the above example we have use "!" operator, used with a expression "3>1" which is "true". Since the "!" operator is used it becomes "false", this boolean value is stored in a variable "boo" and displayed.
Relational Operators in JSP
Equality or relational operator in JSP Expression Language is used to check the values returned by two expressions or operands. Comparisons can also be done with other values like boolean, string, integer, or floating point literals.
Logic Expression Result
Equal ${10.0==10} true
Equal ${10.0 eq 10} true
Not Equal ${((20*10)!= 200)} true
Not Equal ${3 ne 3} false
Greater than or equal ${3.2>=2} true
Greater than or equal ${3.2 ge 2} true
Less than ${2<3} true
Less than ${4 lt 6} true
Less than or equal ${2 <= 4} true
Less than or equal ${4 le 2}
Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
Relational Operator Example
It's true that Computer is not equal to computer
Result:
Relational Operator Example
It's true that Computer is not equal to computer
In the above example we have used a "if" statement along with relational operator "!=" to compare two strings "Computer" and "computer" which are not equal since the first letter "C" are in different cases. So its "true" the message "It's true that Computer is not equal to computer" is displayed.
EL Empty Operator in JSP
Empty operator in JSP Expression Language is used for prefix operation to check whether a value is empty or null.
Example:
Empty Operator Example
The Value for the Empty operator is:: ${empty "txxt"}
Result:
Empty Operator Example
The Value for the Empty operator is:: false
In the above example we have used the empty operator along with a text "txxt". Empty operator checks finds the string so its not "null" so returns "false".
EL Conditional Operator in JSP
Conditional Operator is just same as the ternary operator that take three operands, first the "condition" based on which it provides any one of the two results "result1" or "result2".
|
|