하은양 믿음군 효실맘 호홍홍집s

한글인코딩확인방법2 본문

가벼운 배움/jsp

한글인코딩확인방법2

호홍홍집 2016. 6. 3. 15:31

한글인코딩을 사용시 매번 서버환경이 바뀔 때마다 확인하기 힘들다면...꼼수..ㅋㅋ

<form name="loginForm" id="loginForm" method="post" action ="" >
 <input name="encodingChk" type="hidden" value="한글인코딩 체크" />
 
 <input type="text" id="hangulName" name="hangulName" value="" />
 <input type="submit" value="로그인" /> 
</form>

++++++++  아래 수신받는 jsp 코드  ++++++++++
<%
String reqName = (request.getParameter("hangulName") == null) ? "" : request.getParameter("hangulName");
String encodingChk = (request.getParameter("encodingChk") == null) ? "" : request.getParameter("encodingChk");

// 한글인코딩 처리여부 확인....
//////////////////////////////////////////////////////////////////////////////
String charset[] = {"utf-8","ms949","euc-kr", "ksc5601", "iso-8859-1", "8859_1", "ascii"};
boolean chkOk = false;
for(int i = 0; i < charset.length; i++){
 if(chkOk) break;
 for(int j = 0; j < charset.length; j++){
  String chkEncodingHangul = ChangeEncoding(encodingChk, charset[i], charset[j]);
  // out.println("POST :: " + charset[i] + " --> " + charset[j] + " :::: " + chkEncodingHangul + "<br />");
  if(chkEncodingHangul.equals("한글인코딩 체크")){
   reqName = ChangeEncoding(reqName, charset[i], charset[j]);
   // out.println("당첨 :: " + charset[i] + " --> " + charset[j] + "<br />");
   chkOk = true;
   break;
  }
 }
}
%>


<%!
public String ChangeEncoding(String str, String incn, String outcn) {
        if (str == null) {
            return "";
        }
        try {
            str = new String(str.getBytes(incn), outcn);
        } catch (Exception ex) {
            System.out.println("Exception:" + ex.toString());
        }
        return str;
    }
%>