무료 Ajax 강의 배우기
무료 Ajax 강의 배우기
<%@ 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>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
<script type="text/javascript">
$(function () {
$("input").eq(3).click(function () {
var id= $("input:first").val();
var name = $("input").eq(1).val();
var xmlhttp;
if(window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}else{
xmlhttp = new ActiveXObeject("Microsoft.XMLHTTP");
}
xmlhttp.open("Get", "test.check?id="+id+"&name="+name,true);
xmlhttp.send();
xmlhttp.onreadystatechange = function () {
if(xmlhttp.readyState==4 && xmlhttp.status==200){
var data = xmlhttp.responseText;
$("input").eq(2).val(data);
}
}
});
});
</script>
</head>
<body>
<h2> PW 찾기 </h2>
<form action="#">
ID <input type="text" name="id"><br>
NAME <input type="text" name="name"><br>
당신의 Password <input type="text" name="pw"><br>
<input type="button" value="pw찾기">
<input type="submit" value="login">
</form>
</body>
</html>
jsp 파일을 하나 만들어 준다
코드는 바로 위의 것
코드는 이런식
바디는 이런식
web.xml을 만들어준다
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>Ajax_1103</display-name>
<servlet>
<servlet-name>go</servlet-name>
<servlet-class>Join.JoinCheck</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>go</servlet-name>
<url-pattern>*.check</url-pattern>
</servlet-mapping>
</web-app>
이게 코드
복사 해도 무방하다
서블렛은 제대로 만들어준다
Join.JoinCheck
Join 패키지 안에
JoinCheck 라는 서블렛이 필요하다
소,대문자 주의
doPost 메서드 안에
이것을 집어 넣는다
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String id = request.getParameter("id");
String name= request.getParameter("name");
String user="team01";
String password="team01";
String url="jdbc:oracle:thin:@192.168.30.14:1521:xe";
String driver="oracle.jdbc.driver.OracleDriver";
String result="";
try {
Class.forName(driver);
Connection con = DriverManager.getConnection(url, user, password);
String sql ="select pw from member where id=? and name=?";
PreparedStatement st = con.prepareStatement(sql);
st.setString(1, id);
st.setString(2, name);
ResultSet rs = st.executeQuery();
if(rs.next()){
result=rs.getString("pw");
}else{
result="pw 없네요";
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.setAttribute("result", result);
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.println(result);
}
이렇게 뜨고 pw 찾기를 눌렀을때
DB에서 불러온다면 성공!
이때 주의사항은
doPost에서 굵은 글씨는 자신의 DB와 맞게 세팅해야 한다는 점 유의바란다