본문 바로가기

개발하자/ajax&json

텍스트박스에 id치고 정보보기 클릭하면 db정보 보임

b.jsp

==========================================

<%@ page language="java" contentType="text/html; charset=EUC-KR"
 pageEncoding="EUC-KR"%>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>

$(document).ready(function(){

$("#b").click(function(){
 $.ajax({
 url:"a.jsp",
 data:{"id":$("#txt").val()},
 dataType:"json",
 cache:false,
 success:function(data){
 var str="";
 str+="ID:"+data.member.id+"<br>";
 str+="PW:"+data.member.pw+"<br>";
 str+="ADDR:"+data.member.addr+"<br>";
 str+="TEL:"+data.member.tel+"<br>";

 $("#con").html(str);
 }
 });
});

});
</script>
<body>
<input type="text" id="txt">
<input type="button" id="b" value="회원정보보기">
<div id="con"></div>
</body>
</html>

 

a.jsp

===================================================

<%@ page language="java" contentType="text/html; charset=EUC-KR"
 pageEncoding="EUC-KR"%>
<%@ page import="java.util.*" %>
<%@ page import="kr.co.seoulit.member.dao.*" %>
<%@ page import="kr.co.seoulit.member.to.*" %>
<%@ page import="net.sf.json.*"%>
<%
 String id=request.getParameter("id");
 MemberDAO dao=MemberDAOImpl.getInstance();
 MemberBean member=dao.selectMember(id);
 HashMap<String,Object> map=new HashMap<String,Object>();
 map.put("member",member);
 JSONObject jsonObject= new JSONObject();
 jsonObject.putAll(map);
 out.println(jsonObject);
%>