
โ๏ธ Test1.
๐ป ์ ๋ ฅ
์์์ค
helloAjax.jsp<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%
request.setCharacterEncoding("UTF-8");
String cp = request.getContextPath();
%>
<!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">
var xmlHttp;
function ajaxRequest() {
xmlHttp = new XMLHttpRequest();
xmlHttp.open("get","helloAjax_ok.jsp",true);
xmlHttp.onreadystatechange = viewMessage;
xmlHttp.send(null);
}
function viewMessage() {
var responseText = xmlHttp.responseText;
var divE = document.getElementById("printDiv");
divE.innerHTML = responseText;
}
</script>
</head>
<body>
<h1>Hello AJax</h1>
<input type="button" value="ํด๋ฆญ" onclick="ajaxRequest();"/>
<br/><br/>
<div id="printDiv" style="border: 1px solid red; width: 50%"></div>
</body>
</html>
helloAjax_ok.jsp<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%
request.setCharacterEncoding("UTF-8");
String cp = request.getContextPath();
%>
์ด๋ ต์ง๋ง ์ฌ๋ฐ๋ AJax!!!

โ๏ธ Test2.
๐ป ์ ๋ ฅ
ajaxGetPost.jsp<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%
request.setCharacterEncoding("UTF-8");
String cp = request.getContextPath();
%>
<!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">
var xmlHttp;
function ajaxRequestGet() {
xmlHttp = new XMLHttpRequest();
var f = document.myForm;
var data = f.greeting.value; //<input type="text" name="greeting"/> -> greeting์ ๋ปํจ
if(data==""){
alert("๋ฐ์ดํฐ๋ฅผ ์
๋ ฅํ์ธ์!");
f.greeting.focus();
return;
}
xmlHttp.open("GET", "ajaxGetPost_ok.jsp?greeting="+data,true); //๋น๋๊ธฐ๋ฐฉ์์ผ๋ก ๋ณด๋ผ๊ฑฐ์
xmlHttp.onreadystatechange = viewMessage; //viewMessage = callback ํจ์ -> ํจ์ ๋ง๋ค์ด์ผํจ
xmlHttp.send(null);
}
function viewMessage() {
var divE = document.getElementById("printDiv");
if(xmlHttp.readyState==1||
xmlHttp.readyState==2||
xmlHttp.readyState==3){
divE.innerHTML = "<img src='./image/processing.gif' width='50' height='50'/>" //์ง๊ธ ์คํ์ค์ด์ผ~๋ผ๋ ์ด๋ฏธ์ง๋ฅผ ๋ฃ์๊ฑฐ์
}else if(xmlHttp.readyState==4){
divE.innerHTML = xmlHttp.responseText; //๋ฐ์์ ๋ฟ๋ฆฌ๋ฉด ๋จ
}
}
</script>
</head>
<body>
<h1>AJaxGetPost</h1>
<hr/>
<form action="" name="myForm">
<input type="text" name="greeting"/>
<input type="button" value="Get ์ ์ก" onclick="ajaxRequestGet();"/>
</form>
<div id="printDiv" style="border: 1px solid blue; widows: 50%"></div>
</body>
</html>


ajaxGetPost_ok.jsp<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%
request.setCharacterEncoding("UTF-8");
String cp = request.getContextPath();
//๋์ด์ค๋ ๋ฐ์ดํฐ๊ฐ ์์ผ๋๊น ๋ฐ์์ผํจ
String greeting = request.getParameter("greeting");
//์์๋ก ์ฒ๋ฆฌ์๊ฐ ๋ง๋ค์ด์ฃผ๊ธฐ
for(int i=0;i<35000;i++){
System.out.print("๊ธฐ๋ค๋ ค..์ฒ๋ฆฌ์ค...");
}
%>
<%="server : " + greeting %>



for(int i=0;i<35000;i++){
System.out.print("๊ธฐ๋ค๋ ค..์ฒ๋ฆฌ์ค...");
}
โ๏ธ Test3.
๐ป ์ ๋ ฅ
ajaxGetPost.jsp<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%
request.setCharacterEncoding("UTF-8");
String cp = request.getContextPath();
%>
<!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">
var xmlHttp; //์ฌ๊ธฐ์ 3๊ฐ์ง ๊ฐ์ฒด๊ฐ ๋ค์ด๊ฐ์์ (์๋ if๋ฌธ 3๊ฐ)
function getXmlHttpRequest() { //๋๊ฐ ์คํ์ํค๋๋ฉด -> ์คํฌ๋ฆฝํธ ๋งจ ์๋์ชฝ์ ๋ง์ด ์ ์ด๋
if(window.ActiveXObject){
//๊ฐ์ฒด ์์ฑ ๋ฐฉ๋ฒ
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}else{
xmlHttp = new XMLHttpRequest();
}
}
function ajaxRequestGet() {
var f = document.myForm;
var data = f.greeting.value; //<input type="text" name="greeting"/> -> greeting์ ๋ปํจ
if(data==""){
alert("๋ฐ์ดํฐ๋ฅผ ์
๋ ฅํ์ธ์!");
f.greeting.focus();
return;
}
xmlHttp.open("GET", "ajaxGetPost_ok.jsp?greeting="+data,true); //๋น๋๊ธฐ๋ฐฉ์์ผ๋ก ๋ณด๋ผ๊ฑฐ์
xmlHttp.onreadystatechange = viewMessage; //viewMessage = callback ํจ์ -> ํจ์ ๋ง๋ค์ด์ผํจ
xmlHttp.send(null);
}
function ajaxRequestPost() {
var f = document.myForm;
var data = f.greeting.value; //<input type="text" name="greeting"/> -> greeting์ ๋ปํจ
if(data==""){
alert("๋ฐ์ดํฐ๋ฅผ ์
๋ ฅํ์ธ์!");
f.greeting.focus();
return;
}
xmlHttp.open("POST", "ajaxGetPost_ok.jsp",true); //POST๋ฐฉ์ -> ๋ฐ์ดํฐ ๊ฐ์ง๊ณ ๊ฐ ์ ์์
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
xmlHttp.onreadystatechange = viewMessage;
xmlHttp.send("greeting="+data);
}
function viewMessage() {
var divE = document.getElementById("printDiv");
if(xmlHttp.readyState==1||
xmlHttp.readyState==2||
xmlHttp.readyState==3){
divE.innerHTML = "<img src='./image/processing.gif' width='50' height='50'/>" //์ง๊ธ ์คํ์ค์ด์ผ~๋ผ๋ ์ด๋ฏธ์ง๋ฅผ ๋ฃ์๊ฑฐ์
}else if(xmlHttp.readyState==4){
divE.innerHTML = xmlHttp.responseText; //๋ฐ์์ ๋ฟ๋ฆฌ๋ฉด ๋จ
}
}
//function getXmlHttpRequest() { // ์๊ฐ ์คํ์ํด
window.onload = function () {
getXmlHttpRequest();
}
</script>
</head>
<body>
<h1>AJaxGetPost</h1>
<hr/>
<form action="" name="myForm">
<input type="text" name="greeting"/>
<input type="button" value="Get ์ ์ก" onclick="ajaxRequestGet();"/>
<input type="button" value="Post ์ ์ก" onclick="ajaxRequestPost();"/>
</form>
<div id="printDiv" style="border: 1px solid blue; widows: 50%"></div>
</body>
</html>



โ๏ธ Test4.
๐ป ์ ๋ ฅ
ajaxId.jsp<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%
request.setCharacterEncoding("UTF-8");
String cp = request.getContextPath();
%>
<!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">
var xmlHttp = new XMLHttpRequest();
function requestGet() {
var f = document.myForm;
var params = "?userId=" + f.userId.value;
xmlHttp.open("GET", "ajaxId_ok.jsp" + params, true);
xmlHttp.onreadystatechange = viewMessage;
xmlHttp.send(null);
}
function viewMessage() {
if(xmlHttp.readyState==4){
if(xmlHttp.status=200){
//์ ์กฐ๊ฑด์ ๋ง์ ๋ ๋ฐ์ดํฐ ๋ฐ์๋ด๊ธฐ
var str = xmlHttp.responseText;
var divE = document.getElementById("resultDiv");
divE.innerHTML = str;
}
}else{
var divE = document.getElementById("resultDiv");
divE.innerHTML = "<img src='./image/loading.gif' width='15' height='15'/>"
}
}
</script>
</head>
<body>
<h1>Ajax ID ํ์ธ</h1>
<hr/>
<form action="" name="myForm">
์์ด๋ : <input type="text" name="userId" onkeyup="requestGet();"/>
</form>
<div id="resultDiv" style="color: red; border: 1px solid; width: 40%"></div>
</body>
</html>

ajaxId_ok.jsp<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%
request.setCharacterEncoding("UTF-8");
String cp = request.getContextPath();
String userId = request.getParameter("userId");
String str = "";
if(!userId.equals("")){
if(userId.startsWith("suzi")){
str = userId + "๋ ์ ํจํ ์์ด๋ ์
๋๋ค";
}else{
str = userId + "๋ ์ ํจํ ์์ด๋๊ฐ ์๋๋๋ค";
}
}
%>
<%=str %>



`setTimeout("requestTime()",1000);` setTimeout : ์๊ธฐ์์ ์ ๋ด๊ฐ ์ํ๋ ์๊ฐ์ ๋ง์ถฐ์ ๊ณ์ ๋ฐ๋ณต๋์ด์ ์คํํ๋ ๊ฒ
โ๏ธ Test5.
๐ป ์ ๋ ฅ
clock.jsp<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%
request.setCharacterEncoding("UTF-8");
String cp = request.getContextPath();
%>
<!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="<%=cp%>/data/js/ajaxUtil.js"></script>
<script type="text/javascript">
//var xmlHttp = new XMLHttpRequest();
function printClientTime() {
var clientTime = document.getElementById("clientTime");
var now = new Date();
var timeStr = now.getFullYear() + "๋
" + (now.getMonth()+1) + "์ " + now.getDate() + "์ผ "
+ now.getHours() + "์ " + now.getMinutes() + "๋ถ " + now.getSeconds() + "์ด";
clientTime.innerHTML = timeStr;
setTimeout("printClientTime()",1000);
}
function requestTime(){
//์๋ฒ์ฐพ์๊ฐ์ผํจ
//xmlHttp.open("GET", "clock_ok.jsp",true);
//xmlHttp.onreadystatechange = printServerTime;
//xmlHttp.send(null);
sendRequest("clock_ok.jsp", null, printServerTime,"GET");
setTimeout("requestTime()",1000);
}
function printServerTime() {
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
var serverTime =
document.getElementById("serverTime");
serverTime.innerHTML = xmlHttp.responseText;
}
}
}
window.onload = function () {
printClientTime();
requestTime();
}
</script>
</head>
<body>
1.ํ์ฌ ํด๋ผ์ด์ธํธ ์๊ฐ์ <b><span id="clientTime"></span></b>์
๋๋ค.<br/>
2.ํ์ฌ ์๋ฒ ์๊ฐ์ <b><span id="serverTime"></span></b>์
๋๋ค.<br/>
</body>
</html>
clock_ok.jsp<%@page import="java.util.Date"%>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%
request.setCharacterEncoding("UTF-8");
String cp = request.getContextPath();
%>
<%=new Date() %> <!-- ์๋ฒ์ ์๊ฐ -->


โ๏ธ Test6.
๐ป ์ ๋ ฅ
newsTitleCSV.jsp<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
request.setCharacterEncoding("UTF-8");
String cp = request.getContextPath();
%>
<!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>
<style type="text/css">
.news{
font-size: 9pt;
display: block;
margin: 0 auto;
background: lightgray;
color: blue;
border: 1px dashed black;
width: 50%;
}
.newsError{
font-size: 9pt;
display: block;
margin: 0 auto;
background: orange;
color: red;
border: 1px dashed black;
width: 50%;
}
</style>
<script type="text/javascript" src="<%=cp%>/data/js/ajaxUtil.js"></script>
<script type="text/javascript">
function newsTitle() {
//hideNewsTitle();
sendRequest("newsTitleCSV_ok.jsp",null,displayNewsTitle,"GET");
}
function displayNewsTitle() {
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
var csvStr = xmlHttp.responseText;
//alert(csvStr);
var csvArray = csvStr.split("|");
var countStr = csvArray[0];
//alert(countStr);
if(countStr==0){
alert("News๊ฐ ์๋ค");
return;
}
var csvData = csvArray[1];
//alert(csvData);
var newsTitleArray = csvData.split("*");
//alert(newsTitleArray.length);
var html = "";
html += "<ol>";
for(var i=0; i<newsTitleArray.length;i++){
var newsTitle = newsTitleArray[i];
html += "<li>" + newsTitle + "</li>";
}
html += "<ol>";
//alert(html);
var newsDiv = document.getElementById("newsDiv");
newsDiv.innerHTML = html;
}
}
}
function showNewsDiv() {
var newsDiv = document.getElementById("newsDiv");
newsDiv.style.display = "block";
}
function hideNewsDiv() {
var newsDiv = document.getElementById("newsDiv");
newsDiv.style.display = "none";
}
window.onload = function() {
newsTitle();
}
</script>
</head>
<body>
<h2>ํค๋๋ผ์ธ ๋ด์ค</h2>
<hr/>
<br/>
<div onmouseover="showNewsDiv();" onmouseout="hideNewsDiv();" style="display: block; border: 3px solid; width: 50%; margin: 0 auto;">๋ด์ค๋ณด๊ธฐ</div>
<div id="newsDiv" class="news"></div>
<hr/>
</body>
</html>


newsTitleCSV_ok.jsp //๋ด์ค๊ธฐ์ฌ
<%@page import="java.util.Date"%>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%
request.setCharacterEncoding("UTF-8");
String cp = request.getContextPath();
%>
<!-- ! = ์ ์ญ๋ณ์๋ก ๋น ์ง๊ธฐ -->
<%!
String[] newsTitle = {
"๊น๊ฒฝ์จ, ์ค ๋ํต๋ น '๋ช
ํ๋ฐฑ' ์
์ฅ์ '๋ค์ฏ๊ธ์๋ก ์์ฝ์ต๋๋ค'",
"๋ช
๋ ์๊ถ์ด ๋ฌ๋ผ์ก๋คโฆ ์ธ๊ตญ์ธ ๊ด๊ด๊ฐ ์ฆ๊ฐ์ '1์ ์๊ถ' ํํ",
"'์ ์ฑ์ธ์ธ๋ฐ์' ๊ฑฐ์ง๋ง์ ์์ ์ ยท๋ด๋ฐฐ ํ๋งคํ ์
์ฃผ๋ '๊ตฌ์ '",
"๋ฒ์ '๋ํต๋ น์ค, ์ค ๋ํต๋ น ๋ถ์ฐ ํ์ง ํ์๋น ๊ณต๊ฐํด์ผ'",
"๊ฐ์ด์ด ๋จน๋จนยทยทยท์ผ์ ์นจ๋ ์ ๋ถ๊ทน๊ณฐ์ '๋ถ์ํ ์ชฝ์ '",
"'์ ๊ฑฐ๋ฒ ์๋ฐ' ์์ข
์ฑ, ์ง์ญ 4์ยท์ง์ 2๋
ํ์ ยทยทยท์์์ง ์์ค"
};
/*
6|๊น๊ฒฝ์จ, ์ค ๋ํต๋ น '๋ช
ํ๋ฐฑ' ์
์ฅ์ '๋ค์ฏ๊ธ์๋ก ์์ฝ์ต๋๋ค'[2024-02-02 ์คํ2:21]*
๋ช
๋ ์๊ถ์ด ๋ฌ๋ผ์ก๋คโฆ ์ธ๊ตญ์ธ ๊ด๊ด๊ฐ ์ฆ๊ฐ์ '1์ ์๊ถ' ํํ[2024-02-02 ์คํ2:21]*
.
.
.
*/
%>
<%
out.print(newsTitle.length + "|"); //๋ฐฐ์ด์์ ์๋ ๋ฐ์ดํฐ 6(์ค)๊ฐ๊ฐ ๋ค์ด์ด
for(int i = 0; i<newsTitle.length;i++){
out.print(newsTitle[i]+"[" + new Date() + "]");
if(i!=(newsTitle.length-1)){
out.print("*");
}
}
%>
---


---