JAVA 개발 메뉴얼
0. 환경
- Eclipse EE Luna(v4.4.2) or Latest version
- Tomcat7
1. 프로젝트 생성
-
Eclipse 실행 후 File > New > Project를 선택하여 프로젝트를 생성합니다.
-
Dynamic Web Project를 선택합니다.
-
Project name을 입력하고 Finish를 눌러 마칩니다.
2. jsp 코딩
-
생성한 프로젝트의 WebContents 폴더하위에 sample.jsp 생성합니다.
-
sample.jsp를 다음과 같이 코딩합니다.
-
stringUrl에 사용할 API주소를 입력합니다.
source code
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.io.*" %> <%@ page import="java.net.*" %> <%@ page import="javax.xml.*" %> <%@ page import="javax.xml.parsers.*" %> <%@ page import="org.w3c.dom.*" %> <% URL url = null; URLConnection urlConnection = null; String stringUrl = "http://www.sample.kr/openapi/SttsApiTblData.do?STATBL_ID=T186503126543136&DTACYCLE_CD=QY&WRTTIME_IDTFR_ID=201704&Type=json"; InputStream is = null; String data = ""; try { url = new URL(stringUrl); urlConnection = url.openConnection(); urlConnection.setDoOutput(true); is = urlConnection.getInputStream(); byte[] buf = new byte[2048]; int len = -1; StringBuffer sb = new StringBuffer(); while ((len = is.read(buf, 0, buf.length)) != -1) { sb.append(new String(buf, 0, len)); } data = sb.toString(); } catch (MalformedURLException e) { e.getMessage(); } catch (IOException e) { e.getMessage(); } finally { if (is != null) { is.close(); } } %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> java sample page <%=data%>; </body> </html>
3. 배포
-
File > New > Other를 선택하여 서버를 생성합니다.
-
server를 선택하고 다음단계로 넘어갑니다.
-
Tomcat v7.0 Server를 선택하고 다음단계로 넘어갑니다.
-
Available영역에 있는 프로젝트를 선택하고 Add를 눌러 Configured영역으로 이동시킵니다.
-
Servers 탭에 새로운 서버가 생성되었습니다.
-
생성한 서버를 더블클릭하여 설정화면으로 들어갑니다. 아래에서 Modules탭을 선택합니다. 대상을 선택하고 Edit버튼을 클릭합니다.
-
Path에 "/"를 입력합니다.
-
Servers에서 서버를 선택하고 마우스우클릭 > Start로 실행시킵니다.
4. 확인
-
브라우저 주소창에 http://localhost:8080/sample.jsp를 입력하고 결과를 확인합니다.