import java.io.InputStream; import java.util.Properties; import javax.servlet.ServletContext; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) { ServletContext context = getServletContext(); InputStream inputStream = context.getResourceAsStream("/WEB-INF/classes/my.properties"); Properties props = new Properties(); try { props.load(inputStream); String value = props.getProperty("myKey"); // do something with the value } catch (Exception e) { e.printStackTrace(); } } }
import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import javax.servlet.ServletContext; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) { ServletContext context = getServletContext(); InputStream inputStream = context.getResourceAsStream("/myfile.txt"); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line; try { while ((line = reader.readLine()) != null) { // do something with each line } } catch (Exception e) { e.printStackTrace(); } } }Package/library: javax.servlet