problema con dto en servlet
Hola, estoy comenzando en esto de los servlets y tengo mucho lÃo. Resulta que tengo mi proyecto ear, con sus ejbs y su .war, y en un servlet quiero que me inserte unos datos en la bbdd. Para ello tengo un dto, con un método Add, que en teorÃa deberÃa insertármelos, pero cuando llamo al método, me dá el siguiente error:
Type mismatch: cannot convert from void to clientsDto
y creo que es porque el método donde llamo al método Add de mi dto es void, y mi dto no es void, pero no sé como resolver el problema, porque cuando lo he intentado me ha dado error. Por favor, ¿puede alguien echarme un cable? Muchas gracias, un saludo
Elena
Type mismatch: cannot convert from void to clientsDto
y creo que es porque el método donde llamo al método Add de mi dto es void, y mi dto no es void, pero no sé como resolver el problema, porque cuando lo he intentado me ha dado error. Por favor, ¿puede alguien echarme un cable? Muchas gracias, un saludo
Elena
Hola, os copio el código del servlet por si ayuda:
package es.BBDDCaja.cajaelena.Web;
import java.rmi.RemoteException;
import java.io.IOException;
import java.util.ArrayList;
import java.lang.*;
import javax.ejb.CreateException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import es.BBDDCaja.cajaelena.dto.clientsDto;
import es.BBDDCaja.cajaelena.facade.Home.clientsFacade;
import es.BBDDCaja.cajaelena.facade.Home.clientsFacadeHom e;
import es.BBDDCaja.cajaelena.facade.Remote.clientsFacadeB ean;
/**
* @web.servlet name = "PedirConfClienteServlet"
* display-name = "PedirConfClienteServlet"
* @web.servlet-init-param name = "SCajaElena.jndi.context"
* value = "ejb/es.BBDDCaja.cajaelena.Home/clientsFacadeHome"
* @web.servlet-mapping "/doPedirConfCliente"
*/
public class PedirConfClienteServlet extends HttpServlet {
private boolean initialized;
private ArrayList initErrors;
private HttpServlet servlet;
private String RESULT_URL,name,lastname1,lastname2,adress,birthda y,phone;
private ArrayList errors;
private clientsFacadeHome clientHome;
/** Constructor for IntrodDatosClienteServlet
*/
public PedirConfClienteServlet(){
super();
}
public void doGet(
HttpServletRequest theRequest,
HttpServletResponse theResponse)
throws ServletException, IOException{
if(!initialized){
ViewDispatcher.displayErrors(theRequest, theResponse, initErrors);
}
try{
doPedirConfCliente(theRequest,theResponse);
}
catch (Exception ex){
//top-level exception handler
ViewDispatcher.displayError(theRequest, theResponse, ex.toString());
}
}
public void doPost(
HttpServletRequest theRequest,
HttpServletResponse theResponse)
throws ServletException, IOException{
doGet (theRequest,theResponse);
}
public void init(){
String SCajaElenaJNDIContext =
getServletConfig().getInitParameter("SCajaElenaJND IContext");
String errorMsg = null;
boolean hasError = false;
initErrors = new ArrayList();
if (SCajaElenaJNDIContext == null){
errorMsg = "Error: undefined servlet property \"SCajaElena.jndi.context\".";
log(errorMsg);
hasError = true;
initErrors.add(errorMsg);
}
if (!hasError){
try{
Controller(this,SCajaElenaJNDIContext);
}
catch (Exception ex){
log (ex.toString());
initErrors.add(ex.toString());
hasError = true;
}
}
if (!hasError){
initialized = true;
}
}
public void Controller(
HttpServlet theServlet,
String theJNDIName)
throws NamingException {
servlet = theServlet;
}
public void doPedirConfCliente(
HttpServletRequest theRequest,
HttpServletResponse theResponse){
System.out.println("#doPedirConfCliente");
try{
populate(theRequest);
//perform transaction using IntrodDatosCliente EJB
try{
theRequest.setAttribute("confname", name);
theRequest.setAttribute("conflastname1", lastname1);
theRequest.setAttribute("conflastname2", lastname2);
theRequest.setAttribute("confadress", adress);
theRequest.setAttribute("confphone", phone);
theRequest.setAttribute("confbirthday", birthday);
ViewDispatcher.includeRequest(theRequest, theResponse, RESULT_URL);
}
catch (Exception ex){
ViewDispatcher.displayError(theRequest, theResponse, "Error creating IntrodDatosCliente");
}
}
catch (NamingException e){
e.printStackTrace();
}
//terminate operation if errors
if (hasErrors()){
ViewDispatcher.displayErrors(theRequest, theResponse, getErrors());
}
}
public void populate (HttpServletRequest theRequest) throws NamingException{
int phoneint;
phoneint=(Integer.valueOf(phone)).intValue();
clientsDto clients = new clientsDto();
clients.setName(name);
clients.setFirstlastname(lastname1);
clients.setSecondlastname(lastname2);
clients.setAdress(adress);
clients.setTelephone(phoneint);
Context ctx = getInitialContext();
Object home = ctx.lookup("SCajaElenaClientsEjb");
clientHome = (clientsFacadeHome)home;
clientsFacade clientmethod = null;
clientmethod = clientHome.create();
//LA SIGUIENTE LINEA ES LA QUE ME DA ERROR:
clients = clientmethod.Add(clients);
}
public ArrayList getErrors(){
if (!hasErrors()){
errors = new ArrayList();
}
return errors;
}
public boolean hasErrors(){
return basicGetErrors()!=null;
}
private ArrayList basicGetErrors(){
return errors;
}
private Context getInitialContext() throws NamingException{
try{
//Get an InitialContext
return new InitialContext();
}
catch (NamingException ne){
log("We were unable to get a connection to the server with new InitialContext(). ");
log("Please make sure that the server is running.");
throw ne;
}
}
}
donde el clientsFacadeHome llama a un método del clientsFacade que es el siguiente:
public void Add (es.BBDDCaja.cajaelena.dto.clientsDto clientsdto)
throws java.rmi.RemoteException;
y ese método consiste en :
public void Add (clientsDto clientsdto)
{
clientsDao clientsdao = new clientsDao();
try
{
clientsdao.Create(clientsdto);
}
catch (SQLException e)
{
e.printStackTrace();
}
}
si me podéis ayudar os lo agradeceré. Un saludo
package es.BBDDCaja.cajaelena.Web;
import java.rmi.RemoteException;
import java.io.IOException;
import java.util.ArrayList;
import java.lang.*;
import javax.ejb.CreateException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import es.BBDDCaja.cajaelena.dto.clientsDto;
import es.BBDDCaja.cajaelena.facade.Home.clientsFacade;
import es.BBDDCaja.cajaelena.facade.Home.clientsFacadeHom e;
import es.BBDDCaja.cajaelena.facade.Remote.clientsFacadeB ean;
/**
* @web.servlet name = "PedirConfClienteServlet"
* display-name = "PedirConfClienteServlet"
* @web.servlet-init-param name = "SCajaElena.jndi.context"
* value = "ejb/es.BBDDCaja.cajaelena.Home/clientsFacadeHome"
* @web.servlet-mapping "/doPedirConfCliente"
*/
public class PedirConfClienteServlet extends HttpServlet {
private boolean initialized;
private ArrayList initErrors;
private HttpServlet servlet;
private String RESULT_URL,name,lastname1,lastname2,adress,birthda y,phone;
private ArrayList errors;
private clientsFacadeHome clientHome;
/** Constructor for IntrodDatosClienteServlet
*/
public PedirConfClienteServlet(){
super();
}
public void doGet(
HttpServletRequest theRequest,
HttpServletResponse theResponse)
throws ServletException, IOException{
if(!initialized){
ViewDispatcher.displayErrors(theRequest, theResponse, initErrors);
}
try{
doPedirConfCliente(theRequest,theResponse);
}
catch (Exception ex){
//top-level exception handler
ViewDispatcher.displayError(theRequest, theResponse, ex.toString());
}
}
public void doPost(
HttpServletRequest theRequest,
HttpServletResponse theResponse)
throws ServletException, IOException{
doGet (theRequest,theResponse);
}
public void init(){
String SCajaElenaJNDIContext =
getServletConfig().getInitParameter("SCajaElenaJND IContext");
String errorMsg = null;
boolean hasError = false;
initErrors = new ArrayList();
if (SCajaElenaJNDIContext == null){
errorMsg = "Error: undefined servlet property \"SCajaElena.jndi.context\".";
log(errorMsg);
hasError = true;
initErrors.add(errorMsg);
}
if (!hasError){
try{
Controller(this,SCajaElenaJNDIContext);
}
catch (Exception ex){
log (ex.toString());
initErrors.add(ex.toString());
hasError = true;
}
}
if (!hasError){
initialized = true;
}
}
public void Controller(
HttpServlet theServlet,
String theJNDIName)
throws NamingException {
servlet = theServlet;
}
public void doPedirConfCliente(
HttpServletRequest theRequest,
HttpServletResponse theResponse){
System.out.println("#doPedirConfCliente");
try{
populate(theRequest);
//perform transaction using IntrodDatosCliente EJB
try{
theRequest.setAttribute("confname", name);
theRequest.setAttribute("conflastname1", lastname1);
theRequest.setAttribute("conflastname2", lastname2);
theRequest.setAttribute("confadress", adress);
theRequest.setAttribute("confphone", phone);
theRequest.setAttribute("confbirthday", birthday);
ViewDispatcher.includeRequest(theRequest, theResponse, RESULT_URL);
}
catch (Exception ex){
ViewDispatcher.displayError(theRequest, theResponse, "Error creating IntrodDatosCliente");
}
}
catch (NamingException e){
e.printStackTrace();
}
//terminate operation if errors
if (hasErrors()){
ViewDispatcher.displayErrors(theRequest, theResponse, getErrors());
}
}
public void populate (HttpServletRequest theRequest) throws NamingException{
int phoneint;
phoneint=(Integer.valueOf(phone)).intValue();
clientsDto clients = new clientsDto();
clients.setName(name);
clients.setFirstlastname(lastname1);
clients.setSecondlastname(lastname2);
clients.setAdress(adress);
clients.setTelephone(phoneint);
Context ctx = getInitialContext();
Object home = ctx.lookup("SCajaElenaClientsEjb");
clientHome = (clientsFacadeHome)home;
clientsFacade clientmethod = null;
clientmethod = clientHome.create();
//LA SIGUIENTE LINEA ES LA QUE ME DA ERROR:
clients = clientmethod.Add(clients);
}
public ArrayList getErrors(){
if (!hasErrors()){
errors = new ArrayList();
}
return errors;
}
public boolean hasErrors(){
return basicGetErrors()!=null;
}
private ArrayList basicGetErrors(){
return errors;
}
private Context getInitialContext() throws NamingException{
try{
//Get an InitialContext
return new InitialContext();
}
catch (NamingException ne){
log("We were unable to get a connection to the server with new InitialContext(). ");
log("Please make sure that the server is running.");
throw ne;
}
}
}
donde el clientsFacadeHome llama a un método del clientsFacade que es el siguiente:
public void Add (es.BBDDCaja.cajaelena.dto.clientsDto clientsdto)
throws java.rmi.RemoteException;
y ese método consiste en :
public void Add (clientsDto clientsdto)
{
clientsDao clientsdao = new clientsDao();
try
{
clientsdao.Create(clientsdto);
}
catch (SQLException e)
{
e.printStackTrace();
}
}
si me podéis ayudar os lo agradeceré. Un saludo
