본문 바로가기
JSP

230208_서블릿

by 경 훈 2023. 2. 8.

mapping(맵핑): 연결의 뜻

web.xml 파일이 필요할수 있다.->프로젝트 생성시

보안에 유리하기 때문에 복잡하게 느낄수있다.

서블릿도 자바다.. 즉 클래스

예) 아이디와 패스워드

폼에서 <form action="onputsave.jsp" method="post">

                               =>맵핑명

맵핑 방법 2

1.web.xml 파일을 이용

2.서블릿 코드에서 직접 입력

 

서블릿의 단점 : 폼1 서블릿1 각각필요

보완 : 확장자 패턴

 

<%@ 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="mysave" method="post">
		아이디 : <input type="text" name="id"><br>
		이름은 : <input type="text" name="name"><br>
		국어점 : <input type="text" name="kor"><br>
		<input type="submit" value="전송"><br>
	</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.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("/mysave")
public class mySample 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 mySample() 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 id = request.getParameter("id");
		String name =request.getParameter("name");
		int kor = Integer.parseInt(request.getParameter("kor")); 
		//
		String sql ="insert into my208 values(?,?,?)";
		try {
			psmt = con.prepareStatement(sql);
			psmt.setString(1, id);
			psmt.setString(2, name);
			psmt.setInt(3, kor);
			psmt.executeUpdate();
			response.sendRedirect("mymenu.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="myinput.jsp">자료입력</a>
	<a href="myout.jsp">자료출력</a>
</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,name;
	int kor;
	%>
	<%
	String sql ="select * from my208 order by kor desc";
	psmt = con.prepareStatement(sql);
	rs = psmt.executeQuery();
	%>
	<table border="1" align="center">
		<tr>
			<th>아이디</th><th>이름</th><th>국어</th>
		</tr>
	<%
	while(rs.next()) {
		id = rs.getString(1);
		name = rs.getString(2);
		kor = rs.getInt(3);
	%>	
		<tr>
			<td><%=id%></td><td><%=name%></td><td><%=kor%></td>
		</tr>
	<%
	}
	%>
	</table>
	<div align="center">
		<a href="mymenu.jsp"><button>메뉴로</button></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>
	<a href="input.jsp"><button>입력</button></a>
	<a href="out.jsp"><button>출력</button></a>
	<a href="login.jsp"><button>로그인</button></a>
	<a href="logout.jsp"><button>로그아웃</button></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="save1">
		<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="name"></td>
			</tr>
			<tr>
				<th>전화</th>
				<td><input type="text" name="phone"></td>
			</tr>
			<tr>
				<th>거주지</th>
				<td><input type="text" name="address"></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"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	입력한 자료가 잘 저장되었습니다<br>
	<a href="menu.jsp"><button>메뉴로</button></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="session1">
		<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>
				<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;
	%>
	<%
	HttpSession hs = request.getSession();
	id = (String)hs.getAttribute("id");
	//hs.setAttribute("id", id);
	String sql = "delete from db02081 where id =?";
	psmt = con.prepareStatement(sql);
	psmt.setString(1, id);
	int k = psmt.executeUpdate();
	if(k==1) {
		response.sendRedirect("menu.jsp");
	}
	else {
		out.print("없는 아이디입니다");
		out.print("<a href='input.jsp'>입력으로</a>");
	}
	%>
</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,name,phone,address;
	%>
	<%
	String sql ="select * from db02081";
	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()) {
		id = rs.getString(1);
		pw = rs.getString(2);
		name = rs.getString(3);
		phone = rs.getString(4);
		address = rs.getString(5);
	%>	
		<tr>
			<td><%=id%></td><td><%=pw%></td><td><%=name%></td>
			<td><%=phone%></td><td><%=address%></td>
		</tr>
	<%
	}
	%>
	</table>
	<div align="center">
		<a href="menu.jsp"><button>메뉴로</button></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>
	<%!
	String id,pw;
	%>
	<%
	HttpSession hs = request.getSession();
	id = (String)hs.getAttribute("id");
	pw = (String)hs.getAttribute("pw");
	%>
	<h1>
	<%=id%>님 반갑습니다!!<br>
	정상적으로 로그인이 되었습니다.<br>
	아이디 : <%=id%> <br>
	비밀번호 : <%=pw%>
	</h1>
	<div align="center">
		<a href="menu.jsp"><button>메뉴로</button></a> &emsp;
		<a href="delete.jsp"><button>삭제</button></a> &emsp;
		<a href="modify1.jsp"><button>수정</button></a> &emsp;
	</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 id;
	%>
	<%
	HttpSession hs = request.getSession();
	id = (String)hs.getAttribute("id");
	String sql = "select * from db02081 where id=?";
	psmt = con.prepareStatement(sql);
	psmt.setString(1, id);
	rs = psmt.executeQuery();
	if(rs.next()) {
		String id = rs.getString(1);
		String pw = rs.getString(2);
		String name = rs.getString(3);
		String phone = rs.getString(4);
		String address = rs.getString(5);
	%>
	<form action="modify2">
		<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="name" value="<%=name%>"></td>
			</tr>
			<tr>
				<th>전화</th>
				<td><input type="text" name="phone" value="<%=phone%>"></td>
			</tr>
			<tr>
				<th>거주지</th>
				<td><input type="text" name="address" value="<%=address%>"></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"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%!
	String id;
	%>
	<%
	HttpSession hs = request.getSession();
	id = (String)hs.getAttribute("id");
	%>
	<h1>
		<%=id%>님!! 로그아웃 하셨어요!!<br>
		다음에 또 뵈요!!		
	</h1>
	<%
	hs.invalidate();
	%>
</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("/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 id = request.getParameter("id");
		String pw = request.getParameter("pw");
		String name = request.getParameter("name");
		String phone = request.getParameter("phone");
		String address = request.getParameter("address");
		String sql ="insert into db02081 values (?,?,?,?,?)";
		try {
			psmt = con.prepareStatement(sql);
			psmt.setString(1, id);
			psmt.setString(2, pw);
			psmt.setString(3, name);
			psmt.setString(4, phone);
			psmt.setString(5, address);
			int n = psmt.executeUpdate();
			if(n==1) {
				response.sendRedirect("memberok.jsp");
			}
			else {
				response.sendRedirect("menu.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("/session1")
public class session1 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 session1() 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 id = request.getParameter("id");
		String pw = request.getParameter("pw");
		if(id==null) {
			response.sendRedirect("menu.jsp");
		}
		else {
			HttpSession hs = request.getSession();
			hs.setAttribute("id", id);
			hs.setAttribute("pw", pw);
			String sql = "select * from db02081 where id=? and pw=?";
			try {
				psmt = con.prepareStatement(sql);
				psmt.setString(1, id);
				psmt.setString(2, pw);
				rs = psmt.executeQuery();
				if(rs.next()) {
					//회원
					response.sendRedirect("loginok.jsp");
				}
				else {
					response.sendRedirect("menu.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 id = request.getParameter("id");
		String pw = request.getParameter("pw");
		String name = request.getParameter("name");
		String phone = request.getParameter("phone");
		String address = request.getParameter("address");
		String sql ="update db02081 set pw=?,name=?,phone=?,address=? where id=?";
		try {
			psmt = con.prepareStatement(sql);
			psmt.setString(1, pw);
			psmt.setString(2, name);
			psmt.setString(3, phone);
			psmt.setString(4, address);
			psmt.setString(5, id);
			int n = psmt.executeUpdate();
			if(n==1) {
				response.sendRedirect("memberok.jsp");
			}
			else {
				response.sendRedirect("menu.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"%>
<%@include file="dbcon.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%!
	String id,pw,name,phone,address;
	%>
	<%
	HttpSession hs = request.getSession();
	id = (String)hs.getAttribute("id");
	if(id==null) {
		out.println("죄송합니다 볼 권한이 없습니다"+"<br>");
		out.println("<a href='menu.jsp'>메뉴</a>");
	}
	else {%>
	<%
	String sql ="select * from db02081";
	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()) {
		id = rs.getString(1);
		pw = rs.getString(2);
		name = rs.getString(3);
		phone = rs.getString(4);
		address = rs.getString(5);
	%>	
		<tr>
			<td><%=id%></td><td><%=pw%></td><td><%=name%></td>
			<td><%=phone%></td><td><%=address%></td>
		</tr>
	<%
	}
	%>
	</table>
	<div align="center">
		<a href="menu.jsp"><button>메뉴로</button></a>
	</div>
	<%	
	}
	%>
	
</body>
</html>

'JSP' 카테고리의 다른 글

230210_서블릿  (0) 2023.02.10
230209_서블릿  (0) 2023.02.09
230207_기본  (0) 2023.02.07
230206_기본  (0) 2023.02.06
230203_DB  (0) 2023.02.03

댓글