본문 바로가기

study/java

restAPI짭퉁

<%@ page contentType="text/html; charset=UTF-8" %>
<%@page import="java.util.*"%>
<%@page import="java.net.*"%>
<%@page import="java.io.*"%> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

 
<%!
 
public String aPI004(String DI) throws Exception 
{

URL url = null;
    String readLine = null;
    StringBuilder buffer = null;
    OutputStream outputStream = null;
    BufferedReader bufferedReader = null;
    BufferedWriter bufferedWriter = null;
    HttpURLConnection urlConnection = null;
    
    int connTimeout = 1000;
    int readTimeout = 1000;


String sendData = ""; 
String DI_VAL = java.net.URLEncoder.encode(DI);
String apiUrl = "http://192.168.3.1:8080/api/semas/getAPI004.do?CI_DI_CD=DI&CI_DI_VAL="+DI_VAL;    // 각자 상황에 맞는 IP & url 사용 
 try 
    {
        url = new URL(apiUrl);
        
        urlConnection = (HttpURLConnection)url.openConnection();
        urlConnection.setRequestMethod("POST");
        urlConnection.setConnectTimeout(connTimeout);
        urlConnection.setReadTimeout(readTimeout);
        urlConnection.setRequestProperty("Content-Type", "application/json;");
        urlConnection.setDoOutput(true);
        urlConnection.setInstanceFollowRedirects(true);
        
        outputStream = urlConnection.getOutputStream();
        
        bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
        bufferedWriter.write(sendData);
        bufferedWriter.flush();
        
        buffer = new StringBuilder();
        if(urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) 
        {
            bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"UTF-8"));
            while((readLine = bufferedReader.readLine()) != null) 
            {
                buffer.append(readLine).append("\n");
            }
        }
        else 
        {
            buffer.append("\"code\" : \""+urlConnection.getResponseCode()+"\"");
            buffer.append(", \"message\" : \""+urlConnection.getResponseMessage()+"\"");
        }
    }
    catch(Exception ex) 
    {
     return null;
     //ex.printStackTrace();
    }
    finally 
    {
        try 
        {
            if (bufferedWriter != null) { bufferedWriter.close(); }
            if (outputStream != null) { outputStream.close(); }
            if (bufferedReader != null) { bufferedReader.close(); }
        }
        catch(Exception ex) 
        { 
            return null;
         // ex.printStackTrace();
 
        }
    }
 
  
return buffer.toString();
}
%>
<%!
 
public String aPI003(String DI) throws Exception 
{

URL url = null;
    String readLine = null;
    StringBuilder buffer = null;
    OutputStream outputStream = null;
    BufferedReader bufferedReader = null;
    BufferedWriter bufferedWriter = null;
    HttpURLConnection urlConnection = null;
    
    int connTimeout = 1000;
    int readTimeout = 1000;

 

String sendData = ""; 
String DI_VAL = java.net.URLEncoder.encode(DI);
String apiUrl = "http://192.168.220.52:8080/api/semas/getAPI003.do?CI_DI_CD=DI&CI_DI_VAL="+DI_VAL;    // 각자 상황에 맞는 IP & url 사용 
 try 
    {
        url = new URL(apiUrl);
        
        urlConnection = (HttpURLConnection)url.openConnection();
        urlConnection.setRequestMethod("POST");
        urlConnection.setConnectTimeout(connTimeout);
        urlConnection.setReadTimeout(readTimeout);
        urlConnection.setRequestProperty("Content-Type", "application/json;");
        urlConnection.setDoOutput(true);
        urlConnection.setInstanceFollowRedirects(true);
        
        outputStream = urlConnection.getOutputStream();
        
        bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
        bufferedWriter.write(sendData);
        bufferedWriter.flush();
        
        buffer = new StringBuilder();
        if(urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) 
        {
            bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"UTF-8"));
            while((readLine = bufferedReader.readLine()) != null) 
            {
                buffer.append(readLine).append("\n");
            }
        }
        else 
        {
            buffer.append("\"code\" : \""+urlConnection.getResponseCode()+"\"");
            buffer.append(", \"message\" : \""+urlConnection.getResponseMessage()+"\"");
        }
    }
    catch(Exception ex) 
    {
        return null;
    }
    finally 
    {
        try 
        {
            if (bufferedWriter != null) { bufferedWriter.close(); }
            if (outputStream != null) { outputStream.close(); }
            if (bufferedReader != null) { bufferedReader.close(); }
        }
        catch(Exception ex) 
        { 
            return null;
 
        }
    }
 return buffer.toString();
}
%>
 
<!DOCTYPE html>
<html>
<head>
</head>
<body>

<c:set var="DI" value="${discrhash}"/>
             
<script>

function viewData(){
    var dat2 = `<% out.println(aPI004((String)pageContext.getAttribute("DI")));%>`;
var dat1 = `<% out.println(aPI003((String)pageContext.getAttribute("DI")));%>`;
 
if(dat2.length==5){
htmls="<tr><td colspan='4' style='text-align:center'>데이터가 없습니다.</tr>";
$("#bodyContent").html(htmls);
<c:if test="${cntval ne ''}"> 
$(".myop1").html("<b>${cntval}</b>건"); 
</c:if>
return false;
}

var jkey1 = JSON.parse(dat1);
var jkey2 = JSON.parse(dat2);

 
var htmls="";
var dat = jkey2.data;
var dat2 = jkey1.data;
$(".cnts").text(dat.length); // 건수 출력
for(var i=0;i<dat.length;i++){ 
var tbl=dat[i]; 
var APLY_YMD=""; // 대출 신청일자
var GIVE_AMT="";//지급금액
for(var j=0;j<dat2.length;j++){
var tbl2 = dat2[j];
if(tbl.ENT_NM!=null && tbl2.ENT_NM!=null && tbl.ENT_NM==tbl2.ENT_NM && tbl2.SLCTN_YMD.substr(0,4)==tbl.SPRT_BIZ_YR){
APLY_YMD = tbl2.APLY_YMD;
GIVE_AMT = tbl2.GIVE_AMT;
}

}

var ENT_NM = tbl.ENT_NM;
if(ENT_NM=="null"|| ENT_NM==null){ ENT_NM="";}
var SPRT_BIZ_NM = tbl.SPRT_BIZ_NM;
if(GIVE_AMT==null){GIVE_AMT=""};

if(SPRT_BIZ_NM=="null"||SPRT_BIZ_NM==null){SPRT_BIZ_NM="";};
htmls +="<tr>"; 
htmls +="<td>"+ENT_NM+"</td>";
htmls +="<td>"+SPRT_BIZ_NM+"</td>";
htmls +="<td>"+APLY_YMD+"</td>"; 
htmls +="<td>"+GIVE_AMT+"</td>";

htmls +="</tr>";   
}

  $("#bodyContent").html(htmls);
  <c:if test="${cntval ne ''}">
  
  $(".myop1").html("<b>${cntval}</b>건");
   
  </c:if>
}
viewData();
</script>   



</body>
</html>