ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • ServletContext
    JAVA/웹 프로그래밍 - 서블릿 2020. 11. 1. 20:19

    ServletContext의 필요 이유는 값을 기억할 필요가 있을 때 필요하다.
    계산기 처럼 어떤 값을 연산한 결과를 기억해야할 경우에 그렇다.

    그리고 태그에서 name이 중요하다.

    그리고 디버깅을 해볼 수 있는데, 이는 크롬 창에서 "도구 더보기"에서 "개발자 도구"를 선택하면 된다.

     

    구현하려는 내용은 아래와 같다.

    위와 같은 경우에 변수를 저장할 저장소 하나를 마련한 상태에서 

    입력 란에 숫자를 입력받고 나서 덧셈과 뺄셈 연산 버튼이 클릭 됨에 따라

    기존의 저장소의 변수 값을 로드해서 입력 한 수와의 연산을 하고 나서 다시 저장을 하는 방식으로 만든다.

     

    add3.html

    더보기
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    	<form action="Add3" method="post">
    		<input type="text" name="value">
    		
    	<input type="submit" name="operator" value="+">
    	<input type="submit" name="operator" value="-">
    	<input type="submit" name="operator" value="=">
    	<br>
    		결과 : 0	
    	</form>	
    </body>
    </html>

     

     

    Add3.java의 전체 내용

    application의 메소드인 getAttribute함수는 Object를 리턴하는 함수여서 만약 null이면 해당 데이터가 없는 경우이고

    null이 아니면 해당 데이터가 존재하는 경우로 파악할 수 있다.즉, 이와 같은 작업이 필요한 이유는 처음에 3과 덧셈 연산이 선택이 되면 기존에 저장된 데이터가 없으므로 getAttribute메소드는 null이 리턴되므로 이 경우에 연산할 필요 없이 저장소에 바로 저장을 한다.그 이외의 경우라면 getAttribute메소드에서 데이터를 로드해서 연산하고 다시 setAttribute 메소드를 통해 저장을 한다. 

    더보기
    package com.iot.web;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    @WebServlet("/Add3")
    public class Add3 extends HttpServlet {
    	private static final long serialVersionUID = 1L;
    
    	protected void service(HttpServletRequest request, 
    			HttpServletResponse response) throws ServletException, IOException 
    	{
    		//	서블릿이 끝나고 나서도 값을 유지할 수 있는 저장공간 만들기
    		ServletContext application = request.getServletContext();
    		
    		response.setCharacterEncoding("UTF-8");
    		response.setContentType("text/html;charset=UTF-8");
    		PrintWriter out = response.getWriter();
    		
    		String value_ = request.getParameter("value");
    		String operator = request.getParameter("operator");
    		
    		int value = 0;
    		
    		Object val = null;	//	저장소에 데이터가 존재하는지를 판단하는 변수
    							//	application.getAttribute 메소드는 Object를 리턴한다.
    		
    		if (!value_.equals(""))	
    			value = Integer.parseInt(value_);
    		
    		if (operator.equals("+"))
    		{
    			//	저장소에 데이터가 있는지를 확인
    			val = application.getAttribute("result");
    			
    			//	저장소에 데이터가 있는 경우에 누적합
    			if (val != null)
    			{
    				//	setAttribute 메소드는 String은 키를 의미하고, Object는 값을 의미한다.
    				application.setAttribute("result", 
    						value + (Integer)application.getAttribute("result")
    						);
    			}
    			//	저장소에 데이터가 없으면 단순 저장
    			else
    			{
    				application.setAttribute("result",value);
    				out.println("저장소에 데이터 없음");
    			}
    		}
    		else if (operator.equals("-"))
    		{
    			//	저장소에 데이터가 있는지를 확인
    			val = application.getAttribute("result");
    			
    			//	저장소에 데이터가 있는 경우에 누적합
    			if (val != null)
    			{
    				//	setAttribute 메소드는 String은 키를 의미하고, Object는 값을 의미한다.
    				application.setAttribute("result", 
    						(Integer)application.getAttribute("result") - value
    						);
    			}
    			//	저장소에 데이터가 없으면 단순 저장
    			else
    			{
    				application.setAttribute("result",value);
    				out.println("저장소에 데이터 없음");
    			}
    		}
    		else if (operator.equals("="))
    		{
    			//	저장소에 데이터가 있는지를 확인
    			val = application.getAttribute("result");
    					
    			if (val != null)
    			{
    				int l_num = (Integer)application.getAttribute("result");
    				out.println("결과값 : "+l_num);
    			}
    			else
    			{
    				out.println("계산한 결과가 없습니다.");
    			}
    		}
    	}
    }

     

     

    실행할 시에 숫자를 입력하고 연산 버튼을 선택하고 다시 뒤로 가기로 나와서 수 입력하고 연산 버튼을 누르면 된다.

    그리고 마지막에 연산한 결과를 출력할 때에 "=" 을 선택하면 된다.

     

    소스 파일

    add3.html
    0.00MB
    Add3.java
    0.00MB

     

     

     

    위의 예제는 로직 상에 약간의 이상한 점을 발견했을 것이다.

    즉, 위의 인터페이스를 생각해봤을 떼, 예를 들어

    입력 칸에 5를 입력하고  "+" 버튼을 선택하고 다시 입력 칸에 3을 입력하고 "-" 버튼을 선택하고

    다시 입력 칸에 10을 입력하고 "=" 버튼을 선택하면

    아래의 연산이 이뤄져야 한다.

    " ( 5 + ) + ( 3 - ) + ( 10 ) = "

    그래서 이 연산의 결과는 -2가 나오면 된다.

    이와 같은 방식으로 만들어보자.

    add4.html과 Add4.java 서블릿 파일을 만들고 add3.html과 Add3.java 파일의 내용을 그대로 복사한다.

    그리고 아래에서 application.removeAttribute메소드의 호출을 볼 수 있는데

    이는 저장소의 값들을 초기화 하는 메소드들이다.

    이 함수의 호출을 한 이유는 우리가 "=" 을 클릭하면 연산이 끝났음을 의미하는데

    이 초기화를 해주지 않으면 기존의 저장소의 값이 저장되어서 다시 연산을 하려면 서버를 재실행하는 수 밖에 없다.

    이는 불편하다. 그래서 "=" 버튼이 클릭이 되면 출력된 결과값을 보여주고 나서 저장소를 초기화를 하는 것이다. 

    저장소는 두 군데이므로 아래의 두 문장을 호출한다.
    application.removeAttribute("result");      //누적된 값들을 초기화
    application.removeAttribute("operator");  //누적된 값들을 초기화

    add4.html 전체 내용

    더보기
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    	<form action="Add4" method="post">
    		<input type="text" name="value">
    		
    	<input type="submit" name="operator" value="+">
    	<input type="submit" name="operator" value="-">
    	<input type="submit" name="operator" value="=">
    	<br>
    		결과 : 0	
    	</form>	
    </body>
    </html>

     

    Add4.java 전체 내용

    더보기
    package com.iot.web;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    @WebServlet("/Add4")
    public class Add4 extends HttpServlet {
    	private static final long serialVersionUID = 1L;
    
    	protected void service(HttpServletRequest request, 
    			HttpServletResponse response) throws ServletException, IOException 
    	{
    		//	서블릿이 끝나고 나서도 값을 유지할 수 있는 저장공간 만들기
    		ServletContext application = request.getServletContext();
    		
    		response.setCharacterEncoding("UTF-8");
    		response.setContentType("text/html;charset=UTF-8");
    		PrintWriter out = response.getWriter();
    		
    		String value_ = request.getParameter("value");
    		String operator = request.getParameter("operator");
    		
    		int value = 0;
    		
    		if (!value_.equals(""))		value = Integer.parseInt(value_);
    		
    		//	아래의 두 if문은 첫 데이터를 저장 시에 대한 예외처리로 한 묶음으로 가지고 간다.
    		{
    			//	저장소가 비어있으면 0으로 초기화
    			if (application.getAttribute("result") == null)
    			{
    				application.setAttribute("result", 0);
    			}
    			
    			//	기본 연산자는 "+"로 초기화
    			if (application.getAttribute("operator") == null)
    			{
    				application.setAttribute("operator", "+");
    			}
    		}
    			
    		//	연산 후 출력
    		if (operator.equals("="))
    		{
    			//	이전에 저장되어 있는 연산자를 가지고 와서 계산을 해야 한다.
    			int result = 0;
    			
    			if (application.getAttribute("operator").equals("+"))
    			{
    				result = (Integer)application.getAttribute("result") + value; 
    			}
    			else if (application.getAttribute("operator").equals("-"))
    			{
    				result = (Integer)application.getAttribute("result") - value; 
    			}
    			
    			out.println("결과 : " +result);
    			application.removeAttribute("result");		//	누적된 값들을 초기화
    			application.removeAttribute("operator");	//	누적된 값들을 초기화
    		}
    		//	이전에 저장되어 있는 연산자를가지고 와서 연산만 한다.
    		else
    		{
    			//	결과를 임시 저장해야 한다.
    			if (application.getAttribute("operator").equals("+"))
    			{
    				application.setAttribute("result", 
    					(Integer)application.getAttribute("result") + value);
    			}
    			else if (application.getAttribute("operator").equals("-"))
    			{
    				application.setAttribute("result", 
    						(Integer)application.getAttribute("result") - value);
    			}
    			
    			//	실시간으로 저장소에 저장된 값을 학인
    			System.out.println(application.getAttribute("result"));
    			
    			application.setAttribute("operator",operator);
    		}
    	}
    }

     

     

     

    그리고, ServletContext객체는 전역 변수와 같다.

    전역 변수와 같은지 한 번 알아보자.

    이에 대해 실험을 하는 방법은 아주 간단하다.

    위의 내용을 Edge와 크롬 두 창을 띄워놓고 번갈아가면서 수 입력하고 연산자를 선택하면 된다.

    서버를 실행한 후에, 웹 브라우저 Edge와 Chrome을 띄우자. 

    두 웹 브라우저의 주소를 localhost:9090/add4.html로 접속을 하자.

    Chorome 창에서 입력 칸에 5를 입력하고  "+" 버튼을 선택하고

    Edge 창에서 입력 칸에 3을 입력하고 "-" 버튼을 선택하고 

    다시 Chrome 창에서 입력 칸에 10을 입력하고 "=" 버튼 선택해보자.

    그러면 위의 결과와 마찬가지로 -2가 나오는지를 확인하면 된다.

    서로 다른 웹 브라우저에서 실행을 하였는데, 위와 마찬가지로 -2가 나왔다.

    그러므로 ServletContext 객체는 전역 변수와 같음이 입증되었다.

     

    그런데 위와 같은 방법이 불만이 있을 수 있을 것이다.

    이는 ServletContext객체가 아닌 HttpSession을 활용하면 된다.

    이에 대해서는 아래를 참고하자.

     

    designatedroom87.tistory.com/276

     

    HttpSession

    HttpSession에 대하여 알아보자. 우선은 ServletContext에 대해 알아보고 오자. designatedroom87.tistory.com/272?category=899787 ServletContext ServletContext의 필요 이유는 값을 기억할 필요가 있을 때 필..

    designatedroom87.tistory.com

     

    'JAVA > 웹 프로그래밍 - 서블릿' 카테고리의 다른 글

    Cookie  (0) 2020.11.02
    HttpSession  (0) 2020.11.02
    웹 브라우저에서 수의 덧셈을하고 출력하기  (0) 2020.11.01
    Filter  (0) 2020.10.30
    form 과 나머지 input 태그 더 살펴보기  (0) 2020.10.30

    댓글

Designed by Tistory.