Day06(์ฟ ํ‚ค ๋กœ๊ทธ์ธ ๊ตฌํ˜„)

2021. 10. 12. 18:27ยท๐Ÿ“† Today I Learned(๊ฐœ๋ฐœ์–ธ์–ดํ•™์Šต)/JSP
๋ชฉ์ฐจ
  1. CookieUtils.java์˜ ์ฝ”๋“œ๋‚ด์šฉ
  2.  
  3. UserVO.java์˜ ์ฝ”๋“œ๋‚ด์šฉ
  4.  
  5. UserList.java์˜ ์ฝ”๋“œ๋‚ด์šฉ
  6.  
  7. login.jsp์˜ ์ฝ”๋“œ๋‚ด์šฉ
  8.  
  9. loginCheck.jsp์˜ ์ฝ”๋“œ๋‚ด์šฉ
  10.  
  11. logout.jsp์˜ ์ฝ”๋“œ๋‚ด์šฉ

CookieUtils.java์˜ ์ฝ”๋“œ๋‚ด์šฉ

package com.study.common.util;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

public class CookieUtils {
//์ƒ์„ฑ์ž, exist, getValue, getCookie, createCookie
	
	private Map<String, Cookie> cookieMap = new HashMap<String, Cookie>();
	
	//request์— ์žˆ๋Š” ๋ชจ๋“  ์ฟ ํ‚ค๋“ค์„ ๋งต์—๋‹ค ์ €์žฅ
	public CookieUtils(HttpServletRequest request) {
		Cookie[] cookies = request.getCookies();
		if(cookies != null) {
			for(Cookie cookie : cookies) {
				cookieMap.put(cookie.getName(), cookie);
			}
		}
	}
	
	//name์— ํ•ด๋‹นํ•˜๋Š” ์ฟ ํ‚ค๊ฐ€ ์žˆ๋ƒ ์—†๋ƒ? ์žˆ์œผ๋ฉด true
	public boolean exist(String name) {
		Cookie cookie = cookieMap.get(name);
		
		return cookie != null;
	}
	
	//name์ด๋ผ๋Š” ์ด๋ฆ„์˜ ์ฟ ํ‚ค์˜ ๊ฐ’
	//์ฟ ํ‚ค์œ ํ‹ธ ์‚ฌ์šฉํ•˜๋Š” ๊ณณ์—์„œ null์ฒดํฌ ์•Œ์•„์„œ ํ•˜๋„๋ก ํ•˜์ž
	public String getValue(String name) {
		return cookieMap.get(name).getValue();
	}
	
	//name์˜ ์ฟ ํ‚ค
	public Cookie getCookie(String name) {
		return cookieMap.get(name);
	}
	
	
	public static Cookie createCookie(String name, String value) {
		return createCookie(name, value, "/", "", -1);
	}
	
	public static Cookie createCookie(String name, String value, String path) {
		return createCookie(name, value, path, "", -1);
	}
	
	public static Cookie createCookie(String name, String value, String path, int maxAge) {
		return createCookie(name, value, path, "", maxAge);
	}
	
	public static Cookie createCookie(String name, String value, int maxAge) {
//		Cookie cookie = new Cookie(name, value);
//		cookie.setMaxAge(maxAge);
//		return cookie;
		
		return createCookie(name, value, "/", "", maxAge);
	}
	
	public static Cookie createCookie(String name, String value, String path, String domain, int maxAge) {
		Cookie cookie = new Cookie(name, value);
		cookie.setDomain(domain);
		cookie.setPath(path);
		cookie.setMaxAge(maxAge);
		return cookie;
	}
	
}

 

UserVO.java์˜ ์ฝ”๋“œ๋‚ด์šฉ

package com.study.login.vo;
//์ด๋ฏธ ์ œ๊ณต๋๋‹ค.

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

public class UserVO {
	private String userId;
	private String userName;
	private String userPass;
	private String userRole;
	
	// toString() ๊ตฌํ˜„
	  @Override 
	  public String toString() {
		  return ToStringBuilder.reflectionToString(this,
				  ToStringStyle.MULTI_LINE_STYLE); 
		}
	// ์ƒ์„ฑ์ž
	public UserVO() {
		
	}
	
	// ์ƒ์„ฑ์ž
	public UserVO(String userId, String userName, String userPass, String userRole) {
	this.userId = userId;
	this.userName = userName;
	this.userPass = userPass;
	this.userRole = userRole;
	}

	
	// ๋งด๋ฒ„ํ•„๋“œ์˜ get/set ๋ฉ”์„œ๋“œ ์ƒ์„ฑ

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserPass() {
		return userPass;
	}

	public void setUserPass(String userPass) {
		this.userPass = userPass;
	}

	public String getUserRole() {
		return userRole;
	}

	public void setUserRole(String userRole) {
		this.userRole = userRole;
	}
	
	
	
}

 

UserList.java์˜ ์ฝ”๋“œ๋‚ด์šฉ

package com.study.common.util;
//์ œ๊ณต

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.study.login.vo.UserVO;

public class UserList {
	private Map<String, UserVO> usersMap = null;

	public UserList() {
		UserVO user = null;
		usersMap = new HashMap<String, UserVO>();
		user = new UserVO("malja", "๋ง์ž", "1004", "ADMIN");
		usersMap.put(user.getUserId(), user);
		user = new UserVO("sunja", "์ˆœ์ž", "1111", "USER");
		usersMap.put(user.getUserId(), user);
		user = new UserVO("nolja", "์•ผ๋†€์ž", "1004", "USER");
		usersMap.put(user.getUserId(), user);
		user = new UserVO("milkis", "๋ฐ€ํ‚ค์Šค", "1004", "MANAGER");
		usersMap.put(user.getUserId(), user);
		user = new UserVO("areum", "์•„๋ฆ„", "0000", "MANAGER");
		usersMap.put(user.getUserId(), user);
	}

	public UserVO getUser(String id) {
		System.out.println("UserList getUser id=" + id);
		if (usersMap.containsKey(id)) {
			System.out.println("[" + id + "] ํšŒ์› ์กด์žฌ");
			return usersMap.get(id);
		} else {
			System.out.println("[" + id + "] ํšŒ์›์ด ์กด์žฌํ•˜์ง€ ์•Š์Œ");
			return null;
		}
	}

	public List<UserVO> getUserList() {
		return new ArrayList<UserVO>(usersMap.values());
	}
	
	public Map<String, UserVO> getUsersMap() {
		return usersMap;
	}
	
} // class

 

login.jsp์˜ ์ฝ”๋“œ๋‚ด์šฉ

<%@page import="com.study.common.util.CookieUtils"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	request.setCharacterEncoding("UTF-8");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<%@include file="/WEB-INF/inc/header.jsp"%>
<title>Insert title here</title>
</head>
<body>
<!-- 
	์กฐ๊ฑด ์ข…๋ฅ˜
	msg๋ผ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ, AUTH์ฟ ํ‚ค ์กด์žฌ์—ฌ๋ถ€
 -->
<%@include file="/WEB-INF/inc/top.jsp"%>

	<%
		String msg = request.getParameter("msg");
		CookieUtils cookieUtils = new CookieUtils(request);
		Cookie cookie = cookieUtils.getCookie("AUTH");
		Cookie saveCookie = cookieUtils.getCookie("SAVE_ID");
		String checked="";
		String id="";
		if(saveCookie != null){
			checked="checked='checked'";
			id=saveCookie.getValue();
		}
		
		
		if(cookie == null){
	%>
	<%=msg %>
	<div class="container">
		<form action="loginCheck.jsp" class="loginForm">
			<h2>๋กœ๊ทธ์ธ</h2>
			<table class="table table-bordered">
				<tbody>
					<tr>
						<th>์•„์ด๋””</th>
						<td><input type="text" name="userId"
							class="form-control input-sm" value="<%=id%>"></td>
					</tr>
					<tr>
						<th>๋น„๋ฐ€๋ฒˆํ˜ธ</th>
						<td><input type="password" name="userPass"
							class="form-control input-sm"></td>
					</tr>
					<tr>
						<td colspan="2"><label><input type="checkbox"
								name="rememberMe" value="Y" <%=checked %> >ID ๊ธฐ์–ตํ•˜๊ธฐ</label></td>
					</tr>
					<tr>
						<td colspan="2">
							<button type="submit" class="btn btn-primary btn-sm pull-right">๋กœ๊ทธ์ธ</button>
						</td>
					</tr>
				</tbody>
			</table>
		</form>
	</div>
	
	<%
	}else{//์ฟ ํ‚ค์žˆ๋Š” ๊ฒฝ์šฐ
	%>
	
	<!-- container -->
		๋กœ๊ทธ์ธ ์ค‘
	<a href="logout.jsp" class="btn btn-success btn-sm">๋กœ๊ทธ์•„์›ƒ</a>
	<%
		}
	%>

</body>
</html>

 

loginCheck.jsp์˜ ์ฝ”๋“œ๋‚ด์šฉ

<%@page import="java.util.Map"%>
<%@page import="com.study.common.util.CookieUtils"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="com.study.login.vo.UserVO"%>
<%@page import="com.study.common.util.UserList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	request.setCharacterEncoding("UTF-8");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<%@include file="/WEB-INF/inc/header.jsp" %>
<title></title>
</head>
<body>

<%
	String id = request.getParameter("userId"); //login์—์„œ ๋ฐ›์•„์˜จ ์ž…๋ ฅ๊ฐ’
	String pw = request.getParameter("userPass"); //login์—์„œ ๋ฐ›์•„์˜จ ์ž…๋ ฅ๊ฐ’
	String rememberMe = request.getParameter("rememberMe");
	String redirectPage = "";
	
	if(id == null || id.isEmpty() || pw == null || pw.isEmpty()){
		//๋ญ”๊ฐ€ ์ž…๋ ฅ ์•ˆํ–ˆ์„ ๋•Œ
		redirectPage = "login.jsp?msg=" + URLEncoder.encode("id ๋˜๋Š” pw๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.", "UTF-8");
	}
	
	//id๋ฅผ ๋ชป ์ฐพ์„ ๋•Œ
	UserList userList = new UserList();
	UserVO user = userList.getUser(id); //์—†์œผ๋ฉด null ์žˆ์œผ๋ฉด ํ•ด๋‹น userVO๋ฅผ ๋ฆฌํ„ด
	
	if(user==null){ //user๊ฐ€ ์—†์„ ๋•Œ, id๊ฐ€ ํ‹€๋ ธ์„ ๋•Œ
		if(redirectPage.length()<2){
			redirectPage = "login.jsp?msg=" + URLEncoder.encode("id๋˜๋Š” pw ํ™•์ธํ•ด์ฃผ์„ธ์š”.", "UTF-8");		
		}
	}else{ //id๋Š” ๋งž์•˜๋‹ค.
		if(pw.equals(user.getUserPass())){
			//pw๊นŒ์ง€ ๋งž์Œ
			Cookie cookie = CookieUtils.createCookie("AUTH", id);
			response.addCookie(cookie);
			redirectPage="login.jsp";
			
			//๋กœ๊ทธ์ธ ์„ฑ๊ณต + id๊ธฐ์–ตํ•˜๊ธฐ ์ฒดํฌ ๋˜์–ด์žˆ๋Š” ๊ฒฝ์šฐ
			if(rememberMe != null){
				if(rememberMe.equals("Y")){
					Cookie cookie2 = CookieUtils.createCookie("SAVE_ID", id, 60*60*24*7);
					response.addCookie(cookie2);
				}else{//์ฒดํฌ์•ˆ๋œ๊ฒฝ์šฐ
					Cookie cookie2 = CookieUtils.createCookie("SAVE_ID", id, 0);
					response.addCookie(cookie2);
				}
			}
			
		}else{//id๋Š” ๋งž์•˜์ง€๋งŒ pw๋Š” ํ‹€๋ฆผ
			if(redirectPage.length()<2){
				redirectPage = "login.jsp?msg=" + URLEncoder.encode("id๋˜๋Š” pw ํ™•์ธํ•ด์ฃผ์„ธ์š”.", "UTF-8");		
			}
		}
	}
	response.sendRedirect(redirectPage);
%>



	
	
</body>
</html>

 

logout.jsp์˜ ์ฝ”๋“œ๋‚ด์šฉ

<%@page import="com.study.common.util.CookieUtils"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	request.setCharacterEncoding("UTF-8");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<%@include file="/WEB-INF/inc/header.jsp" %>
<title>Insert title here</title>
</head>
<body>
<%@include file="/WEB-INF/inc/top.jsp" %> 
<%
	Cookie cookie = CookieUtils.createCookie("AUTH", "", 0);
	response.addCookie(cookie);
	response.sendRedirect("login.jsp");
	
%>

</body>
</html>
์ €์ž‘์žํ‘œ์‹œ (์ƒˆ์ฐฝ์—ด๋ฆผ)

'๐Ÿ“† Today I Learned(๊ฐœ๋ฐœ์–ธ์–ดํ•™์Šต) > JSP' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Day06(session = get,set,remove)  (0) 2021.10.12
Day06(session)  (0) 2021.10.12
Day05(cookie์ •๋ฆฌ)  (0) 2021.10.08
Day05(cookie04)  (0) 2021.10.08
Day05(cookie03)  (0) 2021.10.08
  1. CookieUtils.java์˜ ์ฝ”๋“œ๋‚ด์šฉ
  2.  
  3. UserVO.java์˜ ์ฝ”๋“œ๋‚ด์šฉ
  4.  
  5. UserList.java์˜ ์ฝ”๋“œ๋‚ด์šฉ
  6.  
  7. login.jsp์˜ ์ฝ”๋“œ๋‚ด์šฉ
  8.  
  9. loginCheck.jsp์˜ ์ฝ”๋“œ๋‚ด์šฉ
  10.  
  11. logout.jsp์˜ ์ฝ”๋“œ๋‚ด์šฉ
'๐Ÿ“† Today I Learned(๊ฐœ๋ฐœ์–ธ์–ดํ•™์Šต)/JSP' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • Day06(session = get,set,remove)
  • Day06(session)
  • Day05(cookie์ •๋ฆฌ)
  • Day05(cookie04)
JinSeong
JinSeong
์žฌ๋ฏธ์—†์œผ๋ฉด ์•ˆํ•œ๋‹ค.
  • JinSeong
    As you think, so shall you become.
    JinSeong
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
    • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (96)
      • ๐Ÿ“† Today I Learned(๊ฐœ๋ฐœ์–ธ์–ดํ•™์Šต) (76)
        • JAVA (20)
        • HTML (1)
        • JSP (46)
        • SPRING (2)
        • ORACLE(DB) (6)
        • CSS (1)
      • ๐Ÿ“† Today I Learned(์ฝ”๋”ฉํ…Œ์ŠคํŠธ) (0)
        • JAVA (0)
      • Computer Science (9)
        • ์ปดํ“จํ„ฐ ๊ตฌ์กฐ (9)
        • ์šด์˜์ฒด์ œ (0)
        • ์ž๋ฃŒ๊ตฌ์กฐ & ์•Œ๊ณ ๋ฆฌ์ฆ˜ (0)
        • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค (0)
      • ์ฐฝ๊ณ  (11)
        • ๊ธฐ์ดˆ์ง€์‹ (7)
        • ํŒŒ์ผ์ฐฝ๊ณ  (0)
        • ๊ธฐ์ˆ ์ง€์‹์ฐฝ๊ณ  (0)
        • ๋ฒ„๊ทธ๋ฒ„๊ทธ ๐Ÿ‘€ (4)
      • Tools๐Ÿ”ง (0)
  • ๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

    • ํ™ˆ
    • ํƒœ๊ทธ
    • ๋ฐฉ๋ช…๋ก
  • ๋งํฌ

  • ๊ณต์ง€์‚ฌํ•ญ

  • ์ธ๊ธฐ ๊ธ€

  • ํƒœ๊ทธ

    spring ์Šค์ผ€์ค„๋Ÿฌ
    ํ™˜๊ฒฝ๋ณ€์ˆ˜ ์ฐจ์ด
    substring()
    java ์Šค์ผ€์ค„๋Ÿฌ
    ์ œ์–ด์žฅ์น˜
    java ๊ฐ์ฒดํƒ€์ž…ํ™•์ธ
    java json๋ฆฌํ„ด ์—๋Ÿฌ
    jdk๊ตฌ๋ถ„
    SQL
    ALU
    Oracle
    no converter found for
    DB
    ๋ ˆ์ง€์Šคํ„ฐ
    java๋ฒ„์ „ ํ‘œํ˜„
    JAVA๋ฌธ์ž์—ด์ž๋ฅด๊ธฐ
    instanceof ์—ฐ์‚ฐ์ž
    ์ดํด๋ฆฝ์Šค ์˜ค๋ฒ„๋ผ์ด๋”ฉ ๋ฉ”์†Œ๋“œ ์ž๋™ ์ƒ์„ฑ ๊ธฐ๋Šฅ
    CPU
    ๋ช…๋ น์–ด
  • ์ตœ๊ทผ ๋Œ“๊ธ€

  • ์ตœ๊ทผ ๊ธ€

  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.3
JinSeong
Day06(์ฟ ํ‚ค ๋กœ๊ทธ์ธ ๊ตฌํ˜„)
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”

๋‹จ์ถ•ํ‚ค

๋‚ด ๋ธ”๋กœ๊ทธ

๋‚ด ๋ธ”๋กœ๊ทธ - ๊ด€๋ฆฌ์ž ํ™ˆ ์ „ํ™˜
Q
Q
์ƒˆ ๊ธ€ ์“ฐ๊ธฐ
W
W

๋ธ”๋กœ๊ทธ ๊ฒŒ์‹œ๊ธ€

๊ธ€ ์ˆ˜์ • (๊ถŒํ•œ ์žˆ๋Š” ๊ฒฝ์šฐ)
E
E
๋Œ“๊ธ€ ์˜์—ญ์œผ๋กœ ์ด๋™
C
C

๋ชจ๋“  ์˜์—ญ

์ด ํŽ˜์ด์ง€์˜ URL ๋ณต์‚ฌ
S
S
๋งจ ์œ„๋กœ ์ด๋™
T
T
ํ‹ฐ์Šคํ† ๋ฆฌ ํ™ˆ ์ด๋™
H
H
๋‹จ์ถ•ํ‚ค ์•ˆ๋‚ด
Shift + /
โ‡ง + /

* ๋‹จ์ถ•ํ‚ค๋Š” ํ•œ๊ธ€/์˜๋ฌธ ๋Œ€์†Œ๋ฌธ์ž๋กœ ์ด์šฉ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ํ‹ฐ์Šคํ† ๋ฆฌ ๊ธฐ๋ณธ ๋„๋ฉ”์ธ์—์„œ๋งŒ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.