본문 바로가기

콩's EDUCATION/콩's JSP&SERVLET

웹 컨텍스트 아래 파일 자원 내용 읽어오기

ApplicationTest2.jsp

 

<%@page import="java.io.*"%>
<%@ 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>Insert title here</title>
</head>
<body>
<h3>servlet Context Testg</h3>
<%-- 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){}
 }
%>
    
</body>
</html>

 

ApplicationTest3.jsp

 

<%@page import="java.io.*"%>
<%@ 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>Insert title here</title>
</head>
<body>
<h3>ServletContext를 이용한 자원 읽기</h3>
<%-- 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(isr!=null)isr.close();
   if(br!=null)br.close();
   }catch(Exception e){}
 }
%>

 

ApplicationTest4.jsp

 

<%@page import="java.net.URL"%>
<%@page import="java.io.*"%>
<%@ 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>Insert title here</title>
</head>
<body>
<h3>ServletContext를 이용한 자원 읽기</h3>
<%-- ServletContext 타입의 jsp 내장 객체명은 application ServletContext 객체를 사용해서 웹 컨텍스트 아래 파일 자원 내용 읽어오기 --%>
<% String resourcePath = "/소스참조.txt"; %>
<font color="RED">
소스 참조.txt의 실제 url경로 : <%=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(isr!=null)isr.close();
   if(br!=null)br.close();
   }catch(Exception e){}
 }
%>
    
</body>
</html>


    
</body>
</html>