다크호스 프로그래머javaweb(servletcontext)

4377 단어 servletContext
----------------------------
android 교육
4java 교육 당신과 교류하기를 기대합니다! ----------------------------
1. servletContext 대상을 가져오는 두 가지 방법
ServletContext context = this.getServletConfig().getServletContext();
		ServletContext context1 = this.getServletContext();

2.context 대상으로 데이터 공유 실현
context.setAttribute("data", "aaaaaaaaaa");

3. 서브렛Context의 공유 데이터 가져오기
context.getAttribute("data")

4.servletContext를 통해 웹 응용 프로그램에 대한 초기화 파라미터를 가져옵니다
		String url = this.getServletContext().getInitParameter("url");
		String username = this.getServletContext().getInitParameter("username");
		String password = this.getServletContext().getInitParameter("password");
  <context-param>
  	<param-name>url</param-name>
  	<param-value>jdbc:mysql://localhost:3306/test</param-value>
  </context-param>
  
  <context-param>
  	<param-name>username</param-name>
  	<param-value>root</param-value>
  </context-param>
  
  <context-param>
  	<param-name>password</param-name>
  	<param-value>root</param-value>
  </context-param>

5.servletContext를 통해 파일의 Mime 유형 가져오기
String filename = "1.jpg";
		
		ServletContext context = this.getServletContext();
		System.out.println(context.getMimeType(filename));

6.servletContext를 통해 전송 요청
//servlet , jsp 
		String data = "aaaaaa";
		this.getServletContext().setAttribute("data", data);
		RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/view.jsp");
		rd.forward(request, response);

구성 파일을 읽는 다양한 방법:
방법1:

		ServletContext context = this.getServletContext();
		InputStream in = context.getResourceAsStream("/db.properties");
		
		Properties prop = new Properties();  //map
		prop.load(in);
		
		
		String url = prop.getProperty("url");
		String username = prop.getProperty("username");
		String password = prop.getProperty("password");
		
		System.out.println(url);
		System.out.println(username);
		System.out.println(password);

방법2:

ServletContext context = this.getServletContext();
		String realpath = context.getRealPath("/db.properties");  //c:\\sdsfd\sdf\db.properties
		
		//    realpath=abc.properties
		String filename = realpath.substring(realpath.lastIndexOf("\\")+1);
		System.out.println(" :" + filename);
		
		
		FileInputStream in = new FileInputStream(realpath);
		Properties prop = new Properties();
		prop.load(in);
		
		String url = prop.getProperty("url");
		String username = prop.getProperty("username");
		String password = prop.getProperty("password");
		
		System.out.println(" :");
		System.out.println(url);
		System.out.println(username);
		System.out.println(password);

방법 3:
ServletContext context = this.getServletContext();
		URL url = context.getResource("/resource/db.properties");
		InputStream in = url.openStream();

메서드 4:

InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
		Properties prop = new Properties();  //map
		prop.load(in);
		
		
		String url = prop.getProperty("url");
		String username = prop.getProperty("username");
		String password = prop.getProperty("password");
		
		System.out.println(url);
		System.out.println(username);
		System.out.println(password);

좋은 웹페이지 즐겨찾기