Guia01 Ejemplos Simple Con JSP

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Desarrollo con aplicaciones en software libre Instituto privado KHIPU

Ejemplos simple con JSP


In tomcat, all JSP code should be copied (Deployed) into webapps folder and create own
folder in webapps e.gjsp, in jsp folder copy all your jsp files. Remember, Java is case
sensitive language.

1. JSP Example Simple Print on browser

jspExample1.jsp Output

<html>
<body>
<%
out.print("Hello World!");
%>
</body>
</html>

2. JSP Example Expression base print on browser

jspExample2.jsp Output

<html>
<body>
<%="Hello World!"%>
</body>
</html>

3. JSP Example Current date print on browser

jspExample3.jsp Output

<%@ page language="java" import="java.util.*" errorPage="" %>


<html>
<body>
Current Date time: <%=new java.util.Date()%>
</body>
</html>

Declare a variable en JSP


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


jspString.jsp Output

<%@ page language="java" errorPage="" %>


<%
String stVariable="this is defining String variable";
%>

<html>
<body>

Docente: Prof. Godofredo Poccori Umeres Página 1 de 3


Desarrollo con aplicaciones en software libre Instituto privado KHIPU

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

jspInt.jsp Output

<%@ 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

jspFloat.jsp Output

<%@ 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

jspBoolean.jsp Output

<%@ page contentType="text/html; charset=iso-8859-1" language="java"


%>
<%

Docente: Prof. Godofredo Poccori Umeres Página 2 de 3


Desarrollo con aplicaciones en software libre Instituto privado KHIPU

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”

http://www.easywayserver.com/jsp/JSP-example.htm

Docente: Prof. Godofredo Poccori Umeres Página 3 de 3

You might also like