package json.test;
public class CustomerDTO {
private String id;
private String name;
private int mileage;
private boolean male;
public CustomerDTO() {
super();
// TODO Auto-generated constructor stub
}
public CustomerDTO(String id, String name, int mileage, boolean male) {
super();
this.id = id;
this.name = name;
this.mileage = mileage;
this.male = male;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMileage() {
return mileage;
}
public void setMileage(int mileage) {
this.mileage = mileage;
}
public boolean isMale() {
return male;
}
public void setMale(boolean male) {
this.male = male;
}
@Override
public String toString() {
return "CustomerDTO [id=" + id + ", name=" + name + ", mileage="
+ mileage + ", male=" + male + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + (male ? 1231 : 1237);
result = prime * result + mileage;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
CustomerDTO other = (CustomerDTO) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (male != other.male)
return false;
if (mileage != other.mileage)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
package ajax.json.service;
import java.util.ArrayList;
import json.test.CustomerDTO;
public class CustomerService {
public CustomerDTO getCustomerById(String id){
//select * from customer where id=?
return new CustomerDTO(id, "이순신", 20000, true);
}
public ArrayList<String> getCustomerIds(){
//select id from customer
ArrayList<String> ids = new ArrayList<String>();
ids.add("aaa");
ids.add("bbb");
ids.add("ccc");
ids.add("ddd");
ids.add("eeee");
return ids;
}
}
package ajax.json.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONObject;
import json.test.CustomerDTO;
import ajax.json.service.CustomerService;
public class JSONController extends HttpServlet {
private static final long serialVersionUID = 1L;
public JSONController() {
super();
}
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");
response.setContentType("text/html;charset=utf-8");
response.setHeader("Cache-Control", "no-cache");
String command = request.getParameter("command");
if(command.equals("getCustomerById")){
getCustomerById(request,response);
}else if(command.equals("getIds")){
getIds(request,response);
}
}
protected void getCustomerById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. 요청파라미터 받기
String id = request.getParameter("id");
//2. 비지니스 로직 처리
CustomerService cs = new CustomerService();
CustomerDTO cto = cs.getCustomerById(id);
//3. 응답 - cto -> json형태
JSONObject jobj = new JSONObject(cto);
PrintWriter out = response.getWriter();
out.println(jobj);
}
protected void getIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. 비지니스 로직 처리
CustomerService cs = new CustomerService();
ArrayList<String> cto = cs.getCustomerIds();
//2. 응답 - cto -> json형태
JSONArray jobj = new JSONArray(cto);//["aaa","bbb","ccc","ddd","eeee"]
PrintWriter out = response.getWriter();
out.println(jobj);
}
}
<!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="ajax.js"></script>
<script type="text/javascript">
function getIds(){
createXhr();
xhr.onreadystatechange = callback;
var queryString="command=getIds";
xhr.open("GET","/javascript_ajax_class/JSONController?"+queryString,true);
xhr.send(null);
}
function callback(){
//서버로부터 응답이 왔다면 일한다.
if(xhr.readyState==4){//응답을 다 받은 경우
if(xhr.status==200){//응답코드가 200인 경우 - OK
var resTxt = xhr.responseText;//서버가 보낸 응답
alert(resTxt);
var jarr = eval("("+resTxt+")");
var sel = document.getElementById("ids");
for(i=0;i<jarr.length;i++){
sel.options[i+1] = new Option(jarr[i]);
}
}else{
alert("요청처리가 정상적으로 안되었습니다.\n"+xhr.status);
}
}
}
</script>
</head>
<body>
<select id="ids" name="ids">
<option>고객 ID선택</option>
</select>
<input type="button" value="고객ID 조회" onclick="getIds()">
</body>
</html>
* 결과
'프로그래밍 > ajax' 카테고리의 다른 글
2012-6-1 select박스를 이용한 상품 조회하기[실습](product_category) (0) | 2012.06.01 |
---|---|
2012-6-1 ajax를 이용한 테이블 조회하기select (0) | 2012.06.01 |
json을 활용한 id조회하기(service,dto,controller사용)[실습] (0) | 2012.05.31 |
json데이터 처리 및 관리 (0) | 2012.05.31 |
json을 이용한 테이블 관리 (0) | 2012.05.31 |