<!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">
<script type="text/javascript" src="../jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#btn").click(function(){
//GetServlet : command - getData
//$.get() 함수 이용
/*
$.get("../GetServlet","command=getData",function(data){
alert(data);
$("#showLayer").html(data);
});
*/
//$.post() 함수 이용
/*
$.post("../GetServlet","command=getData",function(data){
alert(data);
$("#showLayer").html(data);
});
*/
//json객체 이용
$.post("../GetServlet","command=getJSON",function(data){
alert(data);
var jobj = $.parseJSON(data);
var txt = jobj.id+"<br>"+jobj.name+"<br>"+jobj.age;
$("#showLayer").html(txt);
});
});
});
</script>
<title>Insert title here</title>
</head>
<body>
<input type="button" value="클릭" id="btn">
<div id="showLayer"></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_serialize[실습] (0) | 2012.06.13 |
---|---|
jQuery ajax - ajax_basic[실습] (0) | 2012.06.13 |
jQuery Ajax - getJSON예제[실습] (0) | 2012.06.13 |
jQuery Ajax연동하기[실습] load (0) | 2012.06.13 |
jQuery Ajax연동하기 (0) | 2012.06.13 |