Servlet中的转换问题

时间:2011-07-27 14:33:47

标签: java servlets

我有一个servlet程序。这是我的代码:

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...

}

当我运行这个servlet时,它会出现以下错误:

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)

此错误的原因是什么,我该如何解决?

1 个答案:

答案 0 :(得分:12)

检查API。 getParameterMap()返回Map<String, String[]>,而不是Map<String, String>

当你从转换图中检索一个对象时,你会隐式地将它转换为String,而它应该是String数组。

Java将“数组类型”表示为“[Ltype”。

如果你确定你的请求参数是单数的(或者愿意做出这个假设),那么只需要取出数组的第一个条目就可以将它们弄平。

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;
}