본문 바로가기

프로그래밍/jQuery

jsp로 값 넘겨주기


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<title>Insert title here</title>
</head>
<body>

<h3>JQuery</h3>

<form >
이름:<input type="text" id="name"><br>
나이:<input type="text" id="age"><br>
주소:<input type="text" id="address"><br>
<button type="button" id="btn">전송</button>
</form>

<script type="text/javascript">
$(function(){
/* 	$("#btn").click(function(){
	});
	 */
	$("#btn").on("click",function(){
		//검사
		//alert("click");
		location.href ="NewFile1.jsp?name=" +$("#name").val()+"&age="+$("#age").val()+"&address="+$("#address").val();
				
	});
	
	
});

</script>
<br><br><br>

<form id ="frm" action="NewFile1.jsp">
이름:<input type="text" id="_name" name="name"><br>
나이:<input type="text" id="_age" name="age"><br>
주소:<input type="text" id="_address" name="address"><br>
<button type="button" id="_btn">전송</button>
<!-- <input type="submit" value="전송"> -->
</form>

<script type="text/javascript">
$(function(){
	$("#_btn").click(function(){
	/* 	$("#frm").attr("action", "NewFile1.jsp").submit(); //attr: attribute의 약자  */
	    $("#frm").submit(); //form안의 submit을 동작시켜라.
	});
});

</script>

</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<% 

String name = request.getParameter("name");
String sage = request.getParameter("age");
String address = request.getParameter("address");

int age = Integer.parseInt(sage);

out.println("이름: "+name+"<br>");
out.println("나이: "+age+"<br>");
out.println("주소: "+address+"<br>");

%>

</body>
</html>