잡동사니에도 사랑을

[21.10.25] (MQBProject) 본문

JAVA_EE/MVC+JQuery+Mybatis

[21.10.25] (MQBProject)

luvforjunk 2021. 10. 25. 23:13
728x90
반응형

지난 시간, navigator 위치에 로그인창을 만들고
유효성검사까지 구현해주는 작업을 진행하였다.
먼저 결과를 보면서 이번 시간엔 어떠한 작업을 구현하고자 하는지 파악해보도록 하겠다.

아이디 혹은 비밀번호 잘못 기입 시 뜨는 알림창

 

 

 

 

흐름을 먼저 살펴보자



////////command.properties

/member/login.do=member.service.LoginService /member/logout.do=member.service.LogoutService




////////memberMapper.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="memberSQL"> <!-- 로그인 --> <select id="login" parameterType="java.util.Map" resultType="member.bean.MemberDTO"> select * from member where id=#{id} and pwd=#{pwd} </select> </mapper>




////////MemberDAO

package member.dao; import java.io.IOException; import java.io.Reader; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.HashMap; import java.util.Map; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import member.bean.MemberDTO; public class MemberDAO { private Connection conn = null; private PreparedStatement pstmt; private ResultSet rs; private static MemberDAO instance = null; private SqlSessionFactory sqlSessionFactory; public static MemberDAO getInstance() { if (instance == null) { synchronized (MemberDAO.class) { instance = new MemberDAO();// 생성 } } return instance; } public MemberDAO() { try { Reader reader = Resources.getResourceAsReader("mybatis-config.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); }catch (IOException e) { e.printStackTrace(); } } public MemberDTO login(String id, String pwd) { Map<String, String> map = new HashMap<String, String>(); map.put("id", id); map.put("pwd", pwd); SqlSession sqlSession = sqlSessionFactory.openSession(); // 생성 MemberDTO memberDTO = sqlSession.selectOne("memberSQL.login", map); sqlSession.close(); return memberDTO; } }





////////LoginService.java

package member.service; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.control.CommandProcess; import member.bean.MemberDTO; import member.dao.MemberDAO; public class LoginService implements CommandProcess { @Override public String requestPro(HttpServletRequest request, HttpServletResponse response) throws Throwable { //데이터 String id = request.getParameter("id"); String pwd = request.getParameter("pwd"); //DB MemberDAO memberDAO = MemberDAO.getInstance(); MemberDTO memberDTO = memberDAO.login(id, pwd); //응답 if(memberDTO != null) { //세션 - Tomcat에다 저장. 경로 지정 필요없음 HttpSession session = request.getSession(); // 세션 생성 session.setAttribute("memName", memberDTO.getName()); session.setAttribute("memId", id); session.setAttribute("memEmail", memberDTO.getEmail1() +"@"+memberDTO.getEmail2()); session.setAttribute("memDTO", memberDTO); } return "/member/login.jsp"; // 여기서 ok를 return시켜 줘서는 안된다 } }





////////login.jsp

<%@ 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.memId != null }">ok</c:if> <c:if test="${sessionScope.memId == null }">fail</c:if>





////////loginOk.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <style type="text/css"> img { width: 70px; height: 50px; cursor: pointer; /* 화살표가 나오는 걸 손가락으로 나오게 하기 위해 */ } </style> ${sessionScope.memName}님 로그인 <br><br> <input type="button" value="로그아웃" onclick="location.href='/MQBProject/member/logout.do'"> <input type="button" value="회원정보수정" onclick="location.href='/MQBProject/member/modifyForm.do'">




////////loginForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>로그인</title> <style type="text/css"> div#idDiv, div#pwdDiv { color: red; font-size: 8pt; font-weight: bold; } </style> </head> <body> <form name="loginForm"> <table border="1" cellspacing="0" cellpadding="5"> <tr> <td width="100" align="center">아이디</td> <td> <input type="text" name="id" id="id" placeholder="아이디 입력"> <div id= "idDiv"></div> </td> </tr> <tr> <td width="100" align="center">비밀번호</td> <td> <input type="password" name="pwd" id="pwd" size="30" placeholder="비밀번호 입력"> <div id= "pwdDiv"></div> </td> </tr> <tr> <td colspan="2" align="center"> <input type="button" id="loginBtn" value="로그인"> <input type="button" value="회원가입" onclick="location.href='writeForm.html'"> </td> </tr> </table> <br><br> <div id="loginResult"></div> </form> <script type="text/javascript" src="http://code.jquery.com/jquery-3.6.0.min.js"></script> <script type="text/javascript" src="js/member.js"></script> </body> </html>
728x90
반응형

'JAVA_EE > MVC+JQuery+Mybatis' 카테고리의 다른 글

[21.10.27] 우편번호 검색 및 글쓰기(JSTL 정의)  (0) 2021.10.27
[21.10.26] MQBProject  (0) 2021.10.26
[21.10.26]  (0) 2021.10.26
[21.10.20]  (0) 2021.10.20
[21.10.19] (MQBProject)  (0) 2021.10.19