サーバ04セッション、Cookie
22568 ワード
attribute
getAttribute(attr 셋팅된거만 가져올 수 있음)
setAttribute(key, value);
setAttribute()
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);
filter()
0)パッケージの作成とクラスの作成
1) implements Filter
2)カーソル+ctrl+1
->
3)setCharacterEncoding()をdoFilter()に追加
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
}
4) @WebFilter()
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
}
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+" 세션에 집어넣음!!");
}
}
->仲介サーバはセッションをどのように区別しますか->JSSIONID Cookieで!!
セッションを期限切れにする
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
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+" 세션에 집어넣음!!");
}
}
@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 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("쿠키~~");
}
}
Cookieのインポート: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("쿠키 체크~~");
}
}
Reference
この問題について(サーバ04セッション、Cookie), 我々は、より多くの情報をここで見つけました https://velog.io/@lecharl/서버04テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol