0

im getting this warning at the startup of apache. Im im trying to set a session attribute "productos" with a HashMap<Integer,webservice.DataProd>. I tried making DataProd implemen Serializable, but stills the same, so since im new with webservices i have no idea what to do.

Here is the DataProd

package Producto;

import java.io.Serializable;
import java.util.Objects;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;

@XmlAccessorType(XmlAccessType.FIELD)
public class DataProd{
    private String nombre;
    private String descripcion;
    private String especificacion;
    private int id;
    private double precio;
    private int cantidadVendidos;

    public DataProd(String nom, String desc, double costo, int identificador, String esp, int cantidad){
        nombre = nom;
        descripcion = desc;
        precio = costo;
        id = identificador;
        especificacion = esp;
        cantidadVendidos = cantidad;
    } 

    public DataProd(){}

    public String getNombre(){
        return nombre;
    }

    public String getDescripcion(){
        return descripcion;
    }

    public double getPrecio(){
        return precio;
    }

    public int getCantidadVendidos(){
        return cantidadVendidos;
    }

    public int getId(){
        return id;
    }
     @Override
    public String toString(){
        return getNombre() + " - " + getId();
    }
    @Override
    public boolean equals(Object d){
        DataProd s = (DataProd) d;
        return ((getNombre().equals(s.getNombre()))&&(getDescripcion().equals(s.getDescripcion()))&&(getId() == s.getId())&&(getPrecio()==s.getPrecio()));
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 11 * hash + Objects.hashCode(this.getNombre());
        hash = 11 * hash + Objects.hashCode(this.getDescripcion());
        hash = 11 * hash + this.getId();
        hash = 11 * hash + (int) (Double.doubleToLongBits(this.getPrecio()) ^ (Double.doubleToLongBits(this.getPrecio()) >>> 32));
        return hash;
    }

    /**
     * @param nombre the nombre to set
     */
    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    /**
     * @param descripcion the descripcion to set
     */
    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }

    /**
     * @return the especificacion
     */
    public String getEspecificacion() {
        return especificacion;
    }

    /**
     * @param especificacion the especificacion to set
     */
    public void setEspecificacion(String especificacion) {
        this.especificacion = especificacion;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * @param precio the precio to set
     */
    public void setPrecio(double precio) {
        this.precio = precio;
    }

    /**
     * @param cantidadVendidos the cantidadVendidos to set
     */
    public void setCantidadVendidos(int cantidadVendidos) {
        this.cantidadVendidos = cantidadVendidos;
    }
}

And here is my stack:

WARNING: Cannot serialize session attribute productos for session 5D0658D2DFB89594B8F8D19FAC54C66B
java.io.NotSerializableException: webservice.DataProd
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
    at java.util.HashMap.writeObject(HashMap.java:1100)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:975)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1480)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
    at org.apache.catalina.session.StandardSession.writeObject(StandardSession.java:1671)
    at org.apache.catalina.session.StandardSession.writeObjectData(StandardSession.java:1077)
    at org.apache.catalina.session.StandardManager.doUnload(StandardManager.java:432)
    at org.apache.catalina.session.StandardManager.unload(StandardManager.java:353)
    at org.apache.catalina.session.StandardManager.stopInternal(StandardManager.java:518)
    at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
    at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5622)
    at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
    at org.apache.catalina.manager.ManagerServlet.undeploy(ManagerServlet.java:1359)
    at org.apache.catalina.manager.ManagerServlet.doGet(ManagerServlet.java:380)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:611)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1852)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)

Thank you, and sorry for my english level.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
DemianArdus
  • 827
  • 8
  • 20
  • possible duplicate of http://stackoverflow.com/q/6893833/1065197 – Luiggi Mendoza Oct 26 '13 at 20:02
  • Note, the question targets to Tomcat 7, the currently accepted answer doesn't explain the reason, but the second most voted does. – Luiggi Mendoza Oct 26 '13 at 20:03
  • i read that post before publishing here, but i couldnt solve my problem with that info. – DemianArdus Oct 26 '13 at 20:05
  • You should first understand that **it is not a problem**. An error **is** a problem, a warning **may** be. As explained in @pap's answer, you're storing a non-serializable object in web application session, so when Tomcat tries to serialize the session to disk, it will fail since there's a non-serializable object tried to be serialized. Looks like you haven't read that answer, and if you did, you didn't apply the solutions proposed in *how to remedy* section. – Luiggi Mendoza Oct 26 '13 at 20:10
  • By the way, you can just ignore these messages since **they won't harm** **nor** your web application **nor** your web service **nor** your tomcat. – Luiggi Mendoza Oct 26 '13 at 20:12
  • I know, the web runs just fine, and there is no problem, but i just want to make the WARNING go. I read the solutions proposed, and i wanted to apply the second one, so i added implements Serializable, but it didnt fix it. Thats why i asked here, to see if anyone could give me an accurate way to make the DataProd Serializable. maybe its not enough just adding the implements sentence. – DemianArdus Oct 26 '13 at 20:16
  • There are three proposed solutions. IMO you should try the last one: *Don't have Tomcat serializing sessions*, and if you read carefully in the same paragraph: *This might be the best course of action for you, unless you really need sessions to be persisted across restarts*. – Luiggi Mendoza Oct 26 '13 at 20:18
  • Acctually, i don really need them to be persisted, so that could be a solution. Thank you! And sorry, i didnt understand that answer couse of my bad english :/ – DemianArdus Oct 26 '13 at 20:29
  • Don't worry. You're welcome. – Luiggi Mendoza Oct 26 '13 at 20:30

1 Answers1

1

Change DataProd to implement Serializable

public class DataProd implements Serializable {
jmj
  • 237,923
  • 42
  • 401
  • 438