로그인 체크 서블릿

(。◠ ◠。)·2022년 7월 5일
0

테이블에 있는 데이터를 가져와서 로그인폼을 만들어 보았다.

protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String url = "jdbc:oracle:thin:@localhost:1521/xe";
		String id = "green01";
		String pw = "1234";

		String mId = request.getParameter("id");
		String mPw = request.getParameter("pw");
		//공백검증
		if (mId.length() == 0 || mPw.length() == 0) {
			out.print("<script>alert('ID PW 를 입력하세요.');");
			out.print("location.href  ='index.jsp'");
			out.print("</script>");
		} else {
			try {
				Class.forName("oracle.jdbc.OracleDriver");
				Connection conn = DriverManager.getConnection(url, id, pw);
				String sql = "select * from member where id = ?";
				PreparedStatement pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, mId);
				pstmt.executeUpdate();
				ResultSet rs = pstmt.executeQuery();

				if (rs.next()) {
					if (rs.getString(1).equals(mId) && rs.getString(2).equals(mPw)) {
						HttpSession session = request.getSession();
						session.setAttribute("id", mId);
						session.setAttribute("pw", mPw);
						response.sendRedirect("webhard.jsp");
                        //아이디와 비밀번호가 같으면 세션에 저장하고 페이지 이동
					}
				} else {
					// response.sendRedirect("index.jsp");
					out.print("<script>alert('틀렸습니다.');");
					out.print("location.href  ='index.jsp'");
					out.print("</script>");
                    //out.print로 페이지 이동
				}

			} catch (SQLException | ClassNotFoundException e) {
				e.printStackTrace();
			}
		}

	}
profile
화이탱!

0개의 댓글

관련 채용 정보