Primeiro Exemplo

Arquivo: webapps\seunome\teste.jsp

<!--
Insira na pasta
webapps\seunome\teste.jsp
-->
<html>
<head>
<title>O tomcat está no ar</title>
</head>
<body>
<% String jsp = "JSP ok!!"; %>
<h1> <% out.println(jsp); %> </h1>
<%
String lista[];
lista = new String[7];
lista[0]="Segunda";
lista[1]="Terça";
lista[2]="Quarta";
lista[3]="Quinta";
lista[4]="Sexta";
lista[5]="Sábado";
lista[6]="Domingo";

String Texto;
Texto = "";
Texto += "Dias da semana: ";

for (int x=0; x <= 6; x++ ) {
Texto += lista[x]+", ";
}
%>
<p><% out.println(Texto); %></p>
</body>
</html>

Formulário

Arquivo: teste.html
<HTML>
<HEAD>
<TITLE>Formulário para Soma</TITLE>
</HEAD>
<BODY>
<FORM action="soma.jsp" method="post">
<INPUT name='vl1' size='5' value='2'/> +
<INPUT name='vl2' size='5' value='2'/>
<INPUT name='submit' type='submit' value='Soma' />
</FORM>
</BODY>
</HTML>
Arquivo: soma.jsp
<html>
<head>
<title>Soma</title>
</head>
<body>
<%
int vl1=Integer.parseInt(request.getParameter("vl1"));
int vl2=Integer.parseInt(request.getParameter("vl2"));
int soma=vl1+vl2;
%>
Resultado = <b> <%=soma%> </b>
</body>
</html>

Arquivo Esterno

Arquivo: teste.jsp
<html>
<head>
<title>Arquivo externo</title>
</head>
<body>
<jsp:forward page="arquivo_externo.jsp" />
</body>
</html>
Arquivo: arquivo_externo.jsp
<% String jsp = "JSP ok!!"; %>
<h1> <% out.println(jsp); %> </h1>

import

Arquivo: semana.jsp

<%@page import="java.util.Calendar" %>
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<%
String Texto;
Texto ="";
// Nome do arquivo: semana.txt
Calendar DataToda = Calendar.getInstance();

int DiaDaSemana=DataToda.get(Calendar.DAY_OF_WEEK);

switch(DiaDaSemana) {

case 1: Texto = "Domingo"; break;
case 2: Texto = "Segunda"; break;
case 3: Texto = "Terça"; break;
case 4: Texto = "Quarta"; break;
case 5: Texto = "Quinta"; break;
case 6: Texto = "Sexta"; break;
case 7: Texto = "Sábado"; break;
}
out.println("<h1>Hoje é "+Texto+".</h1>");
%>
</body>
</html>

Passando variável via link

Arquivo: teste.html
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<a href="teste.jsp?Parametro1=Valor1&Parametro2=Valor2">Teste</a>
</body>
</html>
Arquivo: teste.jsp
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<%
String Var1 = request.getParameter("Parametro1");
String Var2 = request.getParameter("Parametro2");
out.println(Var1 + " " + Var2);
%>
</body>
</html>

Cookies

Arquivo: index.jsp
<%@page import="java.net.*" %>
<%
String assinatura = "algumacoisa";
String cookieName = "assinatura" ;
Cookie cookie = new Cookie(cookieName, assinatura );
cookie.setMaxAge(360); // 1 hora
response.addCookie(cookie);
%>
<html>
<head>
<title>Cookies</title>
</head>
<body>
<a href="recebido.jsp">recebido.jsp </a>
</body>
</html>
Arquivo: recebido.jsp

<%@page import="java.net.*" %>
<html>
<head>
<title>Cookies</title>
</head>
<body>
<%
String cookieName = "assinatura";
Cookie cookies[] = request.getCookies();
Cookie Redcookie = null;
if(cookies != null){
for( int x=0; x<cookies.length; x++ ){
if( cookies[x].getName().equals(cookieName)){
Redcookie=cookies[x];
break;
}
}
}
if(Redcookie == null){
// ======================
// Se o cookie foi
// Excluido
// ======================
out.println("Cookie não localizado");
}else{
// ======================
// lendo o cookie
// ======================
out.println("assinatura = " + URLDecoder.decode(Redcookie.getValue()));
// ======================
// excluindo o cookie
// ======================
Redcookie.setMaxAge(0);
response.addCookie(Redcookie);
}
%>
</body>
</html>

Funções

Arquivo: funcao.jsp
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<p><% out.println(funcao_x()); %></p>
</body>
<%!
public String funcao_x(){
return "Hello Java";
}
%>
</html>

Objeto

Arquivo: objeto.java

<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<p><%
CD cd = new CD();
String Texto = cd.getCodigo();
out.println(Texto);
%></p>
</body>
<%!
public class CD {

public String getCodigo(){
return "CD => Hello Java";
}
}
%>
</html>