<!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" src="json2.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){
var txt = xhr.responseText;
var sel = document.getElementById("sel");
var jarr = JSON.parse(txt);
for(i=0;i<jarr.length;i++){
sel[i+1]=new Option(jarr[i]);
}
}
}
}
function getCustomer(){
var sel = document.getElementById("sel");
alert(sel.options[sel.selectedIndex].value);
createXhr();
xhr.onreadystatechange = callback2;
var queryString = "command=getCustomerById&id="+sel.options[sel.selectedIndex].value;//sel.options로 옵션들의 객체들중에 sel.selectedIndex로 선택된 것을 가져온다.
xhr.open("GET","/javascript_ajax_class/JSONController?"+queryString,true);
xhr.send(null);
}
function callback2(){
if(xhr.readyState==4){
if(xhr.status==200){
var resTxt = xhr.responseText;//서버가 보낸 응답
alert(resTxt);
//var jobj = eval("("+resTxt+")"); //String의 값을 평가해서 json객체로 바꾸기 eval
var jobj = JSON.parse(resTxt);
var id = jobj.id;
var name = jobj.name;
var mileage = jobj.mileage;
var male = jobj.male;
if(male){
male="기혼";
}else{
male="미혼";
}
var str = "id : "+id+"<br>name: "+name+"<br>마일리지 : "+mileage+"<br>결혼여부 : "+male;
document.getElementById("resultLayer").innerHTML = str;
}
}
}
</script>
</head>
<body>
조회ID :
<select id="sel" onchange="getCustomer()">
<option value="default">목록보기</option>
</select>
<input type="button" value="고객ID조회" onclick="getIds()">
<div id="resultLayer"></div>
</body>
</html>
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);
}
}
* 결과
'프로그래밍 > ajax' 카테고리의 다른 글
2012-6-1 select박스를 이용한 상품 조회하기[실습](product_category) (0) | 2012.06.01 |
---|---|
2012-5-31 ajax이용한 회원ID 전체 조회하기(eval사용)[실습] (0) | 2012.05.31 |
json을 활용한 id조회하기(service,dto,controller사용)[실습] (0) | 2012.05.31 |
json데이터 처리 및 관리 (0) | 2012.05.31 |
json을 이용한 테이블 관리 (0) | 2012.05.31 |