<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>JSP - Hello World</title>
</head>
<body>
<h1><%= "Hello World!" %>
</h1>
<br/>
<a href="hello-servlet">Hello Servlet</a>
</body>
<h1><%="hello world!" %></h1>
<h1><%="hello world!" %></h1>
<h1><%="hello world!" %></h1>
</html>
SEND 버튼을 누르게 되면 다음과 같은 화면을 GET 방식으로 볼 수 있게 된다.
<form action="calcResult.jsp" method="post"> //action,method 속성 추가
<input type="number" name="num1">
<input type="number" name="num2">
<button type="submit">SEND</button>
</form>
form 태그에는 action과 method라는 속성을 이용해서 말 그대로 양식을 어디에 어떤 방식으로 전송할 것 인지를 결정.
form 태그의 action을 calcResult.jsp로 전송하고 전송방식은 post로 변경.
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>NUM1 ${param.num1}</h1>
<h1>NUM2 ${param.num2}</h1>
<h1>SUM${Integer.parseInt(param.num1)+Integer.parseInt(param.num2)}</h1>
</body>
</html>
calcResult.jsp를 생성하여 action을 통해 전송을 받음.
JSP가 브라우저에 전달하고 싶은 데이터를 구성하는 데는 상당히 편리하지만 복잡한 코드를 넣는 데는 적합하지 않다. 예를 들어 NUM1 과 NUM2 처럼 전달되는 모든 데이터는 문자열로 처리되기 때문에 결과 데이터를 처리하기 위해서는 calcResult.jsp를 다음과 같이 Integer.parseInt()를 적용해서 다시 '${}'로 감싸서 처리해야 한다.
숫자를 빈칸에 입력하고 SEND를 누르게 되면,
calcResult.jsp 에서 전송을 받아 다음과 같이 출력이 된다.