Foros del Web » Programación para mayores de 30 ;) » Java »

problema con dto en servlet

Estas en el tema de problema con dto en servlet en el foro de Java en Foros del Web. 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 ...
  #1 (permalink)  
Antiguo 12/03/2009, 09:43
 
Fecha de Ingreso: diciembre-2008
Mensajes: 233
Antigüedad: 15 años, 4 meses
Puntos: 1
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
  #2 (permalink)  
Antiguo 13/03/2009, 02:45
Avatar de elAntonie  
Fecha de Ingreso: febrero-2007
Mensajes: 894
Antigüedad: 17 años, 2 meses
Puntos: 10
Respuesta: problema con dto en servlet

Wenas

El error tiene pinta de ir por ahi. Ahora bien, bola de cristal no tenemos.

Coloca el codigo del servlet y la llamada, para que podamos echarte una mano.

Saludos.
  #3 (permalink)  
Antiguo 13/03/2009, 08:12
 
Fecha de Ingreso: diciembre-2008
Mensajes: 233
Antigüedad: 15 años, 4 meses
Puntos: 1
Respuesta: problema con dto en servlet

Hola, este es el código de mi servlet:

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

Elena
  #4 (permalink)  
Antiguo 13/03/2009, 09:17
Avatar de drac94  
Fecha de Ingreso: mayo-2008
Ubicación: México
Mensajes: 383
Antigüedad: 15 años, 11 meses
Puntos: 5
Respuesta: problema con dto en servlet

Primero aqui le dices que lo que te regrese la funcion Add se lo asigne a clients
Código PHP:
clients clientmethod.Add(clients); 
Pero tu metodo Add no regresa ningun dato, o para ser mas especificos, regresa un dato de tipo void
Código PHP:
public void Add (clientsDto clientsdto)
{
    
clientsDao clientsdao = new clientsDao();
    try
    {
        
clientsdao.Create(clientsdto);
    }
    catch (
SQLException e)
    {
        
e.printStackTrace();
    }

Por eso te marca un error de inconmpatibilidad de tipos ya que tu variable clients es de tipo ClientsDto y el tipo que regresa tu funcion es void, Para que le asignas a clients el metodo add?? la verdad no revise el codigo para ver que es lo que hace, pero no se me hace muy buena idea, bueno checa eso y si nos puedes explicar a grandes rasgos que es lo que intentas hacer, tal ves te podramos ayudar mas, bye, saludos
  #5 (permalink)  
Antiguo 16/03/2009, 09:38
 
Fecha de Ingreso: diciembre-2008
Mensajes: 233
Antigüedad: 15 años, 4 meses
Puntos: 1
Respuesta: problema con dto en servlet

Hola, lo que yo quería era mediante el método Add añadir un registro en mi bbdd, y poder acceder a él porque luego quiero mostrar la información de ese cliente en la página web siguiente. Tenías razón en lo de asignar a clients = el resultado del método add, que es void, al final lo he solucionado quitando esa parte y utilizando try-catch, en la forma que sigue:

...
Context ctx = getInitialContext();
Object home = ctx.lookup("SCajaElenaClientsEjb");
clientHome = (clientsFacadeHome)home;
clientsFacade clientmethod = null;

try {
clientmethod = clientHome.create();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CreateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
clientmethod.Add(clients);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
...

Muchas gracias por la ayuda, un saludo
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 16:04.