ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

PROGRAMMING

๊ฒ€์ƒ‰์–ด์ฐพ๊ธฐ

๐Ÿฉท ์Šˆ ๐Ÿงก 2010. 7. 13. 21:19
728x90
SMALL

โ–  ์ œ๋ชฉ์„ ์ž…๋ ฅ ํ›„์— ์˜ค๋ฅธ์ชฝ ํ™”์‚ดํ‘œ ํ‚ค๋ฅผ ๋ˆŒ๋Ÿฌ ์ฃผ์‹ญ์‹œ์˜ค.
gethint.jsp ์†Œ์Šค


<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%
response.setHeader("Expires", "-1");

String[] a = new String[30];
//Fill up array with names
a[1-1]="Anna"            ;
a[2-1]="Brittany"        ;
a[3-1]="Cinderella"      ;
a[4-1]="Diana"           ;
a[5-1]="Eva"             ;
a[6-1]="Fiona"           ;
a[7-1]="Gunda"           ;
a[8-1]="Hege"            ;
a[9-1]="Inga"            ;
a[10-1]="Johanna"        ;
a[11-1]="Kitty"          ;
a[12-1]="Linda"          ;
a[13-1]="Nina"           ;
a[14-1]="Ophelia"        ;
a[15-1]="Petunia"        ;
a[16-1]="Amanda"         ;
a[17-1]="Raquel"         ;
a[18-1]="Cindy"          ;
a[19-1]="Doris"          ;
a[20-1]="Eve"            ;
a[21-1]="Evita"          ;
a[22-1]="Sunniva"        ;
a[23-1]="Tove"           ;
a[24-1]="Unni"           ;
a[25-1]="Violet"         ;
a[26-1]="Liza"           ;
a[27-1]="Elizabeth"      ;
a[28-1]="Ellen"          ;
a[29-1]="Wenche"         ;
a[30-1]="Vicky"          ;

//get the q parameter from URL
String q = request.getParameter("q");
if(q != null)
   q = q.toUpperCase();

//lookup all hints from array if length of q>0
//์กฐํšŒ ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•  ๋ณ€์ˆ˜
String hint = "";
if (q.length() > 0){
 
  for (int i = 0; i < 30; i++){
    //๊ฒ€์ƒ‰์–ด์˜ ๊ธธ์ด
    int qLength = q.length();
    //๊ฐ ๋ฐฐ์—ด์š”์†Œ์˜ ๊ธธ์ด
    int aLength = a[i].length();
    //๊ฒ€์ƒ‰์–ด์™€ ๋น„๊ตํ•  ๋ฐฐ์—ด์š”์†Œ์˜ ์ผ๋ถ€๋ถ„
    String substr = a[i].substring(0,
        (qLength > aLength ? aLength : qLength)
    );
    if ( q.equalsIgnoreCase(substr) ){
      if (hint.equals(""))
        hint=a[i];
      else
        hint=hint + " , " + a[i];
     
    }
  }< BR > }

//Output "no suggestion" if no hint were found
//or output the correct values
if (hint.equals(""))
  out.write("no suggestion");
else
  out.write(hint);

< P > %>


 

suggest.jsp ์†Œ์Šค


<%@ page language="java" contentType="text/html; charset=EUC-KR"
 pageEncoding="EUC-KR"%>
<!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=EUC-KR">
<title>Insert title here</title>
<script type="text/javascript"
        src="js/ajax_basic.js"></script>
<script type="text/javascript">
var xmlhttp;

function showHint(str)
{
 if (str.length==0)
   {
   document.getElementById("txtHint").innerHTML="";
   return;
   }
 var url="gethint.jsp";
 //sidํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ๊ฐ’์„ ์ „๋‹ฌํ•˜๋Š” ๋ชฉ์ ๋ณด๋‹ค๋Š” ํ•ญ์ƒ ๋‹ค๋ฅธ URL์ฃผ์†Œ๋ฅผ
 //ํ˜ธ์ถœํ•˜๊ธฐ ์œ„ํ•œ ์žฅ์น˜์ด๋‹ค.
 var data = "q="+str+"&sid="+Math.random();
 var method = 'GET';
 sendRequest(
   method,  //Http ์ „์†ก๋ฐฉ์‹
   url,     //์„œ๋ฒ„ ์ฃผ์†Œ
   data,    //์„œ๋ฒ„์— ์ „๋‹ฌํ•  ํŒŒ๋ผ๋ฏธํ„ฐ
   stateChanged  //์‘๋‹ต์— ๋ฐ˜์‘ํ•  ํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜ ๋ ˆํผ๋Ÿฐ์Šค
   );
}
function stateChanged()
{
if (request.readyState==4)
  {
  document.getElementById("txtHint").innerHTML=
   request.responseText;
  }
}
</script>
</head>
<body>
<form>First Name: <input type="text" id="txt1"
 onkeyup="showHint(this.value)" /></form>
<p>Suggestions: <span id="txtHint"></span></p>
</body>
</html>


ajax_basic.js ์†Œ์Šค


var request;

function getRequest(){
 if(window.ActiveXObject){
  try{
   return new ActiveXObject("Msxml2.XMLHTTP");
  }catch(e){
   try{
    return new ActiveXObject(
      "Microsoft.XMLHTTP");
   }catch(e){
    return null;
   }
  }
 }else if(window.XMLHttpRequest){
  return new XMLHttpRequest();
 }else {
  return null;
 }
}
function sendRequest(
  method,  //Http ์ „์†ก๋ฐฉ์‹
  address, //์„œ๋ฒ„ ์ฃผ์†Œ
  data,    //์„œ๋ฒ„์— ์ „๋‹ฌํ•  ํŒŒ๋ผ๋ฏธํ„ฐ
  handler  //์‘๋‹ต์— ๋ฐ˜์‘ํ•  ํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜ ๋ ˆํผ๋Ÿฐ์Šค
  ){
 //xhr ๊ฐ์ฒด ์ƒ์„ฑ
 request = getRequest();
 //์‘๋‹ต ํ•ธ๋“ค๋Ÿฌ ๋“ฑ๋ก
 request.onreadystatechange = handler;
 
 //์ „์†ก๋ฐฉ์‹์— ๋”ฐ๋ผ data ๋ฐ address๋ฅผ ์กฐ์ž‘ํ•˜์ž
 if(method == 'GET'){
  address = address + "?" + data;
  data = null;
 }
 
 //open
 request.open(method,address,true);
 
 //์š”์ฒญ์˜ header ์„ค์ •
 request.setRequestHeader(
   "Content-Type",
   "application/x-www-form-urlencoded");
 
 //send
 request.send(data);
}





 

728x90
LIST
๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
ยซ   2025/04   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
๊ธ€ ๋ณด๊ด€ํ•จ
250x250