๊ด€๋ฆฌ ๋ฉ”๋‰ด

gi_dor

์„ธ๋ฏธ ํ”„๋กœ์ ํŠธ - ๋กœ๊ทธ์ธ , ์„ธ์…˜ ๋‹ค๋ฃจ๊ธฐ ๋ณธ๋ฌธ

Back_End/JSP_Servlet

์„ธ๋ฏธ ํ”„๋กœ์ ํŠธ - ๋กœ๊ทธ์ธ , ์„ธ์…˜ ๋‹ค๋ฃจ๊ธฐ

๊ธฐ๋Œ 2024. 2. 14. 14:56
728x90

๐Ÿ”ธ ํ”„๋กœ์ ํŠธ ๋กœ๊ทธ์ธ ๊ธฐ๋Šฅ 

 User ์™€ Seller 2๊ฐœ์˜ ๋กœ๊ทธ์ธ๋ฐฉ์‹

 

 

// vo.Users
public class Users {
	private int no;
	private String id;
	private String pwd;
	private String name;
	private String email;
	private String tel;
	private String delYn;
	private Date updateDate;
	private Date regDate;
	private String zipcode;
	private String address;
	private String bname;
	private String detailed;
	private String userImgPath;
    
   // ์ƒ์„ฑ์ž
   // getter
   // setter
   
   }
public class Seller {
	Integer no;
	String id;
	String password;
	String name;
	String tel;
	String email;
	Date regDate;
	Date updateDate;
	String delYn;
	Integer rsnNo;
    // ์ƒ์„ฑ์ž
    // getter
    // setter
    }

LoginUserDTO.java

public class LoginUser {

        private int no;
        private String id;
        private String name;
        private String type;
        // ์ƒ์„ฑ์ž
        // getter
        // setter
	}

-  String  type์„ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค
-  ๋กœ๊ทธ์ธ ์‹œ์— "seller" , "user" ๋กœ ์‚ฌ์šฉ์ž์™€ ํŒ๋งค์ž ๊ตฌ๋ถ„ํ•  ์šฉ๋„ ์ž…๋‹ˆ๋‹ค

 

SellerLoginForm.jsp

<form class="border bg-light p-3 mx-auto" method="post" action="SellerLogin.jsp" onsubmit="checkForm(event)">
     <div id="welcomeBox">
        <h2 id="welcomeTitle">would YOU</h2> 
        <h3 id="welcomeContent"> ํŒ๋งค์ž ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€์— ์˜ค์‹ ๊ฑธ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค </h3>
     </div>

     <div class="form-group mb-3">
         <label class="form-label">ID</label>
         <input type="text" class="form-control" name="id" id="inputId">
     </div>

     <div class="form-group mb-3">
         <label class="form-label">PassWord</label>
         <input type="password" class="form-control" name="password" id="inputPassword">
     </div>

     <div class="text-end d-grid gap-2 col-6 mx-auto">
         <button type="submit" class="btn btn-dark">Login</button>
     </div>
    <hr>
</form>

 

SellerLoginForm.jsp๋‚ด์— form  ํƒœ๊ทธ action ์œผ๋กœ SellerLogin.jsp๋กœ ๋ณด๋‚ธ๋‹ค.
๋„˜๊ฒจ ์ง€๋Š” ๊ฐ’์€  name=" " ์— ์†ํ•œ๋‹ค

<%

 /*
    ๋กœ๊ทธ์ธ ์ฒ˜๋ฆฌ
       1. ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌ๋ฐ›์€ id์™€ password ์กฐํšŒ
       2. id๋กœ ์‚ฌ์šฉ์ž ์ •๋ณด ์กฐํšŒ
           2-1. ์‚ฌ์šฉ์ž ์ •๋ณด๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค - > ํšŒ์›๊ฐ€์ž… ํ•˜์ง€ ์•Š์Œ loginform.jsp?error=fail
           2-2. ์‚ฌ์šฉ์ž ์ •๋ณด๊ฐ€ ์กด์žฌํ•˜์ง€๋งŒ ํƒˆํ‡ดํ•œ ์‚ฌ์›์ด๋‹ค  - loginform.jsp?error=deleted
           2-3. ์‚ฌ์šฉ์ž ์ •๋ณด๋Š” ์กด์žฌํ•˜์ง€๋งŒ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š๋Š”๋‹ค - loginform.jsp?error=fail
       3. ์‚ฌ์šฉ์ž ์ •๋ณด๊ฐ€ ์กด์žฌํ•˜๊ณ  ๋น„๋ฐ€๋ฒˆํ˜ธ๋„ ์ผ์น˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋กœ๊ทธ์ธ ์ฒ˜๋ฆฌํ•œ๋‹ค
*/
   
        // ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ ์ „๋‹ฌ๋ฐ›์€ ๊ฐ’ 
        String id = request.getParameter("id");
        String password = request.getParameter("password");
	   
	   // CRUD๊ธฐ๋Šฅ ์žˆ๋Š” DAO ์ƒ์„ฑ
	   SellerDao sellerDao = new SellerDao();
	   
	   //  DAO์˜ getSellerById๋ฅผ ์‹คํ–‰ํ•ด์„œ ํ•ด๋‹นํ•˜๋Š” ์•„์ด๋””์— ์ •๋ณด ์กฐํšŒ
	   Seller savedSeller = sellerDao.getSellerById(id);
       
	   
	   // Session ๊ฐ์ฒด์— ๋‹ด๊ธฐ
	   // ๊ธฐ์กด์—๋Š” ์„ธ์…˜์„ 2๊ฐœ๋กœ ๋‚˜๋ˆ ์„œ ํŒ๋งค์ž , ๊ตฌ๋งค์ž 2๊ฐœ์˜ ์„ธ์…˜์œผ๋กœ ๋‹ค๋ค˜๋Š”๋ฐ ๊ฐ๋„ ์•ˆ์žกํžˆ๊ณ  ํž˜๋“ค์–ด์„œ
	   // ๋กœ๊ทธ์ธ ์„ธ์…˜์„ 1๊ฐœ๋กœ ํ†ตํ•ฉํ•ด์„œ ๊ด€๋ฆฌํ•˜๋ ค๊ณ  ํ•œ๋‹ค.
	   LoginUser loginUser = new LoginUser();
	   
	   loginUser.setNo(savedSeller.getNo());
	   loginUser.setName(savedSeller.getName());
	   loginUser.setId(savedSeller.getId());
	   loginUser.setType("seller");
	   
	   session.setAttribute("LOGIN_USER", loginUser);
	   
	   response.sendRedirect("/index.jsp");
       
       %>

 

1. action์„ํ†ตํ•ด SellerLogin.jsp ๋กœ ๋„˜์–ด์˜จ ์š”์ฒญ ๊ฐ’์„ ์กฐํšŒํ•ด๋ณด๋‹ค

// ๋กœ๊ทธ์ธํผ ํ™”๋ฉด์—์„œ ์ž…๋ ฅํ•œ id ๊ฐ’๊ณผ , ๋น„๋ฐ€๋ฒˆํ˜ธ ๊ฐ’์„  ๋‹ด์ž
String id = request.getParameter("id");
String password = request.getParameter("password");

2.  SellerDao ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค

SellerDao sellerDao = new SellerDao();

3. XML , DAO

XML

<sqlMap namespace="sellers">
    <select id ="getSellerById"  parameterClass="string" resultClass="vo.Seller">
        select 
            seller_no			as no,
            seller_id			as id,
            seller_pw			as password,
            seller_name 		as name,
            seller_tel   		as tel,
            seller_email		as email,
            seller_reg_date		as regDate,
            seller_update_date	as updateDate,
            seller_del_yn		as delYn,
            del_rsn_no		as rsnNo
        from seller
        where 
            seller_id = #value#
    </select>
</sqlMap>

 

DAO

public class SellerDaopublic class SellerDao {
	// iBatis ์‚ฌ์šฉ
	private SqlMapClient ibatis = IbatisUtil.getSqlMapClient();
    
    /**
    * ์ฃผ์–ด์ง„ id๋กœ ํ•ด๋‹น id์— ์ •๋ณด๋ฅผ ์กฐํšŒํ•œ๋‹ค
    * @methodNamd	:   getAllServices
    * @param id   	: 	id
    * @author 		:	Han Gi Seon
    * @date		:	2024.02.04  
    * @return
    * @throws SQLException
    */
	public Seller getSellerById(String id) throws SQLException {
		
		Seller seller = (Seller) ibatis.queryForObject("sellers.getSellerById",id);
		return seller;
	}
}

 

4. DAO ๋ฅผ ์‚ฌ์šฉํ•ด ์•„์ด๋”” ์ •๋ณด๋ฅผ ์กฐํšŒ

//  DAO์˜ getSellerById๋ฅผ ์‹คํ–‰ํ•ด์„œ ํ•ด๋‹นํ•˜๋Š” ์•„์ด๋””์— ์ •๋ณด ์กฐํšŒ
Seller savedSeller = sellerDao.getSellerById(id);

 

5. Session ๊ฐ์ฒด์— ๋‹ด๊ธฐ

// setType ์„ "seller"๋กœ ์ง์ ‘ ์„ค์ •

LoginUser loginUser = new LoginUser();

loginUser.setNo(savedSeller.getNo());
loginUser.setName(savedSeller.getName());
loginUser.setId(savedSeller.getId());
loginUser.setType("seller");

session.setAttribute("LOGIN_USER", loginUser);

response.sendRedirect("/index.jsp");

setType์— "seller" ๋กœ ์„ค์ •ํ•ด์„œ ํ•ด๋‹น ๋กœ๊ทธ์ธ์ด Seller ์ธ๊ฒƒ์„ ๋ช…์‹œํ•œ๋‹ค
"user" ๋กœ ํ•œ๋‹ค๋ฉด User ์‚ฌ์šฉ์ž ์ธ๊ฒƒ์„ ๋ช…์‹œ

 

6. ๋กœ๊ทธ์ธ ํ•„ํ„ฐ๋ง

//  ์•„์ด๋””์— ํ•ด๋‹นํ•˜๋Š” Seller ์ •๋ณด๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ์ฒดํฌ
	   if(savedSeller == null ) {
	      response.sendRedirect("SellerLoginForm.jsp?error=fail");
	      return;	
	   }
	   
// ์‚ฌ์šฉ์ž๊ฐ€ ํƒˆํ‡ดํ–ˆ๋Š”์ง€ ์ฒดํฌ
        if("Y".equals(savedSeller.getDelYn())) {
           response.sendRedirect("SellerLoginForm.jsp?error=deleted");
           return;
        }

// ์‚ฌ์šฉ์ž ์ •๋ณด๋Š” ์กด์žฌ ํ•˜๊ณ  ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜๋Š”์ง€ ์ฒดํฌ
        String shaPassword = DigestUtils.sha256Hex(password);

        if(!savedSeller.getPassword().equals(shaPassword)) {
           response.sendRedirect("SellerLoginForm.jsp?error=fail");
           return;
        }
728x90