<쿼리스트링>
getAttribute(attr 셋팅된거만 가져올 수 있음)
setAttribute(key, value);
req.setAttribute("key", value);
//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);
implements Filter
->
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
}
@WebFilter()
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("필터 다시 나갈게요~~");
}
get, post 다 가능
ServletContext applicatoin = req.getServletContext() ;
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 쿠키로!!
@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 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("쿠키~~");
}
}
@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("쿠키 체크~~");
}
}