본문 바로가기

시작

[JSP][Form Tag][JavaScript] 데이터 전송 및 받기

보내는 파일 : form.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<script>
		function ButtonSubmit(form){
			form.method = "post"
			form.action = "./result.jsp"; // 목적지
			form.submit(); // 제출
		}
	</script>
</head>
<body>
	<form name="theForm"> 
		<table border="1">
		<tr>
			<th>이름</th>
			<td><input type="text" id="name" name="name"></td>
		</tr>
			<th>번호</th>
			<td><input type="text" id="ph_number" name="ph_number"></td>
		<tr>
			<th>주소</th>
			<td><input type="text" id="address" name="address"></td>
	</table>
	<br>                                                          
	<input type="button" value="제출" onclick="javascript:ButtonSubmit(document.theForm)"><!--"document.form" == " {현재 문서}.{name 값이 theForm인 }"--> 
	</form>
</body>
</html>

받는 파일 : result.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	request.setCharacterEncoding("utf-8"); //파라미터 값에 한글이 들어가는 경우

	String name = request.getParameter("name");
	String ph_number = request.getParameter("ph_number");
	String address = request.getParameter("address");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
		<table border="1">
		<tr>
			<th>[확인] 이름</th>
			<td><%=name%></td>
		</tr>
			<th>[확인] 번호</th>
			<td><%=ph_number%></td>
		<tr>
			<th>[확인] 주소</th>
			<td><%=address%></td>
		</tr>
	</table>
</body>
</html>

 

입력 후 "제출" 클릭

결과