Archive

JSP - Action Tags(2)

|

JSP - Action Tags(2)


  1. include 액션태그

    • include가 위치한 곳에 지정한 페이지를 포함시킬 수 있다

    • 각각의 파일들은 개별적으로 컴파일된 후 include 액션 태그에 의해 합쳐진다

    • flush 속성은 false로 많이 쓰며, 지정한 페이지 실행시 버퍼의 내용을 제거한다

    • <jsp:include file=”포함할 페이지이름” flush=”false”>

      <jsp:param name=”변수명” value=”값” />

      </jsp:include>

    • JSP 모듈화 예제

    <!-- temp/template/template.jsp -->
    <%@ page language="java" contentType="text/html; charset=EUC-KR"
        pageEncoding="EUC-KR"%>
    <%
    	// 속성을 이용한 동적처리 방법
    	String pageTitle = (String) request.getAttribute("PAGETITLE");
    	// 파라미터를 이용한 동적처리 방법
    	String contentPage = request.getParameter("CONTENTPAGE");
    %>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="EUC-KR">
    <title><%= pageTitle %></title><!--타이틀 동적처리-->
    </head>
    <body>
    <table width="400" border="1" cellpadding="2" cellspacing="0">
    <tr>
         <td colspan="2">
             <jsp:include page="/temp/module/top.jsp" flush="false" />
         </td>
     </tr>
     <tr>
         <td width="100" valign="top">
             <jsp:include page="/temp/module/left.jsp" flush="false" />
         </td>
         <td width="300" valign="top">
             <!-- 내용부분 : 시작 -->
             <!-- 페이지의 내용부분을 동적처리-->
             <jsp:include page="<%=contentPage %>" flush="false" />
             <!-- 내용부분 : 끝 -->
         </td>
     </tr>
     <tr>
         <td colspan="2">
             <jsp:include page="/temp/module/bottom.jsp" flush="false" />
         </td>
     </tr>
    </table>
    </body>
    </html>
    
    <!-- temp/module/top.jsp -->
    <%@ page language="java" contentType="text/html; charset=EUC-KR"
        pageEncoding="EUC-KR"%>
    상단 메뉴 :
    <a href="#">HOME</a>
    <a href="#">INFO</a>
    
    <!-- temp/module/left.jsp -->
    <%@ page language="java" contentType="text/html; charset=EUC-KR"
        pageEncoding="EUC-KR"%>
    좌측 메뉴:
    
    <!-- temp/module/bottom.jsp -->
    <%@ page language="java" contentType="text/html; charset=EUC-KR"
        pageEncoding="EUC-KR"%>
    하단 메뉴: 소개 | 도움말 | 약관 | 사이트맵
    
    <!-- temp/template/info_view.jsp : 내용부분 -->
    <%@ page language="java" contentType="text/html; charset=EUC-KR"
        pageEncoding="EUC-KR"%>
    <table width="100%" border="1" cellpadding="0" cellspacing="0">
    <tr>
    	<td>제품번호</td> <td>1234</td>
    </tr>
    <tr>
    	<td>가격</td> <td>10,000원</td>
    </tr>
    </table>
    <jsp:include page="infoSub.jsp" flush="false" >
    	<jsp:param value="A" name="type"/>
    </jsp:include>
    
    <!-- temp/template/infoSub.jsp -->
     <%@ page language="java" contentType="text/html; charset=EUC-KR"
        pageEncoding="EUC-KR"%>
     <%
         String type = request.getParameter("type");
         if(type != null) {
     %>
     <br>
     <table width="100%" border="1" cellpadding="0" cellspacing="0">
         <tr>
             <td>타입</td>
             <td><b><%= type %></b></td>
         </tr>
    	<tr>
    		<td>특징</td>
    		<td>
    		<% if(type.equals("A")){ %>
    			강한 내구성
    		<% } else if(type.equals("B")) { %>
    			뛰어난 대처 능력
    		<% } %>
    		</td>
    	</tr>
     </table>
    <% } %>
    
    <!-- temp/mainPage.jsp -->
    <%@ page language="java" contentType="text/html; charset=EUC-KR"
        pageEncoding="EUC-KR"%>
    <% request.setAttribute("PAGETITLE", "정보보기"); %>
    <jsp:forward page="/temp/template/template.jsp">
    	<jsp:param value="info_view.jsp" name="CONTENTPAGE"/>
    </jsp:forward>
    
    • 실행화면

    실행


  2. include 액션태그와 include 지시어의 비교

    비교



참고 자료


KG 아이티뱅크 강의 자료

처음해보는 JSP&Servlet 웹 프로그래밍

JSP - Action Tags

|

JSP - Action Tags


  1. JSP 액션태그

    • 클라이언트 혹은 서버에게 어떤 행동을 하도록 지시하는 태그이다.

    • <jsp:param> 액션 태그 : 파라미터 값을 전달할 때 사용한다

      • <jsp:param name=”파라미터 변수” value=”값” />
    • <jsp:forward> 액션 태그 : 페이지 이동할 때 사용한다

      • <jsp:forward page=”이동할 페이지”>

        <jsp:param name=”변수명” value=”값” />

        </jsp:forward>

    • 페이지 이동 예제

    <!-- /forward/select.jsp -->
    <%@ page language="java" contentType="text/html; charset=EUC-KR"
        pageEncoding="EUC-KR"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="EUC-KR">
    <title>옵션 선택 화면</title>
    </head>
    <body>
    <form action="<%= request.getContextPath() %>/forward/view.jsp">
    보고 싶은 페이지 선택 :
    	<select name="code">
    		<option value="A">A 페이지</option>
    		<option value="B">B 페이지</option>
    		<option value="C">C 페이지</option>
    	</select>
    <input type="submit" value="이동">
    </form>
    </body>
    </html>
    
    <!-- forward/view.jsp -->
    <%@ page language="java" contentType="text/html; charset=EUC-KR"
        pageEncoding="EUC-KR"%>
    <%
    	String code = request.getParameter("code");
    	String viewPageURI = null;
    
    	if(code.equals("A")){
    		viewPageURI = "viewModule/a.jsp";
    	} else if (code.equals("B")){
    		viewPageURI = "viewModule/b.jsp";
    	} else if (code.equals("C")) {
    		viewPageURI = "viewModule/c.jsp";
    	}
    %>
    <jsp:forward page="<%= viewPageURI %>" />
    
    <!-- /forward/viewModule/a.jsp -->
    <%@ page language="java" contentType="text/html; charset=EUC-KR"
        pageEncoding="EUC-KR"%>
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="EUC-KR">
            <title>A 페이지</title>
        </head>
        <body>
            이 페이지는 <b><font size="5">A</font></b> 페이지입니다.
        </body>
    </html>
    


  • 실행화면

실행1 실행2



참고 자료


KG 아이티뱅크 강의 자료

처음해보는 JSP&Servlet 웹 프로그래밍

JSP - Attribute & Scope

|

JSP - Attribute & Scope


  1. 속성과 스코프

    • 속성(Attribute)은 PageContext, HttpServletRequest, HttpSession, ServletContext 객체에게 설정해놓은 객체를 말한다
    • 속성은 이름, 값 쌍으로 저장된다
    • 각각의 객체에 저장된 속성들은 서로 다른 생존범위(scope)를 가진다

    속성과스코프

    • 속성과 파라미터의 차이점

    차이점

    • MVC 디자인 패턴에서 Model에서 View쪽으로 데이터를 넘겨줄 때, request scope에 데이터를 저장해서 View쪽으로 데이터를 전달할 수 있다
    <%
    pageContext.setAttribute("pageAttribute", "리무");
    %>
       
    <%
    request.setAttribute("requestAttribute", "010-xxxx-xxxx");
    %>
       
    <%
    session.setAttribute("sessionAttribute", "xxxx@test.com");	
    %>
       
    <%
    application.setAttribute("applicationAttribute", "KG ITBank");
    %>
       
    <ul>
    	<li>이름 : <%=pageContext.getAttribute("pageAttribute") %></li>
    	<li>전화 : <%=request.getAttribute("requestAttribute") %></li>
    	<li>메일 : <%=session.getAttribute("sessionAttribute") %></li>
    	<li>회사 : <%=application.getAttribute("applicationAttribute") %></li>
    </ul>
    
    • 실행 화면

    실행



참고 자료


KG 아이티뱅크 강의 자료

처음해보는 JSP&Servlet 웹 프로그래밍

JSP - 내장객체(2)

|

JSP - 내장객체(2)


  1. pageContext

    • 범위(scope) : Page
    • JSP 페이지를 위한 context로, pageContext 기본 객체는 다른 기본객체에 접근할 수 있는 메소드를 제공한다
    • 주요 메소드

    pageContext

    <%
    	HttpServletRequest httpRequest =
    	//gerRequest()의 반환형이 ServletRequest이므로 강제 형변환
    		(HttpServletRequest)pageContext.getRequest();
    %>
    request 기본 객체와 pageContext.getRequest()의 동일 여부:
    <%= request == httpRequest %><br>
    pageContext.getOut() 메소드를 사용한 데이터 출력:
    <% pageContext.getOut().println("Hello World"); %>
    

    실행

  2. session

    • 범위(scope) : Session
    • 세션 객체를 나타낸다
  3. application

    • 범위(scope) : Application
    • application 기본 객체는 JSP 페이지의 서블릿 컨텍스트를 말한다. application 기본 객체는 웹 어플리케이션 당 하나씩 존재한다
    • 초기파라미터 메소드

    application

    • 서버정보 메소드

    서버정보

    • 로그 메시지 기록 메소드

    로그기록

    • 웹 어플리케이션 자원 메소드

    자원

  4. out

    • 범위(scope) : Page
    • JSP 페이지의 출력을 담당. 웹 브라우저 응답을 출력하기 위한 JspWriter 객체
    • 주요 메소드

    out

  5. config

    • 범위(scope) : Page
    • JSP 페이지에서 사용할 초기 파라미터를 저장하고 있다
  6. page

    • 범위(scope) : Page
    • Page 기본 객체는 JSP 페이지 그 자체를 의미한다
  7. exception

    • exception 기본 객체는 JSP 페이지에서 발생한 에러를 처리할 때 사용한다. page 지시어의 isErrorPage가 true로 설정되는 경우에만 exception 내장객체를 사용할 수 있다



참고 자료


KG 아이티뱅크 강의 자료

처음해보는 JSP&Servlet 웹 프로그래밍

JSP - 내장객체

|

JSP - 내장객체


  1. JSP 내장객체

    내장객체

    • 내장객체는 JSP 에서 기본적으로 제공해주는 객체들이다
    • JSP 컨테이너에 의해 생성되고 제공되기 때문에 따로 선언하거나 생성하지 않아도 사용할 수 있다
    • session, exception 객체는 page 지시어의 설정에 따라 사용여부를 결정할 수 있다
  2. request

    • 범위(scope) : Request
    • 서블릿의 service 메소드의 매개변수인 HttpServletRequest와 동일하게 사용한다
    • 주요 메소드

    request

    클라이언트 IP : <%= request.getRemoteAddr() %><br>
    요청정보 길이 : <%= request.getContentLength() %><br>
    요청정보 인코딩 : <%= request.getCharacterEncoding() %><br>
    요청정보 컨텐트타입 : <%= request.getContentType() %><br>
    요청정보 프로토콜 : <%= request.getProtocol() %><br>
    요청정보 전송방식 : <%= request.getMethod() %><br>
    요청 URL : <%= request.getRequestURL().toString() %><br>
    요청 URI : <%= request.getRequestURI() %><br>
    컨텍스트 경로 : <%= request.getContextPath() %><br>
    서버이름 : <%= request.getServerName() %><br>
    서버포트 : <%= request.getServerPort() %><br>
    

    실행


    • HTML Form 데이터와 요청 파라미터와 관련된 메소드

    Form메소드

    <!-- reqex2.jsp -->
    <form action="viewParameter.jsp" method="post">
    이름: <input type="text" name="name" size="10"><br>
    주소: <input type="text" name="address" size="30"><br>
    좋아하는 동물:
    	<input type="checkbox" name="pet" value="dog">강아지
    	<input type="checkbox" name="pet" value="cat">고양이
    	<input type="checkbox" name="pet" value="pig">돼지
    <br>
    <input type="submit" value="전송">
    </form>
    
    <!-- viewParameter.jsp -->
    <b>request.getParameter() 메소드 사용</b><br>
    name 파라미터 = <%= request.getParameter("name") %><br>
    address 파라미터 = <%= request.getParameter("address") %><br>
    <br><br>
    <b>reqeust.getParameterValues() 메소드 사용</b><br>
    <%
    	String[] values = request.getParameterValues("pet");
    	if(values != null) {
    		for(int i=0; i<values.length; i++){
    %>
    	<%= values[i] %>
    <%
    		}
    	}
    %>
    <br><br>
    <b>request.getParameterNames() 메소드 사용</b><br>
    <%
    	Enumeration enumData = request.getParameterNames();
    	while(enumData.hasMoreElements()){
    		// 강제 형변환 필요
    		String name = (String) enumData.nextElement();
    %>
    	<%= name %>
    <%
    	}
    %>
    <br><br>
    <b>request.getParameterMap() 메소드 사용</b><br>
    <%
    	Map parameterMap = request.getParameterMap();
    	String[] nameParam = (String[])parameterMap.get("name");
    	if(nameParam != null) {
    %>
    	name = <%= nameParam[0] %>
    <%
    	}
    %>
    

    실행1

    실행2


    • 요청헤더 정보처리 관련 메소드

    헤더

    <!-- reqex3.jsp -->
    <%
    	Enumeration enumData = request.getHeaderNames();
    	while(enumData.hasMoreElements()) {
    		String headerName = (String) enumData.nextElement();
    		String headerValue = request.getHeader(headerName);
    %>
    <%= headerName %> = <%= headerValue %><br>
    <%
    	}
    %>
    

    실행


  3. response

    • 범위(scope) : Request
    • 서블릿의 service 메소드의 매개변수인 HttpServletResponse와 동일하게 사용한다

    response



참고 자료


KG 아이티뱅크 강의 자료

처음해보는 JSP&Servlet 웹 프로그래밍