<%@ 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>
<title>jQuery</title>
<script type="text/javascript" src="../jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#ajaxBtn').click(function () {
//.ajax 이용 - 응답 데이터 text형태. url : GetServlet, command - getMemberById
$.ajax({
"url":"./GetServlet",
"type":"get",
"data":"command=getMemberById&id="+$("#id").val(),
"success":function(returnData){
alert(returnData);
$("#layer").html(returnData);
},
"error":function(xhr, status, errMsg){
/*
오류 처리 함수의 인수
1. XMLHTTPRequest 객체
2. 에러인지 정상인지의 상태
3. 에러메세지
*/
alert(xhr.readyState+" - "+status+" - "+errMsg);
}
});
});
$("#jsonBtn").click(function(){
//.ajax 이용 - 응답 데이터 JSON형태. url : GetServlet, command - getMemberByIdJson
$.ajax({
"type":"post",
"url":"../GetServlet",
"dataType":"json",
"data":{"command":"getMemberByIdJson","id":$("#id").val()},
"beforeSend":function(){
alert("요청보내기 전 호출");
if($("#id").val()==""){
return false;//요청처리가 취소
}
},
"success":function(returnData){
//data : JSON object
alert(returnData.id);
$("#layer").html("ID : "+returnData.id+"<br>이름 : "+returnData.name+"<br>나이 : "+returnData.age);
},
"error":function(){
alert("요청 처리중 오류가 발생했습니다.");
}
});
});
});
</script>
</head>
<body>
<form action="../GetServlet">
ID : <input type="text" name="id" id="id">
<input type="button" value="ajax전송" id="ajaxBtn">
<input type="button" value="json전송" id="jsonBtn">
</form>
<div id="layer"></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);
System.out.println(jObj);
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-category_list_jquery2.jsp[실습] (0) | 2012.06.13 |
---|---|
jQuery ajax - ajax_serialize[실습] (0) | 2012.06.13 |
jQuery Ajax - get_post_getJSON예제[실습] (0) | 2012.06.13 |
jQuery Ajax - getJSON예제[실습] (0) | 2012.06.13 |
jQuery Ajax연동하기[실습] load (0) | 2012.06.13 |