K 개발자

JSP BookMarket 만들기 - 11 본문

Web/JSP

JSP BookMarket 만들기 - 11

ddingz 2021. 8. 6. 18:12

이전 글
JSP BookMarket 만들기 - 1
JSP BookMarket 만들기 - 2
JSP BookMarket 만들기 - 3
JSP BookMarket 만들기 - 4
JSP BookMarket 만들기 - 5
JSP BookMarket 만들기 - 6
JSP BookMarket 만들기 - 7
JSP BookMarket 만들기 - 8
JSP BookMarket 만들기 - 9
JSP BookMarket 만들기 - 10

실행 환경
자바 개발 환경 : jdk-15.0.2
웹 서버 환경 : Apache Tomcat v9.0
통합 개발 환경 : Eclipse IDE for Enterprise Java Developers

로그 기록하기

필터 처리로 로그 기록을 작성한다.

  • src 폴더에 filter 패키지를 생성한 후 Filter 인터페이스의 구현 클래스 LogFilter.java 파일을 생성하고 로그 기록을 위한 필터 처리를 작성한다.
  • WebContent/WEB-INF 폴더의 web.xml 파일에 필터 구성을 작성한다.
// LogFilter.java

package filter;

import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class LogFilter implements Filter {

	public void init(FilterConfig config) throws ServletException {
		System.out.println("BookMarket 초기화...");
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws java.io.IOException, ServletException {
		System.out.println(" 접속한 클라이언트 IP : " + request.getRemoteAddr());
		long start = System.currentTimeMillis();
		System.out.println(" 접근한 URL 경로 : " + getURLPath(request));
		System.out.println(" 요청 처리 시작 시각 : " + getCurrentTime());
		chain.doFilter(request, response);

		long end = System.currentTimeMillis();
		System.out.println(" 요청 처리 종료 시각 : " + getCurrentTime());
		System.out.println(" 요청 처리 소요 시간 : " + (end - start) + "ms ");
		System.out.println("==================================================");
	}

	public void destroy() {
	}

	private String getURLPath(ServletRequest request) {
		HttpServletRequest req;
		String currentPath = "";
		String queryString = "";
		if (request instanceof HttpServletRequest) {
			req = (HttpServletRequest) request;
			currentPath = req.getRequestURI();
			queryString = req.getQueryString();
			queryString = queryString == null ? "" : "?" + queryString;
		}
		return currentPath + queryString;
	}

	private String getCurrentTime() {
		DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(System.currentTimeMillis());
		return formatter.format(calendar.getTime());
	}
}
<?xml version="1.0" encoding="UTF-8"?>
<!-- web.xml -->

<web-app>
	<security-role>
		<description></description>
		<role-name>admin</role-name>
	</security-role>
	<security-constraint>
		<display-name>BookMarket Security</display-name>
		<web-resource-collection>
			<web-resource-name>BookMarket</web-resource-name>
			<description></description>
			<url-pattern>/addBook.jsp</url-pattern>
		</web-resource-collection>
		<auth-constraint>
			<description>권한 관리자명</description>
			<role-name>admin</role-name>
		</auth-constraint>
	</security-constraint>
	<login-config>
		<auth-method>FORM</auth-method>
		<form-login-config>
			<form-login-page>/login.jsp</form-login-page>
			<form-error-page>/login_failed.jsp</form-error-page>
		</form-login-config>
	</login-config>
	<error-page>
		<error-code>404</error-code>
		<location>/exceptionNoPage.jsp</location>
	</error-page>
	<filter>
		<filter-name>LogFilter</filter-name>
		<filter-class>filter.LogFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>LogFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

필터 처리로 로그 기록 파일을 만든다.

  • src 폴더의 filter 패키지에 Filter 인터페이스의 구현 클래스 LogFileFilter.java 파일을 생성하고 로그 기록을 위한 필터 처리를 작성한다.
  • WebContent/WEB-INF 폴더의 web.xml 파일에 필터 구성 및 로그 파일명 bookmarket.log를 작성한다.
// LogFileFilter.java

package filter;

import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;

public class LogFileFilter implements Filter {

	PrintWriter writer;

	public void init(FilterConfig config) throws ServletException {
		String filename = config.getInitParameter("filename");

		if (filename == null)
			throw new ServletException("로그 파일의 이름을 찾을 수 없습니다.");

		try {
			writer = new PrintWriter(new FileWriter(filename, true), true);
		} catch (IOException e) {
			throw new ServletException("로그 파일을 열 수 없습니다.");
		}
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws java.io.IOException, ServletException {
		writer.println(" 접속한 클라이언트 IP : " + request.getRemoteAddr());
		long start = System.currentTimeMillis();
		writer.println(" 접근한 URL 경로 : " + getURLPath(request));
		writer.println(" 요청 처리 시작 시각 : " + getCurrentTime());

		chain.doFilter(request, response);

		long end = System.currentTimeMillis();
		writer.println(" 요청 처리 종료 시각 : " + getCurrentTime());
		writer.println(" 요청 처리 소요 시간 : " + (end - start) + "ms ");
		writer.println("==================================================");
	}

	public void destroy() {
		writer.close();
	}

	private String getURLPath(ServletRequest request) {
		HttpServletRequest req;
		String currentPath = "";
		String queryString = "";
		if (request instanceof HttpServletRequest) {
			req = (HttpServletRequest) request;
			currentPath = req.getRequestURI();
			queryString = req.getQueryString();
			queryString = queryString == null ? "" : "?" + queryString;
		}
		return currentPath + queryString;
	}

	private String getCurrentTime() {
		DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(System.currentTimeMillis());
		return formatter.format(calendar.getTime());
	}
}
<?xml version="1.0" encoding="UTF-8"?>
<!-- web.xml -->

<web-app>
	<security-role>
		<description></description>
		<role-name>admin</role-name>
	</security-role>
	<security-constraint>
		<display-name>BookMarket Security</display-name>
		<web-resource-collection>
			<web-resource-name>BookMarket</web-resource-name>
			<description></description>
			<url-pattern>/addBook.jsp</url-pattern>
		</web-resource-collection>
		<auth-constraint>
			<description>권한 관리자명</description>
			<role-name>admin</role-name>
		</auth-constraint>
	</security-constraint>
	<login-config>
		<auth-method>FORM</auth-method>
		<form-login-config>
			<form-login-page>/login.jsp</form-login-page>
			<form-error-page>/login_failed.jsp</form-error-page>
		</form-login-config>
	</login-config>
	<error-page>
		<error-code>404</error-code>
		<location>/exceptionNoPage.jsp</location>
	</error-page>
	<filter>
		<filter-name>LogFilter</filter-name>
		<filter-class>filter.LogFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>LogFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<filter>
		<filter-name>LogFileFilter</filter-name>
		<filter-class>filter.LogFileFilter</filter-class>
		<init-param>
			<param-name>filename</param-name>
			<param-value>c:\\logs\\bookmarket.log</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>LogFileFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

웹 브라우저에 'http://localhost:8080/BookMarket/books.jsp'를 입력하여 실행 결과를 확인한다.

다음 글
JSP BookMarket 만들기 - 12
JSP BookMarket 만들기 - 13
JSP BookMarket 만들기 - 마지막

'Web > JSP' 카테고리의 다른 글

JSP BookMarket 만들기 - 13  (0) 2021.08.06
JSP BookMarket 만들기 - 12  (0) 2021.08.06
JSP BookMarket 만들기 - 10  (0) 2021.08.06
JSP BookMarket 만들기 - 9  (0) 2021.08.06
JSP BookMarket 만들기 - 8  (0) 2021.08.06
Comments