Bom, tenho 2 problemas, o primeiro, é o seguinte.
1 - Tenho uma aplicaçao para entregar para segunda para facul, e estou desenvolvendo um site de caronas, consigo cadastrar a Pessoa, porem, no momento que logo com essa pessoa, tenho problemas em dizer para a tabela de carona, o ID dessa pessoa… como capturo isso da session ?
2 - Como eu poderia listar todas as carnos disponiveis no banco de dados ??
Segue os codigos:
Model do Carona:
[code]public class CaronaModel {
private int pid;
private int cid;
private String preco;
private String dataCarona;
private String horaSaida;
private String horaChegada;
private String localSaida;
private String localChegada;
private int vagas;
private String outrasInfo;
//O restante é get e set
}
[/code]
Bean da Carona:
[code]public class Carona {
public Carona() {
}
public CaronaModel getCarona(int id) {
CaronaModel caronaModel = null;
try {
String sql = "SELECT cid, preco, dataCarona, horaSaida, horaChegada, " +
"localSaida, localChegada, vagas, outrasInfo FROM carona WHERE pid = '" + id + "'";
BDConnector connector = new BDConnector();
PreparedStatement stm = connector.getConnection().prepareStatement(sql);
ResultSet rs = stm.executeQuery();
while (rs.next()) {
caronaModel = new CaronaModel();
caronaModel.setCid(rs.getInt(1));
caronaModel.setPreco(rs.getString(2));
caronaModel.setDataCarona(rs.getString(3));
caronaModel.setHoraSaida(rs.getString(4));
caronaModel.setHoraChegada(rs.getString(5));
caronaModel.setLocalSaida(rs.getString(6));
caronaModel.setLocalChegada(rs.getString(7));
caronaModel.setVagas(rs.getInt(8));
caronaModel.setOutrasInfo(rs.getString(9));
}
rs.close();
stm.close();
connector.closeConnection();
} catch (Exception e) {
e.printStackTrace();
}
return caronaModel;
}
public CaronaModel createCarona(CaronaModel caronaModel, int id) {
try {
String sql = "INSERT INTO carona (pid, cid, preco, dataCarona, horaSaida, horaChegada, " +
"localSaida, localChegada, vagas, outrasInfo) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
BDConnector connector = new BDConnector();
PreparedStatement stm = connector.getConnection().prepareStatement(sql);
stm.setInt(1, caronaModel.getPid());
stm.setInt(2, caronaModel.getCid());
stm.setString(3, caronaModel.getPreco());
stm.setString(4, caronaModel.getDataCarona());
stm.setString(5, caronaModel.getHoraSaida());
stm.setString(6, caronaModel.getHoraChegada());
stm.setString(7, caronaModel.getLocalSaida());
stm.setString(8, caronaModel.getLocalChegada());
stm.setInt(9, caronaModel.getVagas());
stm.setString(10, caronaModel.getOutrasInfo());
stm.executeUpdate();
stm.close();
connector.closeConnection();
} catch (Exception e) {
e.printStackTrace();
}
return caronaModel;
}
}[/code]
Agora os jsp’s e o servlet de cadastro:
Index.jsp:
[code]<%@ page language=“java” contentType=“text/html; charset=ISO-8859-1” pageEncoding=“ISO-8859-1”%>
<%@ taglib uri=“http://java.sun.com/jsf/html” prefix=“h” %>
<%@ taglib uri=“http://java.sun.com/jsf/core” prefix=“f” %>
DedaoPraCima |
---|
<div id="navigationRegionVertical">
<dl class="demos-nav">
<dt>Menu</dt>
<dd>
<a href="javascript:loadContent('main.jsp');" id="" class="" title="In�cio">
Início
</a>
</dd>
<%
if (session.getAttribute("usuario") == null) {
%>
<dd>
<a href="javascript:loadContent('login.jsp');" id="" class="" title="Login">
Login
</a>
</dd>
<dd>
<a href="javascript:loadContent('cadastro.jsp');" id="" class="" title="Cadastro">
Cadastrar
</a>
</dd>
<%
} else {
%>
<dd>
<a href="javascript:loadContent('caronas.jsp');" id="" class="" title="Caronas">
Caronas
</a>
</dd>
<dd>
<a href="javascript:loadContent('cadastroCarona.jsp');" id="" class="" title="CadastroCarona">
Cadastrar Carona
</a>
</dd>
<dd>
<a href="logout.jsp" id="" class="" title="Logout">
Logout
</a>
</dd>
<%
}
%>
</dl>
</div> <!-- end #navigationRegionVertical div -->
<div id="contentRegion">
<%
if (session.getAttribute("usuario") == null) {
%>
<div align="center">
<jsp:include page="main.jsp"></jsp:include>
</div>
<%
} else {
%>
<jsp:include page="caronas.jsp"></jsp:include>
<%
}
%>
</div> <!-- end #contentRegion div -->
<!-- This clearing element should immediately follow the #contentRegion div in order to force the #container div to contain all child floats --><br class="clearfloat" />
<div id="footerRegion">
<br/>
</div> <!-- end #footerRegion div -->
</div> <!-- end #container div -->
</body>
[/code]
Login.jsp
[code]<%@ page language=“java” contentType=“text/html; charset=ISO-8859-1” pageEncoding=“ISO-8859-1”%>
<%@ taglib uri=“http://java.sun.com/jsf/html” prefix=“h” %>
<%@ taglib uri=“http://java.sun.com/jsf/core” prefix=“f” %>
var validaUsuario = $('#validaUsuario');
var validaSenha = $('#validaSenha');
$('#formLogin').submit(function () {
if (document.formLogin.usuario.value != "" && document.formLogin.senha.value != "") {
$('#progresso').html('<img src="images/ajax-loader.gif" height="16" width="16" /> enviando dados...');
document.getElementById("message").style.display = "none";
var optionsFormLogin = {
target: "#message",
success: processResponse
};
$(this).ajaxSubmit(optionsFormLogin);
} else {
if (document.formLogin.usuario.value == "") {
validaUsuario.html('informe o usuário');
} else {
validaUsuario.html('');
}
if (document.formLogin.senha.value == "") {
validaSenha.html('informe a senha');
} else {
validaSenha.html('');
}
}
return false;
});
});
function processResponse(responseText, statusText, xhr, $form) {
$('#progresso').html('');
if (responseText == "ok") {
window.location.href = "index.jsp";
session.putValue("username", username);
} else {
$('#message').html('<img src="images/stop.png" align="middle" /> ' + responseText);
document.getElementById("message").style.display = "block";
}
}
</script>
</head>
<body>
<p id="message" style="display: none;"></p>
<form name="formLogin" id="formLogin" class="" action="LoginServlet" method="post">
<fieldset id="" class="">
<legend align="left">Login</legend>
<p>
<label id="" class="">Usuário:
<input type="text" name="usuario" id="usuario" size="15" maxlength="20"/>
</label>
<span class="erro" id="validaUsuario"></span>
</p>
<p>
<label id="" class="">Senha:
<input type="password" name="senha" id="senha" size="15" maxlength="8"/>
</label>
<span class="erro" id="validaSenha"></span>
</p>
<p>
<label>
<input type="submit" id="submit" class="" name="" value="Enviar"/>
<span class="progresso" id="progresso"></span>
</label>
</p>
</fieldset>
</form>
<br/>
</body>
[/code]
CadastrarCarona.jsp
[code]
<?xml version="1.0" encoding="ISO-8859-1" ?><%@ page language=“java” contentType=“text/html; charset=ISO-8859-1”
pageEncoding=“ISO-8859-1”%>
Data Carona:
Hora Saida:
Hora Chegada:
Local Saida:
Local Chegada:
Vagas:
Outras Info:
[/code]
E o servlet que eu quero que ele saiba o ID da session
CadastraCaronaServle.java
package br.ufscar.dc.dedaopracima.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import br.ufscar.dc.dedaopracima.bean.Endereco;
import br.ufscar.dc.dedaopracima.bean.Login;
import br.ufscar.dc.dedaopracima.bean.Pessoa;
import br.ufscar.dc.dedaopracima.model.EnderecoModel;
import br.ufscar.dc.dedaopracima.model.LoginModel;
import br.ufscar.dc.dedaopracima.model.PessoaModel;
/**
* Servlet implementation class Compromisso
*/
public class CadastraCaronaServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public CadastraCaronaServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*EU GOSTARIA QUE AQUI, EU CONSEGUISSE O ID DA PESSOA LOGADA*/
}
}
Bom, coloquei tudo isso para que entendam melhor meu codigo…
Abçs