ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 11. JSP 내장 객체
    JSP/실전 JSP 2022. 11. 5. 22:51

    11-1. config 객체

    • web.xml (init param) --- getinitParameter() ---> jsp (int param)

     

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
      <display-name>testPjtConfig</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      
     
      <servlet>
        <servlet-name>servletEx</servlet-name>
        <jsp-file>/jspEx.jsp</jsp-file>
        <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>
      <servlet-mapping>
        <servlet-name>servletEx</servlet-name>
        <url-pattern>/jspEx.jsp</url-pattern>
      </servlet-mapping>
    </web-app>

     

    jspEx.jsp

    <%@ page language="java" contentType="text/html; charset=EUC-KR"
        pageEncoding="EUC-KR"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="EUC-KR">
    <title>Insert title here</title>
    </head>
    <body>
    	<%! 
    	String adminId;
    	String adminPw;
    	%>
    	
    	<%
    	adminId = config.getInitParameter("adminId");
    	adminPw = config.getInitParameter("adminPw");
    	%>
    	
    	<p>adminId : <%= adminId %>
    	<p>adminPw : <%= adminPw %>
    	
    </body>
    </html>

     

     

    11-2. application 객체

    • 어플리케이션 전체(모든 jsp)에 데이터를 공유할 수 있음
    • 공통적으로 사용하는 정보들을 저장해놓고 공유해서 사용할수  있음
    • web.xml(context param) --- getinitParameter() ---> jsp(context param), jsp(context param), jsp(context param)

     

    context-param

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
      <display-name>testPjtConfig</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      
      <context-param>
       <param-name>imgDir</param-name>
       <param-value>/upload/img</param-value>
      </context-param>
      <context-param>
       <param-name>testServerIP</param-name>
       <param-value>127.0.0.1</param-value>
      </context-param>
      <context-param>
       <param-name>realServerIP</param-name>
       <param-value>68.0.30.1</param-value>
      </context-param>
      
    </web-app>

     

    jspEx.jsp

    <%@ page language="java" contentType="text/html; charset=EUC-KR"
        pageEncoding="EUC-KR"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="EUC-KR">
    <title>Insert title here</title>
    </head>
    <body>
    	<%! 
    	String imgDir;
    	String testServerIP;
    	String realServerIP;
    	%>
    	
    	<%
    	imgDir = application.getInitParameter("imgDir");
    	testServerIP = application.getInitParameter("testServerIP");
    	realServerIP = application.getInitParameter("realServerIP");
    	%>
    	
    	<p>imgDir : <%= imgDir %>
    	<p>testServerIP : <%= testServerIP %>
    	<p>realServerIP : <%= realServerIP %>
    	
    </body>
    </html>

     

    application.setAttribute(" "," "), application.getAttribute(" "," ")

    jspEx.jsp

    <%@ page language="java" contentType="text/html; charset=EUC-KR" 
       pageEncoding="EUC-KR"%>
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="EUC-KR">
            <title>Insert title here</title>
        </head>
        <body>
    		
    		
    		<%
    			application.setAttribute("connectedIP", "165.62.58.23");
    			application.setAttribute("connectedUser", "hong");
    		%>
    		
    		
        </body>
    </html>

     

    jspExGet.jsp

    <%@ page language="java" contentType="text/html; charset=EUC-KR" 
       pageEncoding="EUC-KR"%>
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="EUC-KR">
            <title>Insert title here</title>
        </head>
        <body>
    		
    		<%!
    			String connectedIP;
    			String connectedUser;
    			
    		%>
    		
    		<!-- application 객체 -->
    		<%
    			connectedIP = (String)application.getAttribute("connectedIP");
    			connectedUser = (String)application.getAttribute("connectedUser");
    		%>
    		
    		<p>connectedIP : <%= connectedIP %></p>
    		<p>connectedUser : <%= connectedUser %></p>
    		
        </body>
    </html>

     

     

    11-3. out 객체

    jspEx.jsp

    <%@ page language="java" contentType="text/html; charset=EUC-KR" 
       pageEncoding="EUC-KR"%>
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="EUC-KR">
            <title>Insert title here</title>
        </head>
        <body>
    		
    		
    		<!-- out 객체 -->
    		<%
    			out.print("<h1>Hello JAVA World!!</h1>");
    			out.print("<h2>Hello JSP World!!</h2>");
    			out.print("<h3>Hello Servlet World!!</h3>");
    		%>
    		
    		
        </body>
    </html>

     

     

    11-4. exception 객체

    jspEx.jsp

    <%@ page language="java" contentType="text/html; charset=EUC-KR" 
       pageEncoding="EUC-KR"%>
    <%@ page errorPage="errorPage.jsp"%>
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="EUC-KR">
            <title>Insert title here</title>
        </head>
        <body>
    		
    		<%!
    			String str;
    		%>
    		
    		<!-- exception 객체 -->
    		<%
    			out.print(str.toString());
    		%>
    		
        </body>
    </html>

     

    errorPage.jsp

    <%@ page language="java" contentType="text/html; charset=EUC-KR" 
       pageEncoding="EUC-KR"%>
    <%@ page isErrorPage="true"%>
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="EUC-KR">
            <title>Insert title here</title>
        </head>
        <body>
    
    		<%
    			response.setStatus(200);
    			String msg = exception.getMessage();
    		%>
    		
    		<h1> error message : <%= msg %> </h1>
    
        </body>
    </html>

     

    'JSP > 실전 JSP' 카테고리의 다른 글

    13. Cookie  (0) 2022.11.06
    12. Servlet 데이터 공유  (0) 2022.11.06
    10. JSP request, response  (0) 2022.11.05
    9. JSP 스크립트  (0) 2022.11.05
    8. form 데이터 처리  (0) 2022.11.05

    댓글

Designed by Tistory.