보여질 메인페이지
index.jsp
<%--
Created by IntelliJ IDEA.
User: console
Date: 2020-04-06
Time: 오전 5:54
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>MainPage</title>
</head>
<body>
<% if(session.getAttribute("id") == null) { %>
이 페이지는 로그인 회원만 접근가능합니다. <br>
<a href="login.jsp">로그인하기</a>
<%
}
else
{
String id = (String)session.getAttribute("id");
out.print(id + "님 반갑습니다.");
}
%>
</body>
</html>
로그인페이지
login.jsp
<%--
Created by IntelliJ IDEA.
User: console
Date: 2020-03-31
Time: 오전 2:50
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="example.*" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<form action="logincheck.jsp" method="GET">
<input type="text" name = "id"> <br>
<input type="text" name = "pw"> <br>
<input type="submit">
</form>
<a href="register.jsp">회원가입</a>
</body>
</html>
로그인체크페이지
logincheck.jsp
<%--
Created by IntelliJ IDEA.
User: console
Date: 2020-03-31
Time: 오전 5:34
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="example.*" %>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>logincheck</title>
</head>
<body>
<%
String id = request.getParameter("id");
String pw = request.getParameter("pw");
Sha sha = new Sha();
pw = sha.sha256(pw);
String dbid = "DBID";
String dbpw = "DBPW";
String dbname = "DBNAME";
String tablename = "java";
Connection conn = null;
ResultSet rs = null;
PreparedStatement pstmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/alpacao?useUnicode=true&characterEncoding=utf-8", "alpacao", "alpaca16");
String sql = "select count(*) as cnt from "+tablename +" where id=? and pw=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.setString(2, pw);
rs = pstmt.executeQuery();
rs.next();
if(rs.getString("cnt").equals("1"))
{
out.print("로그인성공o");
session.setAttribute("id",id); //세션으로 id값 전달.
response.sendRedirect("index.jsp");
}
else
{
%>
<script>
alert("로그인에 실패하였습니다.");
location.href="http://alpacao.cafe24.com/test_war/login.jsp"; // 로그인페이지로 이동시킴
</script>
<%
}
}catch(Exception e) {
out.print(e);
}
%>
</body>
</html>
동작순서
login.jsp
id, pw 입력받고 이를 logincheck.jsp로 전달.
→
logincheck.jsp
login.jsp에서 넘어온 파라미터값중 pw값을 example.sha.class로 넘김
→
sha.class
logincheck.jsp에서 넘어온 pw값을 sha256인코딩 후 다시 리턴
→
logincheck.jsp
sha.class에서 sha256인코딩 후 넘어온 pw값과 id값을 DB조회.
이후 id,pw모두 동일한 값이 있으면 id값을 세션처리. 이후 메인 index.jsp페이지로 세션과 함께 전달.
동일한 값이 없다면 로그인 실패처리.
'JAVA JSP' 카테고리의 다른 글
[JSP] SHA-256인코딩 (0) | 2020.04.10 |
---|---|
[JSP] 팔로우, 팔로워 불러오기 (0) | 2020.04.10 |
[IntelliJ] .war배포파일 만들기 (0) | 2020.04.07 |
[JSP] 로그아웃처리 (0) | 2020.04.07 |
[JSP] 내가 만든 Class import하기 (0) | 2020.04.07 |
최근댓글