}
public String[] getItems() {
String[] s = new String[v.size()];
v.copyInto(s);
return s;
}
public void processar(String[] itens) { if (itens != null && comando != null) {
if (comando.equals("adicionar")) adicionarItens(itens);
else if (comando.equals("remover")) removerItens(itens);
reset();
} }
private void reset() { comando = null;
} }
</th>
<tr bgcolor="#f7f7f7">
<td align=center>Pesquisa</td>
<td align=center>
<input type="text" name="chave" size=30>
<input type=submit value="Pesquisar >>">
<input type=reset value="Limpar">
</td>
</tr>
</form>
</table>
<%
String tem = (String) request.getParameter("chave");
if (tem != null && !tem.equals("")) {
%>
<br><br>
<form action=’carrinho.jsp’ method=post>
<table align=center width="70%">
<th bgcolor="#CCDDEE" colspan=2 align=center>
M´ıdias encontradas
</th>
<th bgcolor="#CCDDEE" >Carrinho</th>
<pesquisa:mostremidias chave=’<%= tem %>’ >
<tr bgcolor="#E3E3E3">
<td align=center>
<b>M´ıdia:</b>
<%= titulo %>
</td>
<td align=center>
<b>Ano:</b> <%= ano %>
</td>
<td>
<input TYPE=checkbox name=’reserva’ value=’<%=titulo %>’ >
</td>
</tr>
<tr bgcolor="#F7F7F7">
<td align=center>
<%= descricao %>
</td>
<td align=center>
<%= tipo %>
</td>
<td> </td>
</tr>
<tr>
<td colspan="3" bgcolor="#FFFFFF">
</td>
</tr>
</pesquisa:mostremidias>
<tr bgcolor="#E0E0E0">
<td align=center>
<input type="submit" value="Confirma">
</td>
<td align=center>
<input type="reset" value="Limpar">
</td>
<td> </td>
</tr>
</table>
Este atributo escondido, diz qual a a¸c˜ ao desejada (adicionar ou remover).
<input TYPE=hidden name=’comando’ value=’adicionar’ >
</form>
<% } %>
</center>
</body>
</html>
Vamos ver a parte l´ ogica do carrinho agora.
Usa-se um bean v´ alido por toda a sess˜ ao. Com o m´ etodo processar(..), inclu´ımos ou removemos m´ıdias do BeanCarrinho.
Colocando o valor do atributo property da a¸c˜ ao <jsp:setProperty> como
“*”, ele vai, para todos os parˆ ametros presentes na requisi¸c˜ ao (objeto re-quest ), tentar usar o m´ etodo setNomeDoParametro() do Bean.
<jsp:useBean id="carrinho" scope="session" class="beans.BeanCarrinho" />
<jsp:setProperty name="carrinho" property="*" />
<%
String[] temp = request.getParameterValues("reserva");
if (temp != null) {
carrinho.processar(temp);
}
String[] items = carrinho.getItems();
if (items.length > 0) {
%>
<form action=’carrinho.jsp’ method=post>
<table>
<th bgcolor="#CCDDEE">Voc^e tem no carrinho:</th>
<th bgcolor="#CCDDEE" >Remover</th>
<%
for (int i=0; i<items.length; i++) {
%>
<tr>
<td bgcolor="#F0F0F0">
<%= items[i] %>
</td>
<td bgcolor="#E0E0E0">
<input type=checkbox name=reserva value=’<%= items[i] %>’>
</td>
</tr>
<%
}
%>
<tr bgcolor="#E0E0E0">
<td align=center>
<input type="submit" value="Confirma">
</td>
<td align=center>
<input type="reset" value="Limpar">
</td>
<td> </td>
</tr>
</table>
<input type=hidden name=’comando’ value=’remover’>
</form>
<%
}
%>
Cap´ıtulo 10 Release 9
Apenas para deixar a aplica¸c˜ ao com um interface melhor, vamos criar um arquivo index.jsp na raiz.
<html>
<head>
<title>Página inicial</title>
<style>
p, td { font-family:Tahoma,Sans-Serif; font-size:11pt;
padding-left:15; }
</style>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<table align="center" border="0" cellspacing="2" cellpadding="2">
<tr>
<td bgcolor="#CCDDEE" align=center colspan=5>
<h3>Escolha a opção</h3>
</td>
</tr>
<tr>
<td bgcolor="#F7F7F7" align=center>
<a href=’cadastro/’>Cadastro</a>
</td>
<td>
<a href=’login.jsp’>Login</a>
</td>
<td>
<a href=’pesquisa.jsp’>Pesquisa</a>
</td>
<td>
<a href=’carrinho.jsp’>Carrinho</a>
</td>
<td>
<a href=’titulos.jsp’>T´ıtulos dispon´ıveis</a>
</td>
</tr>
</table>
</body>
</html>
E outro arquivo index.jsp no diret´ orio /cadastro.
<html>
<head>
<title>Página de cadastro</title>
<style>
p, td { font-family:Tahoma,Sans-Serif; font-size:11pt;
padding-left:15; }
</style>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<table align="center" border="0" cellspacing="2" cellpadding="2">
<tr>
<td bgcolor="#CCDDEE" align=center colspan=2>
<h3>Escolha a opção</h3>
</td>
</tr>
<tr>
<td bgcolor="#F7F7F7" align=center>
<a href=’CadastroMidia.html’>M´ıdias</a>
</td>
<td>
<a href=’CadastroPessoa.html’>Pessoas</a>
</td>
</tr>
</table>
</body>
</html>