If you familiar with other programming language, java too has data type variable to identify what data to belongs. This variable stores information of data, and differentiate with others. E.g.
String, int, byte, long, float, double, boolean
Declare a variable
Declaration of variable in JSP is storing information of data. We need to define data’s type what is this. It may be a string, may be a int (integer), or float
1. String variable Example in JSP
<%@ page language="java" errorPage="" %>
<%
String stVariable="this is defining String variable";
%>
<html>
<body>
String variable having value : <%=stVariable%>
</body>
</html>
2. Int variable Example in JSP
int can only having numeric values e.g 1,2,3,5
<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<%
int iVariable=5;
%>
<html>
<body>
int variable having value : <%=iVariable%>
</body>
</html>
3. Long variable is same as int variable just long can store more bytes than int.
4. float variable Example in JSP
float variable can be having decimal numbers. E.g 5.6 ,23.455
<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<%
float fVariable=567.345f;
%>
<html>
<body>
float variable having value : <%=fVariable%>
</body>
</html>
When you work on float variable don’t forget to add f character float fVariable=567.345f, otherwise it will get error of “Type mismatch: cannot convert from double to float”
5. double variable Example in JSP
This is same as float variable; it can store more bytes than float variable. In double we don’t need to put f character
6. Boolean variable Example in JSP
This is useful when we need to check condition. Let’s check in example
<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<%
boolean checkCondition=false;
%>
<html>
<body>
<%
if(checkCondition==true)
{
out.print("This condition is true");
}
else
{
out.print("This condition is false");
}
%>
</body>
</html>
Output this jsp page “This condition is false”
|