<%@page import="java.io.*"%>
<%@page import="java.net.URL"%>
<%@ page contentType="text/html; charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>ServletContext를 이용한 자원 읽기1</title>
</head>
<body>
<h3>ServletContext를 이용한 자원 읽기1</h3>
<hr>
<%-- ServletContext타입의 jsp내장객체명은 application
ServletContext 객체를 사용해서 웹 컨텍스트 아래 파일 자원 내용 읽어오기 --%>
소스참조.txt의 실제경로 :
<%=application.getRealPath("/소스참조.txt")%><br> 소스참조.txt의 내용 :
<br>
<%
FileReader fr = null;
char[] buff = new char[512];
int len = -1;
BufferedReader br = null;
InputStreamReader isr = null;
try {
/* fr = new FileReader(application.getRealPath("/소스참조.txt"));
while( (len = fr.read(buff)) != -1 ){
out.println(new String(buff, 0, len));
} */
isr = new InputStreamReader(new FileInputStream(application.getRealPath("/소스참조.txt")));
br = new BufferedReader(isr);
String st = null;
while ((st = br.readLine()) != null) {
out.println(st + "<br>");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fr != null)
fr.close();
} catch (Exception e) {
e.printStackTrace();
}
}
%>
</body>
</html>
----------------------------- 요렇게로도 할 수 있어요!!!--------------------------
위 소스에서 변경된 부분에 컬러 넣었어요~~~~
<%@page import="java.io.*"%>
<%@page import="java.net.URL"%>
<%@ page contentType="text/html; charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>ServletContext를 이용한 자원 읽기2</title>
</head>
<body>
<h3>ServletContext를 이용한 자원 읽기2</h3>
<hr>
<!-- ServletContext타입의 jsp내장객체명은 application
ServletContext 객체를 사용해서 웹 컨텍스트 아래 파일 자원 내용 읽어오기 -->
<!-- 변수 선언 -->
<% String resourcePath="/소스참조.txt" ; %>
<font color="blue">
소스참조.txt의 실제경로 :<%=application.getRealPath(resourcePath)%><br>
소스참조.txt의 내용 : <br>
</font>
<%
BufferedReader br = null;
InputStreamReader isr = null;
try {
isr = new InputStreamReader(application.getResourceAsStream(resourcePath));
br = new BufferedReader(isr);
String st = null;
while ((st = br.readLine()) != null) {
out.println(st + "<br>");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (br != null) br.close();
if (isr != null) isr.close();
} catch (Exception e) {
e.printStackTrace();
}
}
%>
</body>
</html>
----------------------------- 요렇게로도 할 수 있어요!!!--------------------------
<%@page import="java.io.*"%>
<%@page import="java.net.URL"%>
<%@ page contentType="text/html; charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>ServletContext를 이용한 자원 읽기3</title>
</head>
<body>
<h3>ServletContext를 이용한 자원 읽기3</h3>
<hr>
<!-- ServletContext타입의 jsp내장객체명은 application
ServletContext 객체를 사용해서 웹 컨텍스트 아래 파일 자원 내용 읽어오기 -->
<!-- 변수 선언 -->
<% String resourcePath="/소스참조.txt" ; %>
<font color="blue">
소스참조.txt의 실제경로 :<%=application.getRealPath(resourcePath)%><br>
소스참조.txt의 내용 : <br>
</font>
<%
BufferedReader br = null;
InputStreamReader isr = null;
try {
URL url = application.getResource(resourcePath);
isr = new InputStreamReader(url.openStream());
br = new BufferedReader(isr);
String st = null;
while ((st = br.readLine()) != null) {
out.println(st + "<br>");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (br != null) br.close();
if (isr != null) isr.close();
} catch (Exception e) {
e.printStackTrace();
}
}
%>
</body>
</html>
'2020년도 이전 > [WebSig] JSP_Servlet' 카테고리의 다른 글
Expression Language (EL) (0) | 2013.08.06 |
---|---|
<jsp:include> 액션 태그 (0) | 2013.08.02 |
pageContext 기본 객체 (0) | 2013.08.01 |
<%= %>, <% %>, ${} 표현식, 스크립트릿 (0) | 2013.08.01 |
요청 전달 ==> 2. sendRedirect방식 (0) | 2013.07.31 |