<%@ 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 () {
$("#btn").click(function(){
//serialize() 메소드 이용.
//var txt = $("#f1").serialize();
//alert(txt);
$.ajax({
"url":"../GetServlet",
"type":"post",
"data":$("#f1").serialize(),
"success":function(resData){
alert(resData);
}
});
});
});
</script>
</head>
<body>
<form action="../GetServlet" id="f1">
ID : <input type="text" name="id"><br>
이름 : <input type="text" name="name"><br>
<input type="hidden" name="command" value="run"><br>
전송여부 : <input type="checkbox" name="isSend" value="true"><br>
선택 번호 :
<select name="number">
<option>선택하세요</option>
<option value="1">일번</option>
<option value="2">이번</option>
<option value="3">삼번</option>
</select><br />
<input type="button" value="ajax전송" id="btn">
<input type="submit" value="전송">
</form>
</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_basic[실습] (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 |