--> -->

JSP

 

WEB-INF 폴더 아래   file 폴더 생성

file 폴더 아래 test.txt 파일 생성

 

test.txt 

test1, test2, test3

 

ContextFileServlet.java

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/cfile")
public class ContextFileServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html; charset=utf-8"); 
		PrintWriter out=response.getWriter();
		ServletContext context=getServletContext();
		InputStream is =context.getResourceAsStream("/WEB-INF/file/test.txt");
		BufferedReader buffer=new BufferedReader(new InputStreamReader(is));
		
		String menu=null;
		String one=null;
		String two=null;
		String three=null;
		while((menu=buffer.readLine())!=null){
			StringTokenizer tokens=new StringTokenizer(menu, ",");
			one=tokens.nextToken();
			two=tokens.nextToken();
			three=tokens.nextToken();
		}
		
		out.print("<html><body>");
		out.print(one+"<br>");
		out.print(two+"<br>");
		out.print(three+"<br>");
		out.close();			
	}
	
	
	
}

 

 

about author

PHRASE

Level 60  라이트

마을과 숲 속, 낮은 곳이나 높은 곳 어디든 성자가 머무시는 곳에는 기쁨이 있다. -법구경

댓글 ( 0)

댓글 남기기

작성

JSP 목록    more