📌 ServletContext

서블릿이 운영되는 컨텍스트와 서버(컨테이너)에 대한 정보를 가진 싱글턴 객체

  1. context parameter 확보 : 

<%=application.getInitParameter("imageFolder") %> <!-- application: 싱글톤 -->


  2. context 정보 확보 : 

<%=application.getContextPath() %>, <%=request.getContextPath() %>
<%=request.getServletContext().getContextPath() %>, <%=session.getServletContext().getContextPath() %>


  3. 서버의 정보 확보 

<%=application.getServerInfo() %> 
<%=application.getMajorVersion() %>.<%=application.getMinorVersion() %>


  4. 로그 기록

<%
 	application.log(
    String.format("%s - %s, %s",request.getRemoteAddr(),request.getMethod(), request.getRequestURI())
    );
 %>

 

5. 현재 컨텍스트의 웹리소스 확보에 활용(***)

<%
 		String imageURL = "/resources/images/cat1.jpg";
 		String realPath = application.getRealPath(imageURL);
 		out.println(realPath); // 이클립스상에서 이미지가 들어있는 경로 
 		//D:\A_TeachingMaterial\06_JSP_Spring\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\WebStudy01\resources\images\cat1.jpg
 		File imageFile = new File(realPath); // 1.
			
 		// 고양이 그림 복사하기!!
 			
 		String folderURL = "/11";
 		String folderPath  = application.getRealPath(folderURL);
 		out.println(folderPath);
 		//D:\A_TeachingMaterial\06_JSP_Spring\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\WebStudy01\11
 		File destFile = new File(folderPath, imageFile.getName());
 		out.println(destFile); // 파일이 복사될 경로
 		//D:\A_TeachingMaterial\06_JSP_Spring\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\WebStudy01\11\cat1.jpg
 			
 		Files.copy(imageFile.toPath(), destFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
					
 			
			 		
 %>

 

'웹기반 애플리케이션' 카테고리의 다른 글

Wrapper 클래스  (0) 2023.04.04
JSP 기본객체(3) PageContext, Scope  (0) 2023.03.26
JSP의 기본객체, cookie와 session  (0) 2023.03.15
예외 (Error, Exception)  (0) 2023.03.15
Http Response Packaging(2)  (0) 2023.03.10

+ Recent posts