jb.log

..loading

Servlet 상태관리

September 24, 2019

서블릿은 요청이 오면 응답을 주고 메모리에서 사라지기 때문에 서블릿들 간의 연결이 불가능하다.

만약 기존의 데이터를 저장할 일이 생겼다고 하면 서블릿 스스로 저장할 수 있는 것은 아니다. 이것은 ServletContext로 해결할 수 있다.

ServletContext

ServletContext는 웹 어플리케이션이 실행되면서 어플리케이션 전체의 자원이나 정보를 바인딩(Binding)하여 서블릿들이 공유하여 사용할 수 있는 클래스이다.

servletContext클래스는 톰캣 컨테이너 실행 시 각 웹 어플리케이션마다 한개의 객체를 생성한다.

ServletContext의 특징

  • javax.servlet.ServletContext 로 정의 됨
  • 서블릿과 컨테이너 간의 연동을 위해 사용
  • 컨텍스트(웹 어플리케이션)마다 하나의 ServletContext가 생성
  • 서블릿끼리 자원을 공유하는 데 사용
  • 컨테이너 실행시 생성되고 종료시 소멸

자원을 저장하는 방법들

상태유지의 방법으로 application, session, cookie 등의 방법이 있다. 계산기 만드는 예제가 있는데 로직과 결과는 동일하나 각각 다른 객체를 활용한 것을 보자.

application

  • application은 그 값이 전역적으로 사용됨
  • 프로세스간 값 공유가 가능
@WebServlet("/app-calc")
public class AppCalcTest extends HttpServlet
{
    protected void service(HttpServletRequest req, HttpServletResponse res)
        throws IOException, ServletException
    {
        ServletContext application =  req.getServletContext();
        PrintWriter out = res.getWriter();
        
        String v_ = req.getParameter("value");
        String op_ = req.getParameter("op");
        
        int v = 0;
        if(!v_.equals("")) v = Integer.valueOf(v_);
        
        if(op_.equals("="))
        {
            int x = (Integer)application.getAttribute("value");
            int y = v;
                    
            String operator = (String)application.getAttribute("op");
            if(operator.equals("+"))
                out.println(x + y);
            else if(operator.equals("-"))
                out.println(x - y);
                
            
        }else
        {
            application.setAttribute("value", v);
            application.setAttribute("op", op_);
        }
        
    }
}

session

  • session은 해당 사용자 기준으로 값이 사용됨
@WebServlet("/session-calc")
public class SessionCalcTest extends HttpServlet
{
    protected void service(HttpServletRequest req, HttpServletResponse res)
        throws IOException, ServletException
    {
        HttpSession session =  req.getSession();
        PrintWriter out = res.getWriter();
        
        String v_ = req.getParameter("value");
        String op_ = req.getParameter("op");
        
        int v = 0;
        if(!v_.equals("")) v = Integer.valueOf(v_);
        
        if(op_.equals("="))
        {
            int x = (Integer) session.getAttribute("value");
            int y = v;
                    
            String operator = (String) session.getAttribute("op");
            if(operator.equals("+"))
                out.println(x + y);
            else if(operator.equals("-"))
                out.println(x - y);
                
        }else
        {
            session.setAttribute("value", v);
            session.setAttribute("op", op_);
        }
    }
}

cookie

@WebServlet("/cookie-calc")
public class CookieCalcTest extends HttpServlet
{
    protected void service(HttpServletRequest req, HttpServletResponse res)
        throws IOException, ServletException
    {
        Cookie[] cookies =  req.getCookies();
        
        PrintWriter out = res.getWriter();
        
        String v_ = req.getParameter("value");
        String op_ = req.getParameter("op");
        
        
        
        if(op_.equals("="))
        {
            int x = 0;
            int y = Integer.valueOf(v_);
            
            for(Cookie cookie : cookies)
            {
                if(cookie.getName().equals("value"))
                {
                    x = Integer.valueOf(cookie.getValue());
                    break;
                }
            }
            
            
            String op = "";
            for(Cookie cookie : cookies)
            {
                if(cookie.getName().equals("op"))
                {
                    op = cookie.getValue();
                    break;
                }
            }
            
            if(op.equals("+"))
                out.println(x + y);
            else if(op.equals("-"))
                out.println(x - y);
        }else
        {
            Cookie valueCookie = new Cookie("value", v_);
            Cookie opCookie = new Cookie("op", op_);
            valueCookie.setPath("/");
            opCookie.setPath("/");
            
            res.addCookie(valueCookie);
            res.addCookie(opCookie);
        }
    }
}

image

Other Posts

October 1, 2019
jsp 프로젝트 만들기 - mvc1, mvc2
이전에 만든 `board-detail.jsp`은 DB와 잘 연결되어 화면에 데이터를 잘 출력하는 것을 볼 수 있다. 하지만 jsp 파일 내의 코드를 보면 자바코드와 html코드가 뒤엉켜 있는 것을 볼 수 있다. 이것을 스파게티 코드라 한다.
September 29, 2019
jsp 프로젝트 만들기 - 시작
본격적으로 jsp를 이용한 servlet 프로젝트를 만들어보겠다. jsp와 servlet의 활용 목적이 주된 내용이기 html/css 는 가급적 손대지 않고 비즈니스 로직에 집중하도록 하겠다.
September 25, 2019
jsp 프로그래밍
jsp란 `Java Server Pages` 의 약자이며 HTML 코드에 JAVA 코드를 넣어 동적웹페이지를 생성하는 웹어플리케이션 도구이다.
September 24, 2019
Servlet 상태관리
서블릿은 요청이 오면 응답을 주고 메모리에서 사라지기 때문에 서블릿들 간의 연결이 불가능하다. 만약 기존의 데이터를 저장할 일이 생겼다고 하면 서블릿 스스로 저장할 수 있는 것은 아니다. 이것은 ServletContext로 해결할 수 있다.
September 22, 2019
한글 인코딩
servlet 클래스에서 한글을 출력하면 한글이 깨지는 것을 볼 수 있다. 해당 문제점은 다음과 같이 해결할 수 있다.
September 20, 2019
Servlet 다루기
기존의 html 문서만으로는 동적인 내용을 전달할 수 없다. 때문에 WAS(web application server) 에서 동작하는 프로그래밍 언어를 사용하면 가능하다.