1. "test2" 프로젝트의 "src > main > webapp > WEB-INF > jsp"에서 오른쪽 버튼을 클릭하여 컨텍스트 메뉴 [New > JSP File]를 클릭하여 "testform.jsp"파일을 생성한다.
"testform.jsp"파일에 "<form>"를 추가합니다.
<form action="/test2/testformprocess.do">
<input name="name">
<button type="submit">전송</button>
</form>
<div><%=request.getAttribute("errorMessage")%></div>
2. "testform.do"으로 요청이 들어오면 "testform.jsp"이 연동되도록 "TestDispatcherServlet.java"의 "service"메소드에 추가합니다.
} else if ("/testform.do".equals(requestURI)) {
request.setAttribute("errorMessage", "");
requestDispatcher = request.getRequestDispatcher("/WEB-INF/jsp/testform.jsp");
}
3. "Servers"탭에서 "Tomcat8"를 선택하고 "start"버튼(start the server)을 클릭합니다. 웹 브라우저에서 "http://localhost:8080/test2/testform.do"를 입력합니다.
"Servers"탭에서 "Tomcat8"를 선택하고 "stop"버튼(stop the server)을 클릭합니다.
4. "test2" 프로젝트의 "Java Resources"에서 "src/main/java"의 "com.hom.project.test2.service"에서 오른쪽 버튼을 클릭하여 컨텍스트 메뉴 [New > Class]를 클릭하여 "TestService3.java" 생성한다.
"process"메소드를 생성합니다.
public void process(HttpServletRequest request) {
String name = request.getParameter("name");
System.out.println("name : " + name);
request.setAttribute("name", name);
}
"<form>"안에 있는 "<input>"의 값을 가져와서 "request.setAttribute"에 장합니다.
5. "testformprocess.do"으로 "<form>"요청이 들어오면 "TestService3"서비스를 통해 처리하고 "testservlet.jsp"이 연동되도록 "TestDispatcherServlet.java"의 "service"메소드에 추가합니다.
} else if ("/testformprocess.do".equals(requestURI)) {
TestService3 testService3 = new TestService3();
testService3.process(request);
requestDispatcher = request.getRequestDispatcher("/WEB-INF/jsp/testservlet.jsp");
}
6. "Servers"탭에서 "Tomcat8"를 선택하고 "start"버튼(start the server)을 클릭합니다. 웹 브라우저에서 "http://localhost:8080/test2/testform.do"를 입력합니다.
입력란에 "홍길동"을 입력하고 "전송"버튼을 클릭합니다.
"Console"탭를 클릭해보면 "TestService3.java"의 "process"메소드에서 "<form>"에서 입력된 "홍길동"이 출력되는 것을 확인 할 수 있습니다.
[Console 출력 내용]
contextPath : /test2
requestURI : /test2/testformprocess.do
changed requestURI : /testformprocess.do
name : 홍길동
웹 브러우저에서 URL를 보시면 URL에 "name=홍길동"이 추가되어 있습니다. "<form>"의 전송방식이(method)이 생략되면 기본적으로 "get"으로 되기 때문입니다.
그럼 "<form>"의 "method"를 "post"로 변경하도록 하겠습니다.
"Servers"탭에서 "Tomcat8"를 선택하고 "stop"버튼(stop the server)을 클릭합니다.
7. "testform.jsp"파일에 "<form>"의 "method"를 "post"를 추가합니다.
<form action="/test2/testformprocess.do" method="post">
8. "Servers"탭에서 "Tomcat8"를 선택하고 "start"버튼(start the server)을 클릭합니다. 웹 브라우저에서 "http://localhost:8080/test2/testform.do"를 입력한 후, 입력란에 "홍길동"을 입력하고 "전송"버튼을 클릭합니다.
전송된 한글이 깨져서 나옵니다.
"Console"탭를 클릭해보면 "TestService3.java"의 "process"메소드에서 "<form>"에서 입력된 "홍길동"이 깨져서 출력되는 것을 확인 할 수 있습니다.
9. "TestService3.java"파일에 "request.setCharacterEncoding("UTF-8");"를 추가하여 코딩하고 저장합니다.
request.setCharacterEncoding("UTF-8");
"setCharacterEncoding"처리에 대한 예외처리를 "Add throws declaration"를 클릭하여 추가합니다.
9. "Servers"탭에서 "Tomcat8"를 선택하고 "start"버튼(start the server)을 클릭합니다. 웹 브라우저에서 "http://localhost:8080/test2/testform.do"를 입력한 후, 입력란에 "홍길동"을 입력하고 "전송"버튼을 클릭합니다.
전송된 한글이 정상적으로 나옵니다.
"Console"탭를 클릭해보면 "TestService3.java"의 "process"메소드에서 "<form>"에서 입력된 "홍길동"이 정상적으로 출력되는 것을 확인 할 수 있습니다.
이처럼 프로세스를 처리한 서비스 클래스에서 모든 "<form>"에 대한 인코딩 처리를 매번 하는 것은 번거로운 일입니다. 그래서 모든 요청(request)에 대해 인코딩을 일괄적으로 처리할 수 있게 "Filter"를 이용하여 처리하면 됩니다.
다음에 이어서 합니다.