서블릿 관련 api
1,서블릿 api계층 구조 및 주요 메서드
servlet
GenericServlet
HttpServlet
ServletRequest => HttpServletRequest
>>요청객체 한글 인코딩 - > request.setCharacterEncoding("utf-8);
>>요청데이터 가져오기 getParameter(K) : String
>> 쿠키 요청 가져오기 : getCookies();
>> 세션 생성: getSession(boolean), getSession()
>> getRequestDispacher("next.jsp"); : RequestDispacher
ServletResponse => HttpServletResponse
>>쿠키 응답 설정addCookie(Cookie)
>> sendRedirect(url);
>>setContentType("text/html; charser=utf-9"); :응답 위한 mime-type, charset > tomcat\conf> web.xml
Cookie
>> getValue() : String
>> getName() : String
HttpSession
>>값 설정 : setAttribute(Key, object);
>>값 조회 : getAttribute(K, object); = >형변환
>> 값 삭제 : romoveAttribute(K); : HttpSession, ServletContext
>> 세션 죽이기 : invalidate();
ServletConfig
>> 서블릿 개별 환경 설정 객체
ServletContext
>>웹어플리케이션 환경 설정 객체
>> 해당 웹 어플리케이션에 있는 모든 서블릿/jsp 공유 객체
2,servlet life cylcle
init(config)
>>재정의 generic method - init()
service(ServletRequest req, ServletResponse resp)
>>service(HttpServletRequest req,HttpServletResponse resp)
>>doGet(HttpServletRequest req,HttpServletResponse resp)
>>doPost(HttpServletRequest req,HttpServletResponse resp)
3.쿠키 / 세션 개념 및 관련 api 및 매서드, 코드 구현
-상태정보 관리하는 기법
--쿠키
Cookie c1 = new Cookie(k, v);/./쿠키생성
c1.setMaxAge(int); //쿠키 관련 설정
response.addCookie(c1);//응답시 쿠키 전송
Cookie[] cookies = request.getCookies(); // 쿠키 정보 가져오기
사용자 상태정보를 정보를 클라이언트에 저장
- 문자열만 가능 ,
- 크기,갯수제한 있음
--세션
HttpSession session = request.getSession();// true :없으면 new, 있으면 기존
HttpSession session = request.getSession();// false : 없으면 null, 있으면 기존
사용자 상태정보를 서버에 저장
- 모든 데이터 가능(object)
- 크기, 갯수 제한 없음
4, 페이지 이동방법
-- next.jsp 이동 코드
-- forward
>> 기존 요청/응답 객체 가지고 페이지 이동, 기존 정보 사용 가능
RequestDispather next = request.getRequestDispacher("next.jsp");
next.forward(request, response);
request.getRequ
-- redirect
>> 새로운 요청/응답객체 생성해서 페이지 이동
response.sendRedirect(request.getContextPath() + "next.jsp");
5.Scope 객체 범위 및 값 설정/조회/삭제 메서드
--pageContext : PageContext
> 현재 페이지
--requestScope : HttpServletRequest
> 요청~응답
--sessionScope : HttpSession
> 로그인 ~ 로그아웃(타임아웃)
--applicationScope : ServletContext
>어플리케이션 시작 ~ 어플리케이션 종료
>>값 설정 : setAttribute(Key, object);
>>값 조회 : getAttribute(K, object); = >형변환
>> 값 삭제 : romoveAttribute(K); : HttpSession, ServletContext
'Web' 카테고리의 다른 글
Front-End 개발 시작해보기 : Platform (0) | 2023.01.12 |
---|---|
[Backend] 월말평가대비 mvc pattern, jsp, EL (0) | 2022.09.30 |
[WEB] 예외처리 (0) | 2022.09.22 |
[WEB]jsp경로설정 (0) | 2022.09.21 |
[WEB]JSP,EL,JSTL (0) | 2022.09.20 |