본문 바로가기

JAVA

JSP 내장객체

-config 객체-

web.xml 데이터를 저장

jsp 에서 getInitParameter() jsp 내에서 호출 하여 사용 한다.

ex)

<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>

-jsp 내에서 호출 방법-

<body>
<%!
String adminId;
String adminPw;
%>

<%
adminId = config.getInitParameter("adminId");
adminPw = config.getInitParameter("adminPw");
%>
<p>adminId : <%= adminId%></p>
<p>adminPw : <%= adminPw%></p>

</body>

해당 서블릿 (jspEx.jsp) 내에서만 해당 데이터를 사용 할수있다.

-application 객체-

모든 어플리케이션 객체에 따른 값을 지정 해줄수있다.

<context-param>
    <param-name>imgDir</param-name>
    <param-value>/upload/img</param-value>
</context-param>
<context-param>
    <param-name>testServer</param-name>
    <param-value>127.0.0.1</param-value>
</context-param>
<context-param>
    <param-name>realServer</param-name>
    <param-value>192.168.0.1</param-value>
</context-param>

  • application 객체를 사용 하기 위해 web.xml에서 context-param에 데이터 값을 구성 한다.
  • <%!
    String adminId;
    String adminPw;
    String imgDir;
    String testServer;
    String realServer;
    %>

    <%
    adminId = config.getInitParameter("adminId");
    adminPw = config.getInitParameter("adminPw");
    %>
    <%
    imgDir = application.getInitParameter("imgDir");
    testServer = application.getInitParameter("testServer");
    realServer = application.getInitParameter("realServer");
    %>
  • 구성 후 application.getInitParameter 객체를 사용 하여 web.xml에 지정한 데이터를 받아올수있다.

setAttribute()

  • 속성을 지정 해줄수있다.
  • setAttribute 객체 사용방법
    • application.setAttribute("connectedIp", "192.168.0.1");
      application.setAttribute("connectedUser", "admin");

getAttribute()

  • setAttribute() 객체에서 지정한 값을 받아온 후 출력 할수있는객체
    • <%
      String connectedIp;
      String connectedUser;
      %>
      <%
      connectedIp = (String) application.getAttribute("connectedIp");
      connectedUser = (String) application.getAttribute("connectedUser");
      %>
    • 다른 jsp 파일에서 setAttribute 객체 를 사용한 지정 값을 구성 중인 jsp 파일 내에서 getAttribute() 객체를 사용 하여 데이터 값을 받아올수있다.

어플리케이션 객체를 가져 올때는 (String) 형변환을 지정 해준다음 가져와야 한다.

'JAVA' 카테고리의 다른 글

Session  (0) 2021.07.01
Cookie  (0) 2021.06.30
JSP 스크립트  (0) 2021.06.27
form 데이터 처리  (0) 2021.06.26
Servlet Life-Cycle  (0) 2021.06.26