티스토리 뷰
저번 포스트에서 jsp 파일이나 어플리케이션끼리 데이터를 공유해서 사용하는 방법을 공부해봤는데요, 이번에는 Servlet에서 데이터를 공유해 사용하는 방법을 공부해보겠습니다.
그런데 사실 아시다시피 jsp에서 데이터를 공유했던 방법이 jsp가 웹 컨테이너에서 Servlet으로 변환하는 것을 이용해 web.xml에 코드를 추가하고 내장객체를 이용했습니다. (참고 : JSP 내장객체)
따라서 Servlet에서는 똑같이 web.xml에 설정을 한 뒤, 이를 가져오는 객체를 사용해 주면 되겠습니다.
내용이 거의 동일하므로 간단하게 방법만 알아보겠습니다.
Servlet parameter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <servlet> <servlet-name>servletEx</servlet-name> <servlet-class>com.servlet.ServletEx</servlet-class> </servlet> <init-param> <param-name>adminId</param-name> <param-value>admin</param-value> </init-param> <init-param> <param-name>adminPw</param-name> <param-value>1234</param-value> </init-param> <servlet-mapping> <servlet-name>servletEx</servlet-name> <url-pattern>/se</url-pattern> </servlet-mapping> | cs |
web.xml에 초기화 할 데이터를 넣어주는 부분은 jsp와 같습니다. 단지 적용할 파일이 Servlet이므로 경로를 패키지 명과 클래스 명으로 지정해 줍니다.
이 초기화 된 데이터를 Servlet의 객체로 가져오면 됩니다.
1 2 3 4 5 6 | String adminId = getServletConfig().getInitParameter("adminId"); String adminPw = getServletConfig().getInitParameter("adminPw"); PrintWriter out = response.getWriter(); out.print("<p>adminId : " + adminId + "</p>"); out.print("<p>adminPw : " + adminPw + "</p>"); | cs |
context parameter
jsp에서 application 내장객체를 이용해 어플리케이션내의 jsp끼리 초기화 한 context 파라미터를 공유했었습니다.
1 2 3 4 5 6 7 8 9 10 | <context-param> <param-name>imgDir</param-name> <param-value>/upload/img</param-value> </context-param> <context-param> <param-name>testServletIP</param-name> <param-value>127.0.0.1</param-value> </context-param> | cs |
이렇게 설정한 context-param을 Servlet에서 불러오는 객체를 생성하는 방법은 getServletContext()를 호출하면 됩니다.
1 2 | String imgDir = getServletContext().getInitParameter("imgDir"); String testServletIP= getServletContext().getInitParameter("testServletIP"); | cs |
그런다음 역시 getInitParmeter()로 초기화 한 파라미터를 가져옵니다.
context attribute
속성 값도 마찬가지입니다.
1 2 3 4 5 | getServletContext().setAttribute("connectedIP","123.45.67.8"); getServletContext().setAttribute("connectedUser","test"); String connectedID = (String )getServletContext().getAttribute("connectedIP"); String connectedUser= (String )getServletContext().getAttribute("connectedUser"); | cs |
setAttribute로 설정을 해주고 getAttribute으로 가져오면 됩니다.
이때도 역시 강제 형 변환을 해줘야 합니다.
이 파라미터도 context이기 때문에 다른 Servlet에서 공유해 사용할 수 있습니다.
'Spring&Spring Boot > JSP' 카테고리의 다른 글
Session (0) | 2019.01.24 |
---|---|
Cookie (0) | 2019.01.23 |
JSP 내장객체 (0) | 2019.01.21 |
JSP request, response (0) | 2019.01.20 |
JSP 스크립트 (0) | 2019.01.19 |
- JPA
- Gradle
- 지옥에서 온 git
- JUnit
- Do it! 정직하게 코딩하며 배우는 딥러닝 입문
- 생활코딩 javascript
- 스프링 테스트
- 쉽게 배우는 운영체제
- 리버싱
- BOF
- Thymeleaf
- 스프링
- spring mvc
- 파이썬 for Beginner 연습문제
- 버퍼오버플로우
- 김영환
- Spring Boot
- 패킷 스위칭
- 선형 회귀
- 프로그래머스
- Computer_Networking_A_Top-Down_Approach
- shell code
- jsp
- 운영체제 반효경
- 쉘 코드
- Spring
- Spring Data JPA
- 파이썬 for Beginner 솔루션
- Python Cookbook
- 방명록 프로젝트
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
- Total
- Today
- Yesterday