Servlet(서블릿):
단점 : 폼 1개 1개의 서블릿이 필요
장점 : request,response 객체 생성
확장자 패턴 :
폼이 여러개가 있어도 action="????.do"
한개의 서블릿에서 매핑명을 / input => *.do
로 인식하는 서블릿을 FrontController 라고 지칭..
출력형식
1. 표현식 <%=필드명%>
2. el 표기법 ... JSTL 표기법
라이브러리를 3개 다운설치
<%@ 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>
<a href="input.jsp">입력</a> 
<a href="out.do">출력</a> 
</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>
<form action="input.do" method="get">
<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>
</body>
</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;
int kor,eng,mat,tot;
%>
<%
String sql ="select * from db02101";
psmt = con.prepareStatement(sql);
rs = psmt.executeQuery();
%>
<table border="1" align="center">
<tr>
<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>
</tr>
<%
}
%>
</table>
<div align="center">
<a href="index.jsp">메뉴로</a>
</div>
</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;
@WebServlet("*.do")
public class FrontController 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 FrontController() 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 {
//1. 전체경로 파악 경로+파일명
String uri = request.getRequestURI();
System.out.println("전체경로 : "+uri);
//2. 전체경로 중에서 경로만
String path = request.getContextPath();
System.out.println("경로 : "+path);
//파일명
String filename = uri.substring(path.length());
System.out.println("파일명 : "+filename);
if(filename.equals("/input.do")) { //input.do 폼에 내용을 처리
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 ="insert into db02101 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();
}
}
else if(filename.equals("/out.do")) {
response.sendRedirect("out.jsp");
}
}
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>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="input.jsp">입력</a> 
<a href="out.do">출력</a> 
</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>
<form action="input.do" method="get">
<table border="1" align="center">
<tr>
<th>아이디</th>
<td><input type="text" name="id"></td>
</tr>
<tr>
<th>비밀번호</th>
<td><input type="password" name="pw"></td>
</tr>
<tr>
<th>전화번호</th>
<td><input type="text" name="phone"></td>
</tr>
<tr>
<th>성별</th>
<td>
<select name="gender">
<option name="gender" value="남자">남자
<option name="gender" value="여자">여자
</select>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="전송">
<input type="reset" value="리셋">
</td>
</tr>
</table>
</form>
</body>
</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 id,pw,phone,gender;
%>
<%
String sql = "select * from db02102";
psmt = con.prepareStatement(sql);
rs = psmt.executeQuery();
%>
<table border="1" align="center" width="600px">
<tr>
<th>아이디</th><th>비밀번호</th><th>전화번호</th><th>성별</th>
<th>비고</th>
</tr>
<%
while(rs.next()) {
id = rs.getString(1);
pw = rs.getString(2);
phone = rs.getString(3);
gender = rs.getString(4);
%>
<tr>
<td><%=id%></td><td><%=pw%></td>
<td><%=phone%></td><td><%=gender%></td>
<td>
<a href="delete.jsp?id=<%=id%>">❌</a>
<a href="modify1.jsp?id=<%=id%>">✅</a>
</td>
</tr>
<%
}
%>
</table>
<div align="center">
<a href="index.jsp">인덱스로</a>
</div>
</body>
</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 did;
%>
<%
request.setCharacterEncoding("utf-8");
did = request.getParameter("id");
String sql = "delete from db02102 where id =?";
psmt = con.prepareStatement(sql);
psmt.setString(1, did);
psmt.executeUpdate();
response.sendRedirect("out.jsp");
%>
</body>
</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 mid;
String id,pw,phone,gender;
%>
<%
request.setCharacterEncoding("utf-8");
mid = request.getParameter("id");
String sql = "select * from db02102 where id =?";
psmt = con.prepareStatement(sql);
psmt.setString(1, mid);
rs = psmt.executeQuery();
if(rs.next()) {
id = rs.getString(1);
pw = rs.getString(2);
phone = rs.getString(3);
gender = rs.getString(4);
%>
<form action="modify.do" method="get">
<table border="1" align="center">
<tr>
<th>아이디</th>
<td><input type="text" name="id" value="<%=id%>" readonly></td>
</tr>
<tr>
<th>비밀번호</th>
<td><input type="password" name="pw" value="<%=pw%>"></td>
</tr>
<tr>
<th>전화번호</th>
<td><input type="text" name="phone" value="<%=phone%>"></td>
</tr>
<tr>
<th>성별</th>
<td>
<select name="gender">
<option name="gender" value="남자">남자
<option name="gender" value="여자">여자
</select>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="수정">
<input type="reset" value="리셋">
</td>
</tr>
<%
}
%>
</table>
</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;
@WebServlet("*.do")
public class FrontController 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 FrontController() 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 {
String uri = request.getRequestURI();
String path = request.getContextPath();
String filename = uri.substring(path.length());
if(filename.equals("/input.do")) {
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
String id = request.getParameter("id");
String pw = request.getParameter("pw");
String phone = request.getParameter("phone");
String gender = request.getParameter("gender");
String sql = "insert into db02102 values (?,?,?,?)";
try {
psmt = con.prepareStatement(sql);
psmt.setString(1, id);
psmt.setString(2, pw);
psmt.setString(3, phone);
psmt.setString(4, gender);
int k = psmt.executeUpdate();
if(k==1) {
response.sendRedirect("index.jsp");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
else if(filename.equals("/out.do")) {
response.sendRedirect("out.jsp");
}
else if(filename.equals("/modify.do")) {
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
String id = request.getParameter("id");
String pw = request.getParameter("pw");
String phone = request.getParameter("phone");
String gender = request.getParameter("gender");
String sql = "update db02102 set pw=?,phone=?,gender=? where id=?";
try {
psmt = con.prepareStatement(sql);
psmt.setString(1, pw);
psmt.setString(2, phone);
psmt.setString(3, gender);
psmt.setString(4, id);
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>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="input.jsp">입력</a> 
<a href="out.do">출력</a> 
<a href="delete.jsp">삭제</a> 
</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>
<form action="input.do" method="get">
<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>
<div align="center">
<a href="index.jsp">홈으로</a>
</div>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1" align="center">
<tr>
<th>이름</th><th>국어</th><th>영어</th><th>수학</th><th>총점</th>
</tr>
<c:forEach items="${list}" var="my">
<tr>
<td>${my.name}</td><td>${my.kor}</td><td>${my.eng}</td>
<td>${my.mat}</td><td>${my.tot}</td>
</tr>
</c:forEach>
</table>
<div align="center">
<a href="index.jsp">홈으로</a>
</div>
</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>
<form action="delete.do" method="get">
<table border="1" align="center">
<tr>
<th>이름</th>
<td><input type="text" name="name"></td>
</tr>
<td colspan="2" align="center">
<input type="submit" value="삭제">
<input type="reset" value="리셋">
</td>
</tr>
</table>
</form>
<div align="center">
<a href="index.jsp">홈으로</a>
</div>
</body>
</html>
package com.ezen;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
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("*.do")
public class FrontController extends HttpServlet {
private static final long serialVersionUID = 1L;
public FrontController() throws ClassNotFoundException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String uri = request.getRequestURI();
String path = request.getContextPath();
String filename = uri.substring(path.length());
service comm = null;
String vpage = null;
if(filename.equals("/input.do")) {
comm = new Input();
comm.exec(request, response);
vpage="index.jsp";
}
else if(filename.equals("/out.do")) {
comm = new output();
comm.exec(request, response);
vpage="out2.jsp";
}
else if(filename.equals("/delete.do")) {
comm = new delete();
comm.exec(request, response);
vpage="index.jsp";
}
RequestDispatcher rd = request.getRequestDispatcher(vpage);
rd.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
package com.ezen;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface service {
public void exec(HttpServletRequest request,HttpServletResponse response);
}
package com.ezen;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Input implements service{
@Override
public void exec(HttpServletRequest request, HttpServletResponse response) {
try {
DAO dao = new DAO();
dao.insert(request);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
package com.ezen;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class output implements service{
@Override
public void exec(HttpServletRequest request, HttpServletResponse response) {
DAO dao;
try {
dao = new DAO();
ArrayList<DTO> list = dao.out();
request.setAttribute("list", list);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
package com.ezen;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class delete implements service{
@Override
public void exec(HttpServletRequest request, HttpServletResponse response) {
try {
String dname = request.getParameter("name");
DAO dao = new DAO();
dao.delete(dname);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
package com.ezen;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
public class DAO { //DB에 자료를 저장,출력
Connection con;
PreparedStatement psmt;
ResultSet rs;
String driver="oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:xe";
ArrayList<DTO> list = new ArrayList<DTO>();
public DAO() throws ClassNotFoundException {
Class.forName(driver);
try {
con=DriverManager.getConnection(url, "ezen", "12345");
} catch (SQLException e) {
e.printStackTrace();
}
}
//저장기능
public void insert(HttpServletRequest request) {
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 = "insert into db02101 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);
psmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
//출력기능
public ArrayList<DTO> out() {
String sql = "select * from db02101";
try {
psmt = con.prepareStatement(sql);
rs = psmt.executeQuery(); //el표기법
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);
DTO dto = new DTO();
dto.setName(name);
dto.setKor(kor);
dto.setEng(eng);
dto.setMat(mat);
dto.setTot(tot);
list.add(dto);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
//삭제기능
public void delete(String dname) {
String sql = "delete from db02101 where name=?";
try {
psmt = con.prepareStatement(sql);
psmt.setString(1, dname);
psmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
'JSP' 카테고리의 다른 글
230214_서블릿 (0) | 2023.02.14 |
---|---|
230213_서블릿 (0) | 2023.02.13 |
230209_서블릿 (0) | 2023.02.09 |
230208_서블릿 (0) | 2023.02.08 |
230207_기본 (0) | 2023.02.07 |
댓글