배경 에 쿠키 값 을 설정 하여 프론트 데스크 에서 가 져 오기
2975 단어 J2EE 개발 사례Javascript
쿠키 를 통 해 사용자 의 개성 화 된 정보 주의사항 을 얻 을 수 있 습 니 다. 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;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Javascript가 Crontab 표현식의 유효성 여부를 판단하는 상세 정보Javascript는 Crontab 표현식의 유효성 여부를 판단합니다. 그동안 Quartz 작업 스케줄링을 하면서 사용한 Crontab 표현식으로 이루어졌습니다.Crontab은 프런트엔드 페이지에서 입력한 매개변수로...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.