프로그래밍/JSP Servlet

JSP/Servlet – 필터 체인의 방향 바꾸기

가카리 2016. 3. 17. 21:59
반응형

 

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로 가면 이번에는 로그인폼으로 자동이동 되지 않습니다.

 

 

 

반응형