서버04_세션, 쿠키

charl hi·2021년 12월 13일
0

서버

목록 보기
4/15

attribute

  • attr 이 안된다

<쿼리스트링>

  • getAttribute(attr 셋팅된거만 가져올 수 있음)
  • setAttribute(key, value);


setAttribute()

req.setAttribute("key", value);
  • setAttribute()로 설정해주고 보낸다.
		//getParameter
		String id = req.getParameter("id");
		System.out.println(id);
		
		//attribute
		req.setAttribute("id", id);
		
		//보내버린다.
		RequestDispatcher dispatcher = req.getRequestDispatcher("views/attrTest.jsp");
		dispatcher.forward(req, resp);




filter()

0) 패키지 생성하고 클래스 생성


1) implements Filter


2) 커서 + ctrl + 1

->


3) doFilter()에 setCharacterEncoding()

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {

		request.setCharacterEncoding("UTF-8");
	}

4) @WebFilter()

  • ✨✨url에 따라 필터를 걸 수 있다!!!

5) chain.doFilter()

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		System.out.println("필터왔음!!");
		request.setCharacterEncoding("UTF-8");
		//chain : 다음으로 보내줄 수 있다.
		chain.doFilter(request, response);
		System.out.println("필터 다시 나갈게요~~");        
	}

  • 한글로 해도 잘 된다~!




session

service()

get, post 다 가능



req.getServletContext()

ServletContext applicatoin = req.getServletContext() ; 


servletContext

package com.kh.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/sessionSet")
public class SessionSet extends HttpServlet {
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String data = req.getParameter("data");
		
		HttpSession ses = req.getSession();
		ses.setAttribute("data", data);
		System.out.println("파라미터로 전달받은 "+data+" 세션에 집어넣음!!");
	}
}


-> ❓❓ 서버가 session을 어떻게 구분할까?? -> JSESSIONID 쿠키로!!



세션 만료시키기

invalidate()


@WebServlet("/sessionDelete")
public class SessionDelete extends HttpServlet{
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		HttpSession session = req.getSession();
		
		//세션 만료시키기
		session.invalidate();
		System.out.println("세션 만료시킴~!");
	}
}




cookie

  • key & value 로 매핑되어 있음

쿠키 만들기

Cookie cookie = new Cookie("num", "1");
resp.addCookie(cookie);

@WebServlet("/cookie")
public class CookieServlet extends HttpServlet{
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//쿠키 만들기
		Cookie cookie = new Cookie("num", "1");
		
		//쿠키 경로 설정 : url /abc 를 요청할 때만 ???
		cookie.setPath("/abc");
		//1초 뒤 쿠키 자동 소멸
//		cookie.setMaxAge(1);
		//하루동안 쿠키 자동 소멸
		cookie.setMaxAge(60*60*24);
		
		resp.addCookie(cookie);
		
		System.out.println("쿠키~~");
	}
}



쿠키 가져오기 : getCookies()

  • 배열 형태로!!

@WebServlet("/cookieCheck")
public class CookieCheck extends HttpServlet{
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//쿠키 가져오기
		//반환타입 : 쿠키배열타입!!
		Cookie[] cookies = req.getCookies();
		for(Cookie c : cookies) {
			String name = c.getName();
			String value = c.getValue();
			System.out.println("[쿠키] : "+ name +" : "+ value);
		}
		System.out.println("쿠키 체크~~");
	}
}

0개의 댓글