728x90
반응형
이제 아래와 같은 화면출력과 제어, DB연결을 할 MVC를 구성하도록 하자.
1. View(index.html)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="logincontroller.jsp?command=login" method="post">
<input type="hidden" name="command" value="login"/>
<table border="1">
<col width="100px"/>
<col width="100px"/>
<tr>
<th>ID</th>
<td><input type="text" name="myid"/></td>
</tr>
<tr>
<th>PW</th>
<td><input type="password" name="mypw"/></td>
</tr>
<tr>
<td colspan="2" align="right">
<input type="button" value="회원가입" onclick="location.href='logincontroller.jsp?command=registerform'"/>
<input type="submit" value="로그인"/>
</td>
</tr>
</table>
</form>
</body>
</html>
2. Controller
logincontroller.jsp(main): 모든 jsp파일은 logincontroller.jsp를 통해 실행되도록 구현했다.
<%@page import="java.util.List"%>
<%@page import="com.login.dto.myDto"%>
<%@page import="com.login.dao.myDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% request.setCharacterEncoding("UTF-8"); %>
<% response.setContentType("text/html; charset=UTF-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String command = request.getParameter("command");
System.out.printf("[%s\n]", command);
myDao dao = new myDao();
if(command.equals("login")){
//1. 값 받아주기
String myid = request.getParameter("myid");
String mypw = request.getParameter("mypw");
//2. db연결
myDto dto = dao.login(myid, mypw);
if(dto.getMyid() != null){
//side effect: myDao에서 myDto = null; 이라고 했기 때문에 id,pw가 일치하지않을때 전달되는 값이 null됨
//(1) 처음에 myDto = new myDto 선언해주거나
//(2) if(dto != null) 이렇게 하거나
// (1,2)번을 둘다 해주면 if(dto !=null)이 항상 참이되어서 항상 로그인 되어있다고 인식하니 주의!
//3. 보내줄 값 request에 저장
//session: 만료될 때 까지 프로젝트 전체에서 사용
session.setAttribute("login", dto);
//setMaxInactiveInterval(): default = 30분, 음수일때 무제한 >> 선언해준 시간동안 활동한다
session.setMaxInactiveInterval(10*60);
//4. page 이동
if(dto.getMyrole().equals("ADMIN")){
response.sendRedirect("adminmain.jsp");
} else if(dto.getMyrole().equals("USER")){
response.sendRedirect("usermain.jsp");
}
} else {
%>
<script type="text/javascript">
alert("login 실패: id나 pw를 다시 확인해주세요!");
location.href="index.html";
</script>
<% }
} else if(command.equals("logout")){
//session만료
session.invalidate();
response.sendRedirect("index.html");
} else if(command.equals("listall")){
//1. X
//2.
List<myDto> list = dao.selectList();
//3.
request.setAttribute("list", list);
//4.
pageContext.forward("adminlistall.jsp");
} else if(command.equals("listenabled")){ //"listenabled".equals(command) 이러게 쓰는게 더 좋음: command가 null이어도 nullpoint가 안됨
//1. X
//2.
List<myDto> list = dao.selectEnabled();
//3.
request.setAttribute("listenabled", list);
//4.
pageContext.forward("adlistenabled.jsp");
} else if("registerform".equals(command)){
//1.X
//2.
//3.
//4.
pageContext.forward("register.jsp");
} else if("register".equals(command)){
//1.
String myid = request.getParameter("myid");
String mypw = request.getParameter("mypw");
String myname = request.getParameter("myname");
String myaddr = request.getParameter("myaddr");
String myphone = request.getParameter("myphone");
String myemail = request.getParameter("myemail");
myDto dto = new myDto();
dto.setMyid(myid);
dto.setMypw(mypw);
dto.setMyname(myname);
dto.setMyaddr(myaddr);
dto.setMyphone(myphone);
dto.setMyemail(myemail);
//2.
int res = dao.register(dto);
//4.
if(res>0){
%>
<script type="text/javascript">
alert("회원가입 성공");
location.href="index.html";
</script>
<% } else {
%>
<script type="text/javascript">
alert("다시 작성해주세요.");
location.href="logincontroller.jsp?command=registerform";
</script>
<% }
} else if("listone".equals(command)){
//1.
int myno = Integer.parseInt(request.getParameter("myno"));
//2.
myDto dto = dao.selectListOne(myno);
//3.
request.setAttribute("dto", dto);
//4.
pageContext.forward("selectListone.jsp");
} else if("updateform".equals(command)){
//1.
int myno = Integer.parseInt(request.getParameter("myno"));
//2.
myDto dto = dao.selectListOne(myno);
//3.
request.setAttribute("dto", dto);
//4.
pageContext.forward("updateform.jsp");
} else if("updateres".equals(command)){
//1.
String myname = request.getParameter("myname");
String myaddr = request.getParameter("myaddr");
String myphone = request.getParameter("myphone");
String myemail = request.getParameter("myemail");
int myno = Integer.parseInt(request.getParameter("myno"));
//2.
myDto dto = dao.selectListOne(myno);
dto.setMyname(myname);
dto.setMyaddr(myaddr);
dto.setMyphone(myphone);
dto.setMyemail(myemail);
int res = dao.update(dto);
//3. X
//4.
if(res>0){
%>
<script type="text/javascript">
alert("정보 수정 성공");
location.href="logincontroller.jsp?command=listone&myno=<%=myno%>";
</script>
<% } else {
%>
<script type="text/javascript">
alert("정보 수정 실패");
location.href="logincontroller.jsp?command=listone&myno=<%=myno%>";
</script>
<%
}
} else if("delete".equals(command)){
//1.
int myno = Integer.parseInt(request.getParameter("myno"));
//2.
int res = dao.delete(myno);
//3. X
//4.
if(res>0){
%>
<script type="text/javascript">
alert("회원 탈퇴 완료");
location.href="index.html";
</script>
<% } else {
%>
<script type="text/javascript">
alert("회원 탈퇴 실패");
location.href="logincontroller.jsp?command=login";
</script>
<% }
} else if("idchk".equals(command)){
//1.
String myid = request.getParameter("myid");
//2.
myDto dto = dao.idCheck(myid);
boolean idnotused = true;
if(dto.getMyid() != null){
idnotused = false;
}
//3. X
//4.
response.sendRedirect("idchk.jsp?idnotused="+idnotused);
} else if("updaterole".equals(command)){
//1.
int myno = Integer.parseInt(request.getParameter("myno"));
//2.
myDto dto = dao.selectListOne(myno);
//3.
request.setAttribute("dto", dto);
//4.
pageContext.forward("adminupdaterole.jsp");
} else if("updateroleres".equals(command)){
//1.
String myrole = request.getParameter("myrole");
int myno = Integer.parseInt(request.getParameter("myno"));
//2.
int res = dao.updateRole(myno, myrole);
//3. X
//4.
if(res>0){
%>
<script type="text/javascript">
alert("회원 등급 변경 성공");
location.href="logincontroller.jsp?command=listenabled";
</script>
<% } else {
%>
<script type="text/javascript">
alert("회원 등급 변경 실패");
location.href="logincontroller.jsp?command=updaterole&myno=<%=myno%>";
</script>
<% }
}
%>
</body>
</html>
3. Model
▶REGISTER(회원가입)
1) register.jsp(회원가입 양식 출력)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% request.setCharacterEncoding("UTF-8"); %>
<% response.setContentType("text/html; charset=UTF-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
//아이디 중복체크
function idChk(){
var doc = document.getElementsByName("myid")[0];
if(doc.value == null || doc.value.trim() == ""){
alert("아이디를 먼저 입력해주세요");
} else {
open("logincontroller.jsp?command=idchk&myid="+doc.value, "", "width=200, height=200");
}
}
//아이디 중복체크 완료 했는지 여부 확인
function idChkConfirm(){
var chk = document.getElementsByName("myid")[0].title;
if(chk == "n"){
alert("아이디 중복체크를 먼저 해주세요.");
document.getElementsByName("myid")[0].focus();
}
}
function mypwChk(){
var pw = document.getElementsByName("mypw")[0].value;
var pwchk = document.getElementsByName("mypwchk")[0].value;
if(pw == pwchk){
document.getElementsByTagName("span")[0].innerHTML = "비밀번호가 일치합니다."
document.getElementsByName("myname")[0].focus();
} else {
document.getElementsByTagName("span")[0].innerHTML = "비밀번호가 일치하지 않습니다.";
document.getElementsByName("mypwchk")[0].focus();
}
}
</script>
</head>
<body>
<h1>REGISETER FORM</h1>
<form action="logincontroller.jsp?command=register" method="post">
<table border="1">
<col width="100">
<col width="800">
<tr>
<th>ID</th>
<td>
<input type="text" name="myid" required="required" title="n"/>
<!-- title은 해당 태그 위에 마우스 포인터가 올라갔을 때 나타나는 말풍선 -->
<input type="button" value="중복체크" onclick="idChk();"/>
</td>
</tr>
<tr>
<th>PW</th>
<td><input type="password" name="mypw" onclick="idChkConfirm();" required="required"/></td>
</tr>
<tr>
<th>PW재확인</th>
<td>
<input type="password" name="mypwchk" onclick="idChkConfirm();" required="required"/>
<input type="button" value = "비밀번호확인" name="mypwconfirm" onclick="mypwChk();"/><span></span>
</td>
</tr>
<tr>
<th>NAME</th>
<td><input type="text" name="myname" onclick="idChkConfirm();" required="required"/></td>
</tr>
<tr>
<th>ADDRES</th>
<td><input type="text" name="myaddr" onclick="idChkConfirm();" required="required"/></td>
</tr>
<tr>
<th>PHONE</th>
<td><input type="text" name="myphone" onclick="idChkConfirm();" required="required"/></td>
</tr>
<tr>
<th>EMAIL</th>
<td><input type="text" name="myemail" onclick="idChkConfirm();" required="required"/></td>
</tr>
<tr>
<td colspan="2" align="right">
<input type="submit" value="가입"/>
</td>
</tr>
</table>
</form>
</body>
</html>
2) idchk.jsp(id중복확인)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% request.setCharacterEncoding("UTF-8"); %>
<% response.setContentType("text/html; charset=UTF-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
onload=function(){
var id = opener.document.getElementsByName("myid")[0].value;
document.getElementsByName("id")[0].value=id;
}
function idConfirm(bool){
if(bool == "true"){
opener.document.getElementsByName("myid")[0].title="y";
opener.document.getElementsByName("mypw")[0].focus();
} else {
opener.document.getElementsByName("myid")[0].focus();
}
self.close();
}
</script>
</head>
<body>
<%
//boolean 타입이었지만 String 타입으로 변환
String idnotused = request.getParameter("idnotused");
%>
<table border="1">
<tr>
<td><input type="text" name="id" readonly="readonly"/></td>
</tr>
<tr>
<td><%=idnotused.equals("true")? "아이디 생성가능":"중복된 아이디 존재" %></td>
</tr>
<tr>
<td> <!-- ''이거 없으면 변수취급이라 undefined됨 -->
<input type="button" value="확인" onclick="idConfirm('<%=idnotused %>');"/>
</td>
</tr>
</table>
</body>
</html>
▶ ADMIN(관리자)
1) adminmain.jsp(관리자 메인화면)
<%@page import="com.login.dto.myDto"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% request.setCharacterEncoding("UTF-8"); %>
<% response.setContentType("text/html; charset=UTF-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
myDto dto = (myDto)session.getAttribute("login");
%>
<h1>ADMIN MAIN PAGE</h1>
<div>
<span><%=dto.getMyid() %>님 환영합니다.</span>
<a href="logincontroller.jsp?command=logout">로그아웃</a>
</div>
<div>
<div>
<a href="logincontroller.jsp?command=listall">회원 정보 전체 조회</a>
</div>
<div>
<a href="logincontroller.jsp?command=listenabled">가입된 회원 조회</a>
</div>
</div>
</body>
</html>
2) adminlistall.jsp(관리자 - 전체회원정보조회/탈퇴회원 포함)
<%@page import="java.util.List"%>
<%@page import="com.login.dto.myDto"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% request.setCharacterEncoding("UTF-8"); %>
<% response.setContentType("text/html; charset=UTF-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
#detail > td{text-align: center;}
</style>
</head>
<body>
<%
List<myDto> list = (List<myDto>)request.getAttribute("list");
%>
<h1>LIST ALL</h1>
<table border="1">
<col width="50">
<col width="200">
<col width="200">
<col width="500">
<col width="200">
<col width="300">
<col width="100">
<col width="50">
<tr>
<th>번호</th>
<th>아이디</th>
<th>이름</th>
<th>주소</th>
<th>전화번호</th>
<th>이메일</th>
<th>가입여부</th>
<th>등급</th>
</tr>
<%
for(myDto dto : list){
%>
<tr id="detail">
<td><%=dto.getMyno() %></td>
<td><%=dto.getMyid() %></td>
<td><%=dto.getMyname() %></td>
<td><%=dto.getMyaddr() %></td>
<td><%=dto.getMyphone() %></td>
<td><%=dto.getMyemail() %></td>
<td><%=dto.getMyenabled().equals("Y")?"가입":"탈퇴" %></td>
<td><%=dto.getMyrole() %></td>
</tr>
<%
}
%>
<tr>
<td colspan="8" align="right">
<input type="button" value="메인" onclick="location.href='adminmain.jsp'"/>
</td>
</tr>
</table>
</body>
</html>
3) adminlistenabled.jsp(관리자 - 가입한 회원 조회)
<%@page import="com.login.dto.myDto"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% request.setCharacterEncoding("UTF-8"); %>
<% response.setContentType("text/html; charset=UTF-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
#detail > td{text-align: center;}
</style>
<script type="text/javascript" src="resources/js/jquery-3.5.1.min.js"></script>
<script type="text/javascript">
function updateRole(myno){
location.href="logincontroller.jsp?command=updaterole&myno="+myno;
}
</script>
</head>
<body>
<%
List<myDto> list = (List<myDto>)request.getAttribute("listenabled");
%>
<h1>LIST ALL</h1>
<table border="1">
<col width="50">
<col width="200">
<col width="200">
<col width="500">
<col width="200">
<col width="300">
<col width="100">
<col width="50">
<tr>
<th>번호</th>
<th>아이디</th>
<th>이름</th>
<th>주소</th>
<th>전화번호</th>
<th>이메일</th>
<th>등급</th>
<th>등급변경</th>
</tr>
<%
for(myDto dto : list){
%>
<tr id="detail">
<td><%=dto.getMyno() %></td>
<td><%=dto.getMyid() %></td>
<td><%=dto.getMyname() %></td>
<td><%=dto.getMyaddr() %></td>
<td><%=dto.getMyphone() %></td>
<td><%=dto.getMyemail() %></td>
<td><%=dto.getMyrole() %></td>
<td><input type="button" value="변경" onclick="updateRole(<%=dto.getMyno()%>)"/></td>
</tr>
<%
}
%>
<tr>
<td colspan="8" align="right">
<input type="button" value="메인" onclick="location.href='adminmain.jsp'"/>
</td>
</tr>
</table>
</body>
</html>
4) adminupdaterole.jsp(관리자 - 가입한 회원 조회 - 등급변경)
<%@page import="com.login.dto.myDto"%>
<%@ 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>
<%
myDto dto = (myDto)request.getAttribute("dto");
%>
<h1>UPDATE ROLE</h1>
<form action="logincontroller.jsp?command=updateroleres" method="post">
<input type="hidden" name="command" value="updateroleres"/>
<input type="hidden" name="myno" value="<%=dto.getMyno() %>"/>
<table border="1">
<col width="100">
<col width="200">
<tr>
<th>NO</th>
<td><%=dto.getMyno() %></td>
</tr>
<tr>
<th>ID</th>
<td><%=dto.getMyid() %></td>
</tr>
<tr>
<th>ROLE</th>
<td>
<select name="myrole">
<option value="USER" <%=dto.getMyrole().equals("USER")? "selected": "" %>>일반회원</option>
<option value="ADMIN" <%=dto.getMyrole().equals("ADMIN")? "selected": "" %>>관리자</option>
</select>
</td>
</tr>
<tr>
<td colspan="2" align="right">
<input type="button" value="취소" onclick="location.href='logincontroller.jsp?command=listenabled'"/>
<input type="submit" value="변경"/>
</td>
</tr>
</table>
</form>
</body>
</html>
▶ USER(일반회원)
1) usermain.jsp(일반회원 메일화면)
<%@page import="com.login.dto.myDto"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% request.setCharacterEncoding("UTF-8"); %>
<% response.setContentType("text/html; charset=UTF-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
myDto dto = (myDto)session.getAttribute("login");
%>
<h1>ADMIN MAIN PAGE</h1>
<div>
<span><%=dto.getMyid() %>님 환영합니다. (등급: <%=dto.getMyrole() %>)</span>
<a href="logincontroller.jsp?command=logout">로그아웃</a>
</div>
<div>
<div>
<a href="logincontroller.jsp?command=listone&myno=<%=dto.getMyno()%>">내 정보 조회</a>
</div>
<div>
<a href="logincontroller.jsp?command=delete&myno=<%=dto.getMyno()%>">회원 탈퇴</a>
</div>
</div>
</body>
</html>
2) selectListone.jsp(일반회원 - 내 정보 조회)
<%@page import="com.login.dto.myDto"%>
<%@ 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>
<%
myDto dto = (myDto)request.getAttribute("dto");
%>
<h1>MY INFORMATION</h1>
<form action="logincontroller.jsp?command=updateform" method="post">
<input type="hidden" name="myno" value="<%=dto.getMyno() %>"/>
<table border="1">
<col width="100">
<col width="800">
<tr>
<th>ID</th>
<td><%=dto.getMyid() %></td>
</tr>
<tr>
<th>PW</th>
<td><input type="button" name="" value="비밀번호 변경"/></td>
</tr>
<tr>
<th>NAME</th>
<td><%=dto.getMyname() %></td>
</tr>
<tr>
<th>ADDRES</th>
<td><%=dto.getMyaddr() %></td>
</tr>
<tr>
<th>PHONE</th>
<td><%=dto.getMyphone() %></td>
</tr>
<tr>
<th>EMAIL</th>
<td><%=dto.getMyemail() %></td>
</tr>
<tr>
<th>회원등급</th>
<td><%=dto.getMyrole() %></td>
</tr>
<tr>
<td colspan="2" align="right">
<input type="button" value="취소" onclick="location.href='usermain.jsp'"/>
<input type="submit" value="수정"/>
</td>
</tr>
</table>
</form>
</body>
</html>
3) updateform.jsp(일반회원 - 내 정보 조회 - 정보 수정)
<%@page import="com.login.dto.myDto"%>
<%@ 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>
<%
myDto dto = (myDto)request.getAttribute("dto");
%>
<h1>UPDATE INFORMATION</h1>
<form action="logincontroller.jsp?command=updateres" method="post">
<input type="hidden" name="myno" value="<%=dto.getMyno() %>"/>
<table border="1">
<col width="100">
<col width="800">
<tr>
<th>ID</th>
<td><%=dto.getMyid() %></td>
</tr>
<tr>
<th>PW</th>
<td><input type="password" value="<%=dto.getMypw() %>" readonly="readonly"><input type="button" name="" value="비밀번호 변경"/></td>
</tr>
<tr>
<th>NAME</th>
<td><input type="text" name="myname" value="<%=dto.getMyname() %>"/></td>
</tr>
<tr>
<th>ADDRES</th>
<td><input type="text" name="myaddr" value="<%=dto.getMyaddr() %>"/></td>
</tr>
<tr>
<th>PHONE</th>
<td><input type="text" name="myphone" value="<%=dto.getMyphone() %>"/></td>
</tr>
<tr>
<th>EMAIL</th>
<td><input type="text" name="myemail" value="<%=dto.getMyemail() %>"/></td>
</tr>
<tr>
<th>회원등급</th>
<td><%=dto.getMyrole() %></td>
</tr>
<tr>
<td colspan="2" align="right">
<input type="button" value="취소" onclick="location.href='logincontroller.jsp?command=listone&myno=<%=dto.getMyno()%>'"/>
<input type="submit" value="수정"/>
</td>
</tr>
</table>
</form>
</body>
</html>
4) 회원탈퇴(logincontroller.jsp 내 command=delete 참고)
※ trim() : 문장(문자열, string)에서, 양끝에 있는 공백문자를 모두 삭제, 익스플로러(ie) 9 이상 & 그외 브라우저에서는 기본적으로 제공되는 함수이다
728x90
반응형
'Web > Jsp_servlet' 카테고리의 다른 글
[Servlet]xml (0) | 2020.08.25 |
---|---|
[Servlet]SCOPE_page_request (0) | 2020.08.14 |
[JSP]로그인만들기_01 (0) | 2020.08.13 |
[JSP]forward와 redirect (0) | 2020.08.12 |
[JSP]MVC2게시판만들기_02 (0) | 2020.08.12 |