IT/jQuery

jQuery - ajax통신, json, xml 사용 예제

노마드오브 2018. 11. 18. 19:55

string.jsp

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

    pageEncoding="UTF-8"%>

<h1>Hello JSP</h1>



string.html

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script>

$(document).ready(function () {

$('button').click(function () {

$.ajax('string.jsp', {

success: function (data) {

//alert(data.trim());

$('div').append(data);

}

});

});

});

</script>

</head>

<body>

<button type="button">버튼</button>

<div></div>

</body>

</html>



string2.jsp

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

    pageEncoding="UTF-8"%>

<%

String name = request.getParameter("name");

String age = request.getParameter("age");

%>


<h1><%=name %> : <%=age %></h1>




string2.html

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script>

$(document).ready(function () {

$('button').click(function () {

$.ajax('string2.jsp', {

data: {name: '홍길동', age: 20}, 

success: function (data) {

$('div').append(data);

},

type: 'POST'

});

});

});

</script>

</head>

<body>

<button type="button">버튼</button>

<div></div>

</body>

</html>




xml.jsp

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

    pageEncoding="UTF-8"%>

<members>

<member>

<name>홍길동</name>

<gender>남자</gender>

<email>aa@a.com</email>

</member>

<member>

<name>성춘향</name>

<gender>여자</gender>

<email>bb@b.com</email>

</member>

<member>

<name>이몽룡</name>

<gender>남자</gender>

<email>cc@c.com</email>

</member>

</members>




xml.html

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script>

$(document).ready(function () {

$('button').click(function () {

$.ajax('xml.jsp', {

success: function (data) {

// j1/test5.jsp 참고

$(data).find('member').each(function () {

// $(this)는 <member> 엘리먼트임

var name = $(this).find('name').text();

var gender = $(this).find('gender').text();

var email = $(this).find('email').text();

$('div').append('<h1>' + name + ':' + gender + ':' + email + '</h1>');

})

}

});

});

});

</script>

</head>

<body>

<button type="button">버튼</button>

<div></div>

</body>

</html>



xmlMembers.jsp

<%@page import="com.example.domain.Member"%>

<%@page import="java.util.List"%>

<%@page import="com.example.repository.MemberDao"%>

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

    pageEncoding="UTF-8"%>

<%

// DB객체 준비

MemberDao memberDao = MemberDao.getInstance();


List<Member> list = memberDao.getMembers();


%>

<members>

<%

for (Member member : list) {

%>

<member>

<id><%=member.getId() %></id>

<name><%=member.getName() %></name>

<email><%=member.getEmail() %></email>

</member>

<%

}

%>

</members>




xmlMembers.html

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script>

$(document).ready(function () {

$('button').click(function () {

$('div').empty();

$.ajax('xmlMembers.jsp', {

success: function (data) {

// j1/test5.jsp 참고

$(data).find('member').each(function () {

// $(this)는 <member> 엘리먼트임

var id = $(this).find('id').text();

var name = $(this).find('name').text();

var email = $(this).find('email').text();

$('div').append('<h1>' + id + ':' + name + ':' + email + '</h1>');

})

}

});

});

});

</script>

</head>

<body>

<button type="button">버튼</button>

<div></div>

</body>

</html>