SOCO

html파일과 서블릿 클래스 파일이 어떻게 연결되는가 본문

백/Servlet,JSP(잠시 pause)

html파일과 서블릿 클래스 파일이 어떻게 연결되는가

ssooda 2021. 7. 1. 17:28

1. html 파일을 불러옴 : http://localhost:8080/calc.html

 

//html 파일 분석

1) html 파일에서  form 태그

action="calc" 속성 : 해당 html에서 form이 제출되는 경우 연결될 서블릿의 url : /calc

method="post"속성 : 쿼리스트링은 url에서 보여지지 않도록

 

2) form 태그의 자식 태그

input type = "text" name = "x"  

input type = "text" name = "y"

input type = "submit" name ="operator" value="덧셈"

 

사용자가 첫 번째 text에 3입력 두 번째 text에 4입력 후 덧셈이라고 적혀진 버튼 누른다면

name ="x"의 value ="3"으로

name ="y"의 value ="4"

name ="operator"의 value = "덧셈"으로

서블릿(calc)에 전달 될 것임

 

 

2. 버튼 클릭(submit)시 서블릿을 꺼내줌 : http://localhost:8080/calc

 

//서블릿 파일 분석 : 상속 받음(extends HttpServlet) 

1) 매칭

@WebServlet("/calc") => 해당 서블릿을 호출할 때 url의 이름을 이렇게 설정함(매칭)

 

2) 쿼리스트링

request : 응답을 받는 것(사용자가 입력)

request.getParameter("x");
request.getParameter("y");
request.getParameter("operator");

서버에서 사용자에게 쿼리스트링으로 받을 수 있는 키값을 x,y,operator로 만들어놓음

html에서 사용자가 submit을 한 경우 네임값(키값)을 받을 수 있게 됨

 

3) 서버의 콘솔이 아닌 사용자의 화면에 출력

response : 사용자에게 응답을 하는 것(출력)

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

 

4) 입력 시 한글인코딩

request.setCharacterEncoding("UTF-8");

 

5) 출력 시 한글인코딩

response.setCharacterEncoding("UTF-8");

 

6) 출력 시 형식지정 (브라우저에게 알려주는 것)

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

 

 

3. 필터 : 인터페이스 활용(implements Filter)

 

1) 필터를 어디에 적용할 지

@WebFilter("/*")

 

2) 다음 실행을 넘겨 줌

chain.doFilter(request, response)