
1. Template
1) WebContent - view - template - sidebar.jsp 수정
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> < c:choose > < c:when test = "${sessionScope.clientDTO==null}" > <!-- 로그인 부분 --> < div class = "card" > < h3 >로그인</ h3 > < form method = "post" action = "${pageContext.request.contextPath}/DispatcherServlet" > < input type = "hidden" name = "command" value = "login" > < p >아이디 < input type = "text" name = "clientId" placeholder = "아이디" size = "12" ></ p > < p >패스워드 < input type = "password" name = "clientPassword" placeholder = "비밀번호" size = "12" ></ p > < input type = "submit" value = "로그인" > < a href = "${pageContext.request.contextPath}/DispatcherServlet?command=registerPage" >회원가입</ a ></ p > </ form > </ div > </ c:when > < c:otherwise > < div class = "card" > < p >${sessionScope.clientDTO.clientName}님 반갑습니다.</ p > < p >< a href = "${pageContext.request.contextPath}/DispatcherServlet?command=logout" >로그아웃</ a ></ p > < p >< a href = "#" >회원수정</ a ></ p > < p >< a href = "#" >회원탈퇴</ a ></ p > < p >< a href = "${pageContext.request.contextPath}/index.jsp" >홈으로</ a > </ p > </ div > <!-- 메뉴부분 --> < div class = "card" > < h3 >게시판</ h3 > < a href = "#" >게시판 보기</ a > </ div > </ c:otherwise > </ c:choose > |
2. View
1) WebContent - view - member - login_result.jsp 추가
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!-- 로그인 실패 : 세션 없음 --> < c:if test = "${sessionScope.clientDTO==null}" > < script lang = "javascript" > alert('아이디가 없거나 패스워드가 맞지 않아 로그인에 실패하셨습니다.'); location.href='http://localhost:8080/webstudy-board/'; </ script > </ c:if > <!-- 로그인 성공 : 세션 있음 --> < c:if test = "${sessionScope.clientDTO!=null}" > < script lang = "javascript" > var name='${sessionScope.clientDTO.clientName}'; alert(name+'님 안녕하세요'); location.href='http://localhost:8080/webstudy-board/'; </ script > </ c:if > |
3. Model
1) src - model.ClientDAO.java에 login 로그인 메소드 추가
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | public ClientDTO login(ClientDTO clientDTO) throws SQLException { ClientDTO resultDTO= null ; Connection con= null ; PreparedStatement pstmt= null ; ResultSet rs= null ; try { con=getConnection(); String sql= "select clientId,clientName from client where clientId=? and clientPassword=?" ; pstmt=con.prepareStatement(sql); pstmt.setString( 1 , clientDTO.getClientId()); pstmt.setString( 2 , clientDTO.getClientPassword()); rs=pstmt.executeQuery(); if (rs.next()) resultDTO= new ClientDTO(rs.getString( 1 ), null , rs.getString( 2 ), null , null , null ); } finally { closeAll(rs, pstmt, con); } return resultDTO; } |
2) src - test.TestLogin.java 추가(login 메소드 테스트)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package test; import model.ClientDAO; import model.ClientDTO; public class TestLogin { public static void main(String[] args) { String clientId= "xiumin" ; String clientPassword= "exo1234" ; try { ClientDTO clientDTO=ClientDAO.getInstance().login( new ClientDTO(clientId,clientPassword, null , null , null , null )); System.out.println(clientDTO); } catch (Exception e) { e.printStackTrace(); } } } |
4. Controller
1) src - controller.HandlerMapping.java에 login, logout command 추가;
1 2 3 4 | else if (command.equals( "login" )) //로그인 c= new LoginController(); else if (command.equals( "logout" )) //로그아웃 c= new LogoutController(); |
2) src - controller.LoginController.java 추가
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | package controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import model.ClientDAO; import model.ClientDTO; public class LoginController implements Controller { @Override public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception { String clientId=request.getParameter( "clientId" ); String clientPassword=request.getParameter( "clientPassword" ); ClientDTO clientDTO=ClientDAO.getInstance().login( new ClientDTO(clientId,clientPassword, null , null , null , null )); HttpSession session=request.getSession(); session.setAttribute( "clientDTO" , clientDTO); return "redirect:view/member/login_result.jsp" ; } } |
3) src - controller.LogoutController.java 추가
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class LogoutController implements Controller { @Override public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception { HttpSession session = request.getSession( false ); if (session!= null ) session.invalidate(); return "redirect:index.jsp" ; } } |
'1.JAVA Web Programming > [4주차] 게시판 만들기' 카테고리의 다른 글
게시판 - webstudy-board - 회원탈퇴 (0) | 2019.08.11 |
---|---|
게시판 - webstudy-board - 회원수정 (0) | 2019.08.11 |
게시판 - webstudy-board - 회원가입 (0) | 2019.08.11 |
게시판 - webstudy-board - Index 페이지 (0) | 2019.08.11 |
게시판 - webstudy-board - EL, JSTL (0) | 2019.08.07 |