bharathi
bharathi

Reputation: 6271

Casting Problem in Servlet

I have a servlet program. This is my code:

public class CompanionProxy extends HttpServlet {
    DeviceDAOHibernateImpl daoImpl = null;
    Logger log = Logger.getLogger("CompanionProxy");
    public void init(){
        daoImpl = new DeviceDAOHibernateImpl();
        ProxyParser parser = ProxyParserFactory.getParser(ProxyParser.Type.XML);
        log.info("Config file Path "+parser.getClass().getName());
        ArrayList<Device> aDeviceList = parser.parse("c:\\proxy_setup_load.xml");//CommonConstants.CONFIG_FILE_PATH);
        for (Iterator iterator = aDeviceList.iterator(); iterator.hasNext();) {
            Device device = (Device) iterator.next();
            try {
                daoImpl.create(device);
            } catch (ProxyException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        try {
            List<Device> listDevices = daoImpl.list();
            for (Iterator iterator = listDevices.iterator(); iterator.hasNext();) {
                Device device = (Device) iterator.next();
                log.info(device.toString());
            }
        } catch (ProxyException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        @SuppressWarnings("unchecked")
        Map<String, String> map = request.getParameterMap();
        log.info(map.toString());
        HashMap<String, String> requestMap  = new HashMap<String, String>();
        requestMap.putAll(map);
        requestMap.put(CommonConstants.DEVICE_IP, request.getRemoteAddr());
        String reqType = requestMap.get(CommonConstants.REQ_PARAM);

        if (reqType.equals(CommonConstants.REGISTER_DEVICE)) {
            Device device = ProxyRequestParser.parseRegisterRequest(requestMap);
            try {
                daoImpl.create(device);
            } catch (ProxyException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else if (reqType.equals(CommonConstants.UNREGISTER_DEVICE)) {
            Device device;

            try {
                device = daoImpl.findByIPAddr(requestMap.get(CommonConstants.DEVICE_IP));
                daoImpl.delete(device);
            } catch (ProxyException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    // some code...

}

When I run this servlet it gives the following error:

java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String
com.nagra.proxy.servlet.CompanionProxy.doGet(CompanionProxy.java:78)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

What is the cause of this error, and how can I fix it?

Upvotes: 1

Views: 1952

Answers (1)

Joeri Hendrickx
Joeri Hendrickx

Reputation: 17445

Check the API. getParameterMap() returns Map<String, String[]>, not Map<String, String>.

When you retrieve an object from your cast map, you're implicitly casting it to String, while it should be String array.

Java expresses "array of type" as "[Ltype".

If you're sure your request parameters are singular (or are willing to make that assumption) just flatten them out by taking the first entry of the array.

HashMap<String, String> requestMap  = flatten(map);

...


public static Map<String, String> flatten(Map<String, String[]> arrayMap){
  Map<String, String> r = new HashMap<String, String>();
  for (Map.Entry<String, String[]> entry: arrayMap.entrySet()){
    String[] value = entry.getValue();
    if (value !=null && value .length>0) r.put(entry.getKey(), value[0]);
  }
  return r;
}

Upvotes: 12

Related Questions