728x90
반응형
18_01_filter02.jsp
18_01_filter02_process.jsp
18_01_filter02.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h3>회원가입</h3> <form action="18_01_filter02_process.jsp" method="post"> <p>아이디 : <input type="text" name="id"> <p>비밀번호 : <input type="text" name="passwd"> <input type="submit" value="전송"></p> </form> </body> </html>
18_01_filter02_process.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% String id = request.getParameter("id"); String passwd = request.getParameter("passwd"); %> <p> 입력된 id값 : <%=id %></p> <p> 입력된 pw값 : <%=passwd %></p> </body> </html>
package com.filter; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; import javax.servlet.annotation.WebInitParam; import javax.servlet.http.HttpFilter; //@WebFilter(filterName = "Filter02", urlPatterns = { "/18_01_filter02_process.jsp" }, initParams = { // @WebInitParam(name = "param1", value = "admin"), @WebInitParam(name = "param2", value = "1234") }) public class InitParamFilter implements Filter { private FilterConfig filterConfig = null; public void init(FilterConfig fConfig) throws ServletException { System.out.println("Filter02 초기화..."); this.filterConfig = fConfig; /* * param1 => admin, param2 =>1234 * */ } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("Filter02 수행..."); request.setCharacterEncoding("UTF-8"); String id = request.getParameter("id"); String passwd = request.getParameter("passwd"); String param1 = filterConfig.getInitParameter("param1"); String param2 = filterConfig.getInitParameter("param2"); String message; // PrintWriter writer = response.getWriter(); //앞에 기술 시 ??? 로 표기됨 response.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); PrintWriter writer = response.getWriter(); if(id.equals(param1) && passwd.equals(param2)) message = "로그인 성공했습니다."; else message = "로그인 실패했습니다."; writer.println(message); chain.doFilter(request, response); } public void destroy() { System.out.println("Filter02 해제.."); this.filterConfig =null; } }
package com.filter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; import javax.servlet.annotation.WebInitParam; import javax.servlet.http.HttpFilter; //@WebFilter(filterName = "Filter02_2", // urlPatterns = { "/18_01_filter02_process.jsp" }, // initParams = {@WebInitParam(name = "filename", value = "monitor.log")}) public class LogFileFilter implements Filter { PrintWriter writer; //null private String getCurrentTime() { DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.S"); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); return formatter.format(calendar.getTime()); } //init 웹서버가 시작될 때 실행됨. public void init(FilterConfig fConfig) throws ServletException { System.out.println("Filter02_2 초기화..."); String realpath = "C:\\log\\"; File f = new File(realpath); if(!f.exists()) {f.mkdirs();} String filename = fConfig.getInitParameter("filename"); //monitor.log if(filename==null) throw new ServletException("로그 파일의 이름을 찾을 수 없습니다."); try {writer = new PrintWriter(new FileWriter(realpath+filename, true), true);} //PrintWriter - 객체를 생성하면서 파일을 받으면 그 파일에 쓴다. //객체를 얻어올 때 response.. 겟라이터 catch(IOException e) {throw new ServletException("로그 파일을 열 수 없습니다.");} } //url-pattern 설정한 곳에 호출되었을 때 실행됨. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("Filter02_2 doFilter()..."); writer.printf("현재일시 : %s %n", getCurrentTime()); String clientAddr = request.getRemoteAddr(); writer.printf("클라이언트 주소 : %s %n", clientAddr); chain.doFilter(request, response); writer.printf("현재일시 : %s %n", getCurrentTime()); String contentType = response.getContentType(); writer.printf("문서의 콘텐츠 유형 : %s %n", contentType); writer.println("----------------------------------------"); } //웹서버가 종료되었을 때 실행됨. public void destroy() { writer.println("destroy()메소드 호출. 강제 해제"); writer.close(); } public LogFileFilter() { super(); } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0"> <display-name>jspthree</display-name> <!-- Filter02_2 --> <!-- 필터객체 생성 --> <filter> <filter-name>Filter02_2</filter-name> <filter-class>com.filter.LogFileFilter</filter-class> <init-param> <param-name>filename</param-name> <param-value>monitor.log</param-value> </init-param> </filter> <!-- 필터객체 사용 --> <filter-mapping> <filter-name>Filter02_2</filter-name> <url-pattern>/18_01_filter02_process.jsp</url-pattern> </filter-mapping> <!-- Filter02 --> <filter> <filter-name>Filter02</filter-name> <filter-class>com.filter.InitParamFilter</filter-class> <init-param> <param-name>param1</param-name> <param-value>admin</param-value> </init-param> <init-param> <param-name>param2</param-name> <param-value>1234</param-value> </init-param> </filter> <filter-mapping> <filter-name>Filter02</filter-name> <url-pattern>/18_01_filter02_process.jsp</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.jsp</welcome-file> <welcome-file>default.htm</welcome-file> </welcome-file-list> </web-app>
728x90
반응형
'[JAVA]' 카테고리의 다른 글
[JSP] API (0) | 2024.03.21 |
---|---|
Filter (0) | 2024.03.20 |
[JSP] cookies (0) | 2024.03.20 |
[JSP] session - getMaxInactiveInterval, getCreationTime, getLastAccessedTime (0) | 2024.03.20 |
[JSP] session - removeAttribute, invalidate (0) | 2024.03.20 |