본문 바로가기

시작

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

보내는 파일 : 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>
	<!--jQuery 불러오기-->
	<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script> 
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<script>
		$(document).ready(function(){ 
			$("#buttonSubmit").on('click',function(){ // 제출 버튼 이벤트 지정
				$('#theForm').attr('action','./result.jsp').submit();
			});
		})
	</script>
</head>
<body>
	<form id="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 id="buttonSubmit" type="button" value="제출">
	</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>

 

입력 후 "제출" 클릭

결과