배경 에 쿠키 값 을 설정 하여 프론트 데스크 에서 가 져 오기

배경 에 쿠키 값 을 설정 하여 프론트 데스크 에서 가 져 오기
 
 
쿠키 를 통 해 사용자 의 개성 화 된 정보 주의사항 을 얻 을 수 있 습 니 다. 1. 중국어 의 존 재 는 utf - 8 의 인 코딩 을 한 다음 에 디 코딩 을 하면 됩 니 다.가끔 cookie.setPath("/"); 문 구 는 SpringMVC 의 contrller 류 에 @ RequestMapping ("/ User") 이 설정 되 어 있 기 때문에 사용 되 고 무시 하지 않 습 니 다.쿠키 의 저장 시간 을 설정 합 니 다 cookie. setMaxAge (60 * 60 * 24);
@RequestMapping("login")
	public String goto_login(HttpServletResponse response, HttpSession session, T_MALL_USER_ACCOUNT user,
			HttpServletRequest request, ModelMap map) {

		//   ,        
		T_MALL_USER_ACCOUNT select_user = loginMapper.select_user(user);

		if (select_user == null) {
			return "redirect:/login.do";
		} else {
			session.setAttribute("user", select_user);

			//              ,              
			try {
				Cookie cookie = new Cookie("yh_mch", URLEncoder.encode(select_user.getYh_mch(), "utf-8"));
				// cookie.setPath("/");
				cookie.setMaxAge(60 * 60 * 24);
				response.addCookie(cookie);

				Cookie cookie2 = new Cookie("yh_nch", URLEncoder.encode("   ", "utf-8"));
				// cookie.setPath("/");
				cookie2.setMaxAge(60 * 60 * 24);
				response.addCookie(cookie2);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		return "redirect:/index.do";
	}

쿠키 의 값 을 가 져 오 는 두 가지 방법 이 있 습 니 다. 하 나 는 서버 로 클 라 이언 트 의 데 이 터 를 가 져 온 다음 에 데 이 터 를 클 라 이언 트 에 게 되 돌려 주 는 것 입 니 다.다른 하 나 는 클 라 이언 트 가 직접 가 져 오 는 것 입 니 다. (이런 것 을 추천 합 니 다. 편리 하고 서버 를 선택 하면 매번 한 페이지 만 되 돌아 갈 수 있 고 실 용적 이지 않 습 니 다)첫 번 째 (추천 하지 않 음): 
	@RequestMapping("index")
	public String index(HttpServletRequest request, ModelMap map) {

		
		 String yh_mch = ""; 
		 Cookie[] cookies = request.getCookies(); 
		 if(cookies != null && cookies.length > 0) {
			 for (int i = 0; i 

두 번 째: 클 라 이언 트 쿠키 를 통 해 사용자 의 개성 화 된 정 보 를 얻 는 decodeURIComponent 는 js 자체 의 utf - 8 디 코딩 함수 yhnch = "안녕하세요";yh_mm = "123456" 에서 가 져 온 쿠키 문자열 은 "빈 칸" 과 ";" 이 나타 납 니 다. 정규 일치 가 필요 합 니 다. 먼저 빈 칸 을 제거 한 다음 ";" 로 분할 하고 분 단 된 배열 은 "=" 분할 하여 필요 한 값 을 가 져 옵 니 다.yh_안녕하세요.

	$(function (){
		var yh_mch = getMyCookie("yh_nch");
		$("#show_name").text(decodeURIComponent(yh_mch));
	});
	
	function getMyCookie(key){
		var val = "";
	
		//  cookie  
		var cookies = document.cookie;
		cookies = cookies.replace(/\s/,"");
		var cookie_array = cookies.split(";");
		for(i=0;i<cookie_array.length;i++){
			// yh_mch=lilei
			var cookie = cookie_array[i];
			var array = cookie.split("=");
			if(array[0]==key){
				val = array[1];
			}
		}
		
		return val;
	}

좋은 웹페이지 즐겨찾기