ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • jsp 내장객체
    jsp&servlet 2020. 6. 30. 17:57

    request, response 외 jsp에서 기본적으로 제공하는 객체에 대해서 학습합니다.

     

    config 객체

    웹 환경설정이라고 할 수 있는 web.xml에 어떠한 데이터를 명시해주고 그것을 getInitParameter()이라는 메소드를 사용하여 jsp에서 데이터를 공유하는 방식

     

    동작방식

    web.xml에서 init-param 선언 (servletEx servlet에서만 사용가능)

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

     

    jsp

    jsp에서 web.xml에서 선언한 init-param 데이터를 불러와서 사용할 수 있습니다.

    <%!
    		String adminId;
    		String adminPw;
    	%>
    	
    	<%
    			adminId = getServletConfig().getInitParameter("adminId");
    			adminPw = getServletConfig().getInitParameter("adminPw");
    	%>

     

    jsp & web.xml 실제 코딩 내용

    결과

     

    application 객체

    config객체는 하나의 servlet에서만 데이터를 사용할 수 있는 반면 application객체는 프로젝트 전체에서 데이터를 공유할 수 있는 방법 입니다

     

     

    web.xml에 context param 설정

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

     

    jspEx.jsp , jspEx2.jsp 두개의 jsp에서 comtext-param 데이터를 호출해서 사용해보겠다.

     

    서버를 실행시켜보면

     

    다음과 같이 실행된다 이로써 어떤 jsp 어떤 프로젝트에서도 context-param을 공동으로 사용가능한것을 알 수 있다!

     

    jsp에서 데이터를 setting 하고 다른 jsp에서 그 데이터를 사용하는법

     

    jspEx.jsp

    		application.setAttribute("id", "oh");
    		getServletContext().setAttribute("connectIP", "192.0.0.1");

     

    jspEx2.jsp

        <%
    		String connectIP = (String)getServletContext().getAttribute("connectIP");
    		String id = (String)application.getAttribute("id");	
    	%>
    	<p>connectIP =<%= connectIP %>
    	<p>id =<%= id %>

     

    jspEx.jsp에서 데이터 셋팅후

    jspEx2.jsp에서 데이터 대입

     

    out 객체

    out.print라는 메소드를 사용해서 html 사용가능

    ex)out.print("<p>hello</p>");

     

    exception 객체

    에러가 발생했을때 에러처리를 수행해주는 객체

     

    에러가 있는 jsp

    <%@page import="java.util.ArrayList"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@page errorPage="errorPage.jsp" %><!-- 에러가 발생하면 해당 페이지로 이동 -->
    
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    	<%!
    		String imgDir;
    		String testServerIp;
    		String str;
    	%>
    	
    	<%
    		imgDir = application.getInitParameter("imgDir");
    		testServerIp = application.getInitParameter("testServerIp");
    		out.print(str.toString());//초기화를 안해서 에러가 나는 부분
    			
    	%>
    	<p>imgDir =<%= imgDir %></p>
    	<p>testServerIp =<%= testServerIp %></p>
    </body>
    </html>

     

    에러 페이지

    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@page isErrorPage="true" %><!-- 이 페이지를 에러페이지로 사용하겟다 -->
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    	<%
    		//response.setStatus(200);
    		String msg = exception.getMessage();//에러내용을 msg에 대입
    	%>
    	<h1>error message: <%= msg %></h1>
    </body>
    </html>

     

    결과

    error message: null

     

    jsp페이지 내에서 에러가 발생하면 지정해둔 에러페이지로 이동해서 등록한 로직대로 에러페이지가 실행됨

    'jsp&servlet' 카테고리의 다른 글

    jsp request,reponse  (0) 2020.06.30
    jsp 주요 script  (0) 2020.06.30
    form 데이터 처리  (0) 2020.06.30
    servlet life-cycle  (0) 2020.06.30
    servlet request,response  (0) 2020.06.30
Designed by Tistory.