JSP/Servlet – 필터 체인의 방향 바꾸기
필터 클래스에서 로그인 여부를 체크하여 웹페이지의 방향을 결정하려면 sendRedirect 메소드나 forward 메소드를 이용해서
구현할 수 있습니다.
다음 예제는 로그인 여부를 체크하여 웹페이지를 다르게 보여줍니다.
예제는 위와 같이 구성됩니다. LoginCheckFilter.java 에서 필터 클래스를 구현합니다. 그리고 NameList.jsp와 Login.jsp, LoginForm.html는 테스트를 위한 파일입니다.
LoginCheckFilter.java
package myfilter;
import java.io.IOException;
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.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginCheckFilter implements Filter{
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletRequest httpRequest = (HttpServletRequest)req;
HttpServletResponse httpResponse = (HttpServletResponse)resp;
HttpSession session = httpRequest.getSession();
//아래는 로그인을 했는지 안했는지 체크를 하고
//로그인을 안했다면 로그인을 할 수 있게 로그인폼으로 넘겨줍니다.
if(session == null){
httpResponse.sendRedirect(
"http://localhost:8080/ch11_jsp_filter_chg/LoginForm.html");
}
String id = (String)session.getAttribute("ID");
if(id == null){
httpResponse.sendRedirect(
"http://localhost:8080/ch11_jsp_filter_chg/LoginForm.html");
}
chain.doFilter(req, resp);
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
}
WEB-INF/list/NameList.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
gakari님 로그인을 성공 하셨군요.
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ch11_jsp_filter_chg</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>login-check-filter</filter-name>
<filter-class>myfilter.LoginCheckFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>login-check-filter</filter-name>
<url-pattern>/list/*</url-pattern>
</filter-mapping>
</web-app>
LoginForm.html
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
로그인
<FORM ACTION=/ch11_jsp_filter_chg/Login.jsp METHOD=POST>
아이디 <INPUT TYPE=TEXT NAME=ID SIZE=8> <BR>
패스워드 <INPUT TYPE=TEXT NAME=PASSWORD SIZE=8> <BR>
<INPUT TYPE=SUBMIT VALUE='로그인'>
</FORM>
</body>
</html>
Login.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
String id = request.getParameter("ID");
String password = request.getParameter("PASSWORD");
String message;
if(id.equals("gakari")&&password.equals("1234")){
session.setAttribute("ID", id);
message = "로그인 성공";
}else{
message = "로그인 실패";
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>로그인 결과</title>
</head>
<body>
<%= message %>
</body>
</html>
실행 화면
맨처음 NameList.jsp를 실행하면 로그인이 아직 안되어있기 때문에 LoginForm.html로 이동합니다.
로그인을 하면 로그인 성공 메시지를 볼 수 있습니다.
다시 list/NameList.jsp로 가면 이번에는 로그인폼으로 자동이동 되지 않습니다.
'프로그래밍 > JSP Servlet' 카테고리의 다른 글
JSP/Servlet – 데이터베이스에서 값 읽어오기 (0) | 2016.04.10 |
---|---|
JSP/Servlet – 응답 메시지의 본체 내용을 변형하는 래퍼 클래스 만들기 (0) | 2016.04.07 |
JSP/Servlet – 응답 래퍼 클래스를 작성하는 방법 (0) | 2016.04.04 |
JSP/Servlet – 요청 래퍼 클래스 작성하기 (0) | 2016.03.21 |
JSP/Servlet - 필터 클래스의 init 메소드와 destroy 메소드 활용 (0) | 2016.03.16 |
JSP/Servlet – 필터(Filter)의 이해 (0) | 2016.03.14 |
JSP/Servlet – 커스텀 액션안에 또 다른 커스텀 액션 포함하기 (0) | 2016.03.06 |
JSP/Servlet – 태그 클래스를 이용해서 변수 지원 커스텀 액션 만들기 (0) | 2016.03.01 |