//Google AdSense
<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/css/main.css" />
</head>
<body>
<%@ include file="/module/top.jsp" %>
<%@ include file="/module/left.jsp" %>
작업했던 처리과정 파일들의 상단 하단에 layout 코드 삽입
<%@ include file="/module/hadan.jsp" %>
</body>
</html>


로그인 process를 db와 연결하기

<%@ 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">
<%
String id = request.getParameter("id");
String pw = request.getParameter("pw");
System.out.println(id + "<- id");
System.out.println(pw + "<- pw");
String dbid = "id001";
String dbpw = "pw001";
String dblevel = "관리자";	//관리자 또는 판매자 또는 구매자 변경해서 테스트
String dbname = "김길동";
String alert = null;
if(id.equals(dbid)){
	System.out.println("01 아이디 일치 조건");
	if(pw.equals(dbpw)){
		System.out.println("03 로그인성공 조건");	
		//response.sendRedirect(request.getContextPath() + "/index.jsp");
		session.setAttribute("S_LEVEL", dblevel);
		session.setAttribute("S_NAME", dbname);
		alert = "로그인성공";
	}else{
		System.out.println("04 비번 불일치 조건");	
		alert = "비번 불일치";
	}
}else{
	System.out.println("02 아이디 불일치 조건");
	alert = "아이디 불일치";
}
%>
<script type="text/javascript">
	alert('<%= alert %>');
	location.href='<%= request.getContextPath()%>/index.jsp';
</script>

<%@ 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">
<%@ page import = "java.sql.DriverManager" %>
<%@ page import = "java.sql.Connection" %>
<%@ page import = "java.sql.PreparedStatement" %>
<%@ page import = "java.sql.ResultSet" %>
<%@ page import = "java.sql.SQLException" %>

<%
String id = request.getParameter("id");
String pw = request.getParameter("pw");
System.out.println(id + "<- id");
System.out.println(pw + "<- pw");

String dbid = null;
String dbpw = null;
String dblevel = null;
String dbname = null;
String alert = null;

Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;

Class.forName("com.mysql.jdbc.Driver");
try{
	String jdbcDriver = "jdbc:mysql://localhost:3306/dev35db?" +
			"useUnicode=true&characterEncoding=euckr";
	String dbUser = "dev35id";
	String dbPass = "dev35pw";
	conn = DriverManager.getConnection(jdbcDriver, dbUser, dbPass);
	System.out.println(conn + "<-- conn");
	pstmt = conn.prepareStatement("select * from tb_member WHERE m_id = ?");
	pstmt.setString(1, id);
	System.out.println(pstmt + "<-- pstmt");
	rs = pstmt.executeQuery();
	System.out.println(rs + "<-- rs");
	if(rs.next()){
		System.out.println("if 조건문 통과-------------");
		System.out.println("01 아이디 일치 조건");
		
		dbid = rs.getString("m_id");
		dbpw = rs.getString("m_pw");
		dblevel = rs.getString("m_level");
		dbname = rs.getString("m_name");
		System.out.println(dbid + "<- dbid");
		System.out.println(dbpw + "<- dbpw");
		System.out.println(dblevel + "<- dblevel");
		System.out.println(dbname + "<- dbname");
			
			if(pw.equals(dbpw)){
				System.out.println("03 로그인성공 조건");	
				//response.sendRedirect(request.getContextPath() + "/index.jsp");
				session.setAttribute("S_LEVEL", dblevel);
				session.setAttribute("S_NAME", dbname);
				alert = "로그인성공";
			}else{
				System.out.println("04 비번 불일치 조건");	
				alert = "비번 불일치";
			}
	}else{
			System.out.println("02 아이디 불일치 조건");
			alert = "아이디 불일치";
	}
} catch(SQLException ex) {
	System.out.println(ex.getMessage());
	ex.printStackTrace();
} finally {
	if (rs != null) try { rs.close(); } catch(SQLException ex) {}
	if (pstmt != null) try { pstmt.close(); } catch(SQLException ex) {}
	if (conn != null) try { conn.close(); } catch(SQLException ex) {}
}



%>
<script type="text/javascript">
	alert('<%= alert %>');
	location.href='<%= request.getContextPath()%>/index.jsp';
</script>

db tb_member에 입력된 datas

로그인시 db data가 출력됨 / 로그인 실패시에 alert도 잘 뜸

'JSP' 카테고리의 다른 글

jsp,mysql,java  (0) 2020.04.09
[JSP + MySQL]  (0) 2020.04.02
[JSP + MySQL] 검색  (0) 2020.03.27
[JSP + MySQL] PreparedStatement vs. Statement / Execute vs. ExecuteQuery vs. ExecuteUpdate  (0) 2020.03.26

+ Recent posts