본문 바로가기

Programming/Java

[Beginner] HttpServletRequest - HashMap, List Convert

/*************************************************************************
 Request를 HashMap에 저장
*************************************************************************/
public HashMap getRequestToMap(HttpServletRequest httpservletrequest) {
        String strKey = "";
        HashMap map = new HashMap();
 
        for ( Enumeration e = httpservletrequest.getParameterNames(); e.hasMoreElements(); ) {
                strKey = (String)e.nextElement();
                map.put(strKey, httpservletrequest.getParameter(strKey));
        }

        return map;
}

/*************************************************************************
 Request를 ArrayList에 저장
*************************************************************************/
public List getRequestToList(HttpServletRequest httpservletrequest) {
        int nMaxCnt = 1;
        String strKey = "";
        List reqList = new ArrayList();
        Object[] obj = null;
        HashMap map = null;

        for ( int i = 0 ; i < nMaxCnt ; i++ ) {
                map = new HashMap();

                for ( Enumeration e = httpservletrequest.getParameterNames(); e.hasMoreElements(); ) {
                        strKey = (String)e.nextElement();

                        obj = httpservletrequest.getParameterValues(strKey);

                        if ( nMaxCnt < obj.length ) {
                                nMaxCnt = obj.length;
                        }

                        if ( nMaxCnt >= i + 1 ) {
                                map.put(strKey,  obj[i]);
                        }
                }

                reqList.add(map);
        }

        return reqList;
}

'Programming > Java' 카테고리의 다른 글

[Log4j] Logger Properties 설정  (0) 2010.03.04
[Beginner] Text Read  (0) 2010.01.28
[Beginner] Excel Read - POI  (0) 2010.01.28
[Secret] Encrypt / Decrypt  (0) 2010.01.19
[Beginner] HashMap, List GetDataString  (0) 2010.01.19