본문 바로가기

Servlet&JSP 프로그래밍

뉴렉처[Servlet/JSP] 강의 복습 23강~27강

23강 - 여러개의 Submit 버튼 사용하기

Submit 버튼의 name 속성을 활용!!

--> input태그의 name과 사용자로부터 입력된 값이 서버로 전달 된다.

(*단! type이 submit일때는 선택한 버튼의 value만 전달이 된다.)

ex. 

<input type="text" name="x" /> 일때 서버에 name과 사용자로부터 입력된 값이 전달된다.("x"와 "입력된 값")

<input type="submit" name="operator" value="덧셈" /> 일때 서버에 name과 value가 전달된다.("operator"과 "덧셈")

 

24강 - 입력 데어터 배열로 보내기

입력 데이터가 여러개 일때 각각의 input태그마다 name을 붙여주면 웹 서버에서 데이터를 저장할 변수를 수에 맞게 여러개 만들어야하는 불편함이 발생된다.

ex.불편함의 예

                                            ==웹 서버로 전송==> 

<input type="text" name="x" />                                    String x_ = request.get request.getParameter("x");
<input type="text" name="y" />                                    String y_ = request.get request.getParameter("y");

<input type="text" name="z" />                                    String z_ = request.get request.getParameter("z");
...                                                                             ...

 

[해결방안]

ex. <div>                                          ===>   String[] num_ = request.getParameterValues("num"); 로 받아준다.
<input type="text" name="num" />
<input type="text" name="num" />
<input type="text" name="num" />
<input type="text" name="num" />
    </div>

 

(num 배열에 저장되어있는 값을 반복문을 통하여 꺼내주면 된다.)

 

25강 - 상태 유지의 필요성

서블릿들 사이에서 전역변수처럼 값을 유지하는 경우 어떻게 해야하나?

---> Application 객체, Session 객체, Cookie 객체를 활용한다.

 

26강 - Application 객체

Application 저장소: 서블릿 컨텍스트(Context: 문맥, 책을 읽을때 책갈피 ==> 책을 이어 읽을 수 있게 해주는 역할)

즉, 서블릿 컨텍스트(Servlet Context)란 서블릿 간에 서로 문맥을 이어갈 수 있는 저장소이다.

 

[저장소 객체 생성]

ServletContext application = request.getServletContext();

[저장소에 값 저장]

application.setAttribute("value", v) //map 컬렉션(key, value)와 같이 저장한다.

application.setAttribute("op", op);

[저장소에 값 가져오기]

application.getAttribute("value"); ==> 괄호 안에 저장한 키워드 값 넣기

---> Object 객체로 반환 됨으로 int x = (Integer)application.getAttribute("value"); Wrapper 클래스를 활용해서 박싱 

 

소스코드 예시

 

 

Application을 활용한 예시 소스

@WebServlet("/calc")

public class Calc2 extends HttpServlet {

@Override

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

ServletContext application = request.getServletContext(); //서블릿 컨텍스트 저장소 application 객체 생성

 

response.setCharacterEncoding("UTF-8");

response.setContentType("text/html; charset=UTF-8");

 

String v_ = request.getParameter("v");

String op = request.getParameter("operator");

 

int v = 0;

 

if(!v_.equals(""))

v = Integer.parseInt(v_);

 

if(op.equals("=")) { //연산이 "=" 일때는 Context 저장소에 있는 값들을 계산해야한다.

int x = (Integer)application.getAttribute("value"); //앞에 Context에 저장한 값

int y = v; //현재 사용자가 입력한 값

String operator = (String)application.getAttribute("op"); //Context에 저장되어있던 operator

int result = 0;

 

if(operator.equals("+")) {

result = x + y;

}else {

result = x - y;

}

response.getWriter().printf("result is %d\n", result);

 

}else { //연산이 "+" 또는 "-" 일때는 값을 저장한다.

application.setAttribute("value", v); //map 컬렉션과 같이 저장한다.

application.setAttribute("op", op);

}

 

}

}

 

27강 - Session 객체로 상태 값 저장하기(그리고 Application 객체와의 차이점)

Application 객체의 범위는 Application 전역에서 상태값을 유지하는데 사용할 수 있다.

Session 객체의 범위는 현재 접속한 사용자에 한에서 상태값을 유지하는데 사용할 수 있다.

--> Session은 접속한 사용자마다 각각 다른 공간을 갖는다

 

사용방법: HttpSession session = request.getSession(); 으로 세션 객체를 생성하고

Application 객체를 사용했던것 처럼 똑같이 사용하면 된다.

 

* 서로 다른 브라우저를 띄어서 동일한 사이트에 접속했을때 각각 다른 사용자로 생각하고 2개의 Session이 생성된다.

반면에 하나의 브라우저를 2개 띄어서 동일한 사이트에 접속했을때는 같은 사용자로 생각하고 1개의 Session이 생성된다. 

 

 

 

 

출처: 뉴렉처
www.youtube.com/watch?v=u6-D8CJbsmo&list=PLq8wAnVUcTFVOtENMsujSgtv2TOsMy8zd&index=14