동기식 전송 : 신호를 맞추는 방식
비동기식 전송 : 일정한 순서가 없는 방식
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<link rel=stylesheet a href ="css/sample.css">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<header>
<h1>성적 관리 프로그램 [ver1.0]</h1>
</header>
<section>
<a href="index.jsp">홈으로</a> 
<a href="input.jsp">성적입력</a> 
<a href="out.jsp">성적출력</a> 
<a href="delete1">최근등록삭제</a> 
<a href="search1.jsp">검색</a> 
</section>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="top.jsp" %>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function check() {
}
</script>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<section>
<form action="save1" method="post" name="form">
<table border="1" align="center">
<tr>
<th>이름</th>
<td><input type="text" name="name"></td>
</tr>
<tr>
<th>국어</th>
<td><input type="text" name="kor"></td>
</tr>
<tr>
<th>영어</th>
<td><input type="text" name="eng"></td>
</tr>
<tr>
<th>수학</th>
<td><input type="text" name="mat"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="전송">
<input type="reset" value="리셋">
</td>
</tr>
</table>
</form>
</section>
<jsp:include page="footer.jsp"/>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="top.jsp" %>
<%@include file="dbcon.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<section>
<%!
String name;
int kor,eng,mat,tot;
%>
<%
String sql = "select * from db02091 order by tot desc";
psmt = con.prepareStatement(sql);
rs = psmt.executeQuery();
%>
<table border="1" align="center" width="800px">
<tr>
<th>이름</th><th>국어</th><th>영어</th><th>수학</th><th>총점</th>
<th>비고</th>
</tr>
<%
while(rs.next()) {
name = rs.getString(1);
kor = rs.getInt(2);
eng = rs.getInt(3);
mat = rs.getInt(4);
tot = rs.getInt(5);
%>
<tr>
<td><%=name%></td><td><%=kor%></td><td><%=eng%></td>
<td><%=mat%></td><td><%=tot%></td>
<td>
<a href="delete.jsp?name=<%=name%>">❌</a>
<a href="modify1.jsp?name=<%=name%>">✅</a>
</td>
</tr>
<%
}
%>
</table>
</section>
<jsp:include page="footer.jsp"/>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="dbcon.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%!
String name;
%>
<%
request.setCharacterEncoding("utf-8");
name = request.getParameter("name");
String sql = "delete from db02091 where name=?";
psmt = con.prepareStatement(sql);
psmt.setString(1, name);
psmt.executeUpdate();
response.sendRedirect("out.jsp");
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="top.jsp" %>
<%@include file="dbcon.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<section>
<%!
String mname;
String name;
int kor,eng,mat,tot;
%>
<%
request.setCharacterEncoding("utf-8");
mname = request.getParameter("name");
String sql = "select * from db02091 where name=?";
psmt = con.prepareStatement(sql);
psmt.setString(1, mname);
rs = psmt.executeQuery();
if(rs.next()) {
name = rs.getString(1);
kor = rs.getInt(2);
eng = rs.getInt(3);
mat = rs.getInt(4);
tot = rs.getInt(5);
%>
<form action="modify2" method="post" name="form">
<table border="1" align="center">
<tr>
<th>이름</th>
<td><input type="text" name="name" value="<%=name%>" readonly></td>
</tr>
<tr>
<th>국어</th>
<td><input type="text" name="kor" value="<%=kor%>"></td>
</tr>
<tr>
<th>영어</th>
<td><input type="text" name="eng" value="<%=eng%>"></td>
</tr>
<tr>
<th>수학</th>
<td><input type="text" name="mat" value="<%=mat%>"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="수정">
<input type="reset" value="리셋">
</td>
</tr>
<%
}
%>
</table>
</form>
</section>
<jsp:include page="footer.jsp"/>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="top.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<section>
<form action="search2.jsp">
<table border="1" align="center">
<tr>
<td>
<select name="sname">
<option value="name">이름
<option value="kor">국어점수
<option value="eng">영어점수
<option value="mat">수학점수
</select>
</td>
<td>
<input type="text" name="svalue">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="검색">
<input type="reset" value="취소">
</td>
</tr>
</table>
</form>
</section>
<jsp:include page="footer.jsp"/>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="top.jsp" %>
<%@include file="dbcon.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<section>
<%!
String sname,svalue;
%>
<%
request.setCharacterEncoding("utf-8");
sname=request.getParameter("sname");
svalue=request.getParameter("svalue");
String sql = "select * from db02091 where "+sname+" like ?";
psmt = con.prepareStatement(sql);
psmt.setString(1, '%'+svalue+'%');
rs = psmt.executeQuery();
%>
<table border="1" align="center" width="800px">
<tr>
<th>이름</th><th>국어</th><th>영어</th><th>수학</th><th>총점</th>
</tr>
<%
while(rs.next()) {
String name = rs.getString(1);
int kor = rs.getInt(2);
int eng = rs.getInt(3);
int mat = rs.getInt(4);
int tot = rs.getInt(5);
%>
<tr>
<td><%=name%></td><td><%=kor%></td><td><%=eng%></td>
<td><%=mat%></td><td><%=tot%></td>
</tr>
<%
}
%>
</table>
</section>
<jsp:include page="footer.jsp"/>
</html>
package com.ezen;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/save1")
public class save1 extends HttpServlet {
Connection con;
PreparedStatement psmt;
ResultSet rs;
String driver="oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:xe";
private static final long serialVersionUID = 1L;
public save1() throws ClassNotFoundException {
Class.forName(driver);
try {
con=DriverManager.getConnection(url, "ezen", "12345");
} catch (SQLException e) {
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
int kor = Integer.parseInt(request.getParameter("kor"));
int eng = Integer.parseInt(request.getParameter("eng"));
int mat = Integer.parseInt(request.getParameter("mat"));
int tot = kor+eng+mat;
HttpSession hs = request.getSession(); //사용자가 원하는 정보를 담아
hs.setAttribute("name", name);
String sql ="insert into db02091 values (?,?,?,?,?)";
try {
psmt = con.prepareStatement(sql);
psmt.setString(1, name);
psmt.setInt(2, kor);
psmt.setInt(3, eng);
psmt.setInt(4, mat);
psmt.setInt(5, tot);
int k = psmt.executeUpdate();
if(k==1) {
response.sendRedirect("index.jsp");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
package com.ezen;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/delete1")
public class delete1 extends HttpServlet {
Connection con;
PreparedStatement psmt;
ResultSet rs;
String driver="oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:xe";
private static final long serialVersionUID = 1L;
public delete1() throws ClassNotFoundException {
Class.forName(driver);
try {
con=DriverManager.getConnection(url, "ezen", "12345");
} catch (SQLException e) {
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
HttpSession hs = request.getSession();
String name = (String)hs.getAttribute("name");
String sql = "delete from db02091 where name=?";
try {
psmt=con.prepareStatement(sql);
psmt.setString(1, name);
psmt.executeUpdate();
response.sendRedirect("index.jsp");
} catch (SQLException e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
package com.ezen;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/modify2")
public class modify2 extends HttpServlet {
Connection con;
PreparedStatement psmt;
ResultSet rs;
String driver="oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:xe";
private static final long serialVersionUID = 1L;
public modify2() throws ClassNotFoundException {
Class.forName(driver);
try {
con=DriverManager.getConnection(url, "ezen", "12345");
} catch (SQLException e) {
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
int kor = Integer.parseInt(request.getParameter("kor"));
int eng = Integer.parseInt(request.getParameter("eng"));
int mat = Integer.parseInt(request.getParameter("mat"));
int tot = kor+eng+mat;
String sql ="update db02091 set kor=?,eng=?,mat=?,tot=? where name=?";
try {
psmt=con.prepareStatement(sql);
psmt.setInt(1, kor);
psmt.setInt(2, eng);
psmt.setInt(3, mat);
psmt.setInt(4, tot);
psmt.setString(5, name);
int k = psmt.executeUpdate();
if(k==1) {
response.sendRedirect("out.jsp");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#button").click(function () {
var num = $("#number").val(); //document.form1.number.value
$.ajax({
type:"post",
url:"bbb2.jsp",
data:{"num":num},
success:function(txt){
$("#result").html(txt);
}
}); //ajax의 종료
}); //button 종료
});
</script>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>비동기식 전송</h3>
<form name="form1">
넘길수 : <input type="text" name="number" id="number"><br>
<input type="button" value="전송" id="button">
<div id="result"></div>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%!
int number;
int gop;
%>
<%
request.setCharacterEncoding("utf-8");
number = Integer.parseInt(request.getParameter("num"));
for(int j=1;j<=9;j++) {
gop=number*j;
out.println(number+"*"+j+"="+gop+"<br>");
}
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#ajax").click(function () {
var name = $("#name").val();
var age = $("#age").val();
var tel = $("#tel").val();
var samdata = {"name":name,"age":age,"tel":tel};
var samdata = JSON.stringify(samdata);//키,값들의 데이터들을 json 형태로 변환
$.ajax({
type:"post",
url:"ajax1", //서블릿매핑명
async:false,
data:{jsondata:samdata},
success:function (data) {
alert("전송성공!!");
},
error:function (data) {
alert("에러가 발생!!");
}
});
});
});
</script>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form id="form1">
이름 : <input type="text" name="name" id="name"><br>
나이 : <input type="text" age="age" id="age"><br>
전화 : <input type="text" age="tel" id="tel"><br>
<a id="ajax" style="cursor: pointer">전송</a>
</form>
</body>
</html>
package com.ezen;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
@WebServlet("/ajax1")
public class AjaxTest1 extends HttpServlet {
Connection con;
PreparedStatement psmt;
ResultSet rs;
String driver="oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:xe";
private static final long serialVersionUID = 1L;
public AjaxTest1() throws ClassNotFoundException {
Class.forName(driver);
try {
con=DriverManager.getConnection(url, "ezen", "12345");
} catch (SQLException e) {
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
String jdata = request.getParameter("jsondata");
//제이슨 형태로 변환객체 생성
JSONParser jp = new JSONParser();
try {
//제이슨 타입의 객체 생성을 하는데-->
JSONObject dobject = (JSONObject)jp.parse(jdata);
String name = (String)dobject.get("name");
int age = Integer.parseInt((String)dobject.get("age"));
String tel = (String)dobject.get("tel");
String sql = "insert into ajax1 values (?,?,?)";
try {
psmt = con.prepareStatement(sql);
psmt.setString(1, name);
psmt.setInt(2, age);
psmt.setString(3, tel);
int k = psmt.executeUpdate();
if(k==1) {
System.out.println("디비에 저장성공");
}
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ParseException e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#check").click(function () {
$.ajax({
type:"post",
async:false,
url:"ajax2",
success:function(data) {
var adata = JSON.parse(data);
var out="";
out+="이름 나이 전화번호"+"<br>";
out+="=================<br>";
for(var i in adata.total){
out+=adata.total[i].name+"\t";
out+=adata.total[i].age+"\t";
out+=adata.total[i].tel+"<br>";
out+="=================<br>";
}
$("#out1").html(out);
},
error:function(data) {
alert("에러발생")
}
});
});
});
</script>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a id="check" style="cursor:pointer;">결과보기</a>
<div id="out1"></div>
</body>
</html>
package com.ezen;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
@WebServlet("/ajax2")
public class AjaxTest2 extends HttpServlet {
Connection con;
PreparedStatement psmt;
ResultSet rs;
String driver="oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:xe";
private static final long serialVersionUID = 1L;
public AjaxTest2() throws ClassNotFoundException {
Class.forName(driver);
try {
con=DriverManager.getConnection(url, "ezen", "12345");
} catch (SQLException e) {
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
JSONObject member = new JSONObject();
JSONArray array = new JSONArray();
JSONObject total = new JSONObject();
PrintWriter ppw = response.getWriter();
String sql = "select * from ajax1";
try {
psmt=con.prepareStatement(sql);
rs = psmt.executeQuery();
while(rs.next()) {
String name = rs.getString(1);
String age = rs.getString(2);
String tel = rs.getString(3);
member = new JSONObject();
member.put("name", name);
member.put("age", age);
member.put("tel", tel);
array.add(member);
}
total.put("total", array);
String str = total.toJSONString();
ppw.print(str);
} catch (SQLException e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
'JSP' 카테고리의 다른 글
230213_서블릿 (0) | 2023.02.13 |
---|---|
230210_서블릿 (0) | 2023.02.10 |
230208_서블릿 (0) | 2023.02.08 |
230207_기본 (0) | 2023.02.07 |
230206_기본 (0) | 2023.02.06 |
댓글