<!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">
<title>Insert title here</title>
<script type="text/javascript" src="../jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#btn').click(function(){
//$.getJSON() 이용 - GetServlet : command - getJSON
/*
$.getJSON("../GetServlet","command=getJSON",function(data){//서블릿에서 넘겨줬을때 데이터가 JSON형태일때 사용하면 좋다.
//data : JSON Object
//alert(data.name);
var str = data.id+"<br>"+data.name+"<br>"+data.age;
$("div#first").html(str);
});
*/
$.getJSON("../GetServlet",{"command":"getJSON"},callback);//.getJSON은 함수 호출이 성공했을 경우만 호출이 되지만 onload는 성공하던 실패하던 실행이 된다.
});
});
function callback(data, status, xhr){
//data : object
if(xhr.status==200){
var str = data.id+"<br>"+data.name+"<br>"+data.age;
$("#first").html(str);
// alert(xhr.status);
// alert(xhr.responseText);
}else{
alert("오류발생 : "+xhr.status);
}
}
</script>
</head>
<body>
<input type="button" value="클릭1" id="btn">
<div id="first"></div>
</body>
</html>
package jquery.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jquery.dto.MemberDTO;
import jquery.service.MemberService;
import org.json.JSONObject;
public class GetServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String cmd = request.getParameter("command");
System.out.println(cmd);
if(cmd.equals("getJSON")){
getJSON(request,response);
}else if(cmd.equals("getData")){
getData(request, response);
}else if(cmd.equals("run")){
run(request, response);
}else if(cmd.equals("getMemberById")){
getMemberById(request, response);
}else if(cmd.equals("getMemberByIdJson")){
getMemberByIdJson(request, response);
}
}
protected void getMemberById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. 요처파라미터 조회
String id = request.getParameter("id");
//2. B.L. 호출
MemberService ms = new MemberService();
MemberDTO mdto = ms.getMemberById(id);
//3. 응답
String str = "id : "+mdto.getId()+"<br>이름 : "+mdto.getName()+"<br>나이 : "+mdto.getAge();
response.setContentType("text/html;charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
out.println(str);
out.close();
}
protected void getMemberByIdJson(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. 요처파라미터 조회
String id = request.getParameter("id");
//2. B.L. 호출
MemberService ms = new MemberService();
MemberDTO mdto = ms.getMemberById(id);
//3. 응답
//JSONObject - JSON 객체 형태의 문자열(string)생성
JSONObject jObj = new JSONObject(mdto);
response.setContentType("text/html;charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
out.println(jObj.toString());
out.close();
}
protected void run(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
String name=request.getParameter("name");
String isSend = request.getParameter("isSend");
String number = request.getParameter("number");
String str = id+", "+name+", "+isSend+", "+number;
System.out.println(str);
response.setContentType("text/html;charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
out.println(str);
out.close();
}
protected void getData(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
out.println("<b>이것이 답입니다.</b><br>요청방식 : "+request.getMethod());
out.close();
}
protected void getJSON(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HashMap map = new HashMap();
map.put("id", "abcde");
map.put("name", "홍길동");
map.put("age", 30);
JSONObject jObj = new JSONObject(map);
response.setContentType("text/html;charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
out.println(jObj.toString());
out.close();
}
}
* 결과
'프로그래밍 > jQuery' 카테고리의 다른 글
jQuery ajax - ajax_basic[실습] (0) | 2012.06.13 |
---|---|
jQuery Ajax - get_post_getJSON예제[실습] (0) | 2012.06.13 |
jQuery Ajax연동하기[실습] load (0) | 2012.06.13 |
jQuery Ajax연동하기 (0) | 2012.06.13 |
jQuery attr,each 메소드[실습] (0) | 2012.06.12 |