330 likes | 626 Views
JSP 101 – Part 1. Internet Computing Laboratory @ KUT Youn-Hee Han. What is JSP?. 교재 28~31. JSP (Java Server Pages) 자바프로그램과 HTML 태그를 이용한 Server Side 애플리케이션 개발기술
E N D
JSP 101 – Part 1 Internet Computing Laboratory @ KUT Youn-Hee Han
What is JSP? 교재 28~31 • JSP (Java Server Pages) • 자바프로그램과 HTML태그를 이용한 Server Side 애플리케이션 개발기술 • a Java technology that allows software developers to create dynamic web pages that are both platform-independent and server-independent, giving you more freedom. • 등장배경 • 비즈니스 로직과 프리젠테이션 로직을 분리할 목적으로 등장. • 재사용성과 유지보수가 수월해진다. • 웹디자이너는 자바언어를 배우지 않고도 웹페이지를 수정할 수 있다. • 자바 개발자는 웹페이지의 디자인 작업 없이도 코드를 수정할 수 있다. • 용도 • 순수하게 클라이언트에 보여주는 작업만을 담당한다. • MVC 모델에서 View 에 해당한다. ( presentation logic ) Advanced Web Programming
What is JSP? • 기존 서블릿의 구성 • Business Logic 과 Presentation Login이 혼합된 형태이다. Advanced Web Programming
What is JSP? • Model 1 vs. Model 2 Model 1 Model 2 Advanced Web Programming
JSP 기본 예제 교재 41~44 • helloJSP 예제 /2006777888/helloJSP.jsp <html> <body> hello JSP </body> </html> Advanced Web Programming
JSP 원리 교재 112~114 • JSP 실행 3단계 • 변환 단계 (Translation Step) • jsp를 java로 변환한다. • helloJSP.jsp가 helloJSP_jsp.java 로 변환된다. • 컴파일 단계 (Compile Step) • 변환된 java파일을 컴파일 한다. • helloJSP_jsp.java 를 컴파일 하여 class를 생성한다. • 실행 단계 (Interpret Step) • 생성된 class를 실행하여 실행된 결과값을 응답처리 한다. • 변환파일 저장 위치 • D:\jakarta-tomcat-5.0.19\work\Catalina\localhost\2006777888\org\apache\jsp Advanced Web Programming
JSP 원리 교재 112~114 • 변환된 java 파일 package org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; public final class helloJSP_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent { private static java.util.Vector _jspx_dependants; public java.util.List getDependants() { return _jspx_dependants; } public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException { JspFactory _jspxFactory = null; PageContextpageContext = null; HttpSession session = null; ServletContextapplication = null; ServletConfigconfig = null; JspWriterout = null; Object page = this; JspWriter _jspx_out = null; PageContext _jspx_page_context = null; D:\jakarta-tomcat-5.0.19\work\Catalina\localhost\2006777888\org\apache\jsp\helloJSP_jsp.java Advanced Web Programming
JSP 원리 교재 112~114 • 변환된 java 파일 (계속) try { _jspxFactory = JspFactory.getDefaultFactory(); response.setContentType("text/html"); pageContext = _jspxFactory.getPageContext(this, request, response, null, true, 8192, true); _jspx_page_context = pageContext; application = pageContext.getServletContext(); config = pageContext.getServletConfig(); session = pageContext.getSession(); out = pageContext.getOut(); _jspx_out = out; out.write("<html>\r\n"); out.write("<body>\r\n"); out.write("hello JSP\r\n"); out.write("</body>\r\n"); out.write("</html>"); } catch (Throwable t) { if (!(t instanceof SkipPageException)){ out = _jspx_out; if (out != null && out.getBufferSize() != 0) out.clearBuffer(); if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); } } finally { if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context); } } } Advanced Web Programming
JSP 스크립팅 요소 교재 47 • JSP Scripting Elements • 항상 <% %> 태그로 구성된다. • 종류 주석 디렉티브 선언 스크립트릿 표현식 + 액션 태그 커스텀 태그 (사용자 생성 액션 태그) 표현 언어 (EL) 스크립팅 <jsp:include …/> XML 태그 독립 언어 Advanced Web Programming
JSP 주석 교재 108-111 • 주석의 종류 • HTML 주석 • JSP 전용 주석 • Java 주석 Advanced Web Programming
JSP 주석 교재 108-111 • 주석 예제 Advanced Web Programming
JSP Directive 교재 48 • Directive • <%@ …. @> • JSP가 서블릿으로 변환될 때 전반적인 영향을 줄 수 있는 정보를 제공한다. • 종류 • page Directive tag: <%@page ... %> • 컨테이너에게 현재 JSP페이지를 어떻게 처리할 것인가에 대한 정보를 제공하는데 사용된다. • 여러가지 속성을 이용한다. • include Directive tag: <%@include ... %> • 여러 JSP페이지에서 공통적인 내용이 있을 때 매번 공통적인 내용을 작성하지 않고 파일로 저장한 후 필요할 때 JSP페이지에서 파일을 삽입하여 사용할 수 있다. • taglib Directive tag:<%@taglib ... %> • 사용자에 의해서 만든 tag library를 이용할 때 사용된다. Advanced Web Programming
JSP Directive 교재 51-57 • Page Directive • page Directive 의 속성 종류 Advanced Web Programming
JSP Directive 교재 51-57 • Page Directive 예제 /2006777888/helloJSP.jsp <%@page contentType="text/html;charset=euc-kr" info="hello JSP"%> <%@page import="java.util.ArrayList, java.util.Date"%> <%@page import="java.sql.*"%> <html> <body> hello JSP </body> </html> Advanced Web Programming
JSP Directive 교재 51-57 • Page Directive 예제 – 변환된 Java 파일 package org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; import java.util.ArrayList; import java.util.Date; import java.sql.*; public final class helloJSP_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent { public String getServletInfo() { return "hello JSP"; } . . . try { _jspxFactory = JspFactory.getDefaultFactory(); response.setContentType("text/html;charset=euc-kr"); pageContext = _jspxFactory.getPageContext(this, request, response, null, true, 8192, true); . . . D:\jakarta-tomcat-5.0.19\work\Catalina\localhost\2006777888\org\apache\jsp\helloJSP_jsp.java Advanced Web Programming
JSP Directive 교재 180 • Include Directive • 여러 JSP페이지에서 사용되는 공통적인 내용은 파일로 저장한 후에 필요한 JSP페이지에서 포함하여 사용할 수 있다. • 코드 재사용 및 유지보수 용이성 높임 • 종류 • include Directive tag • Page 198 • include directive에 의해 지정된 파일의 내용이 inlcude directive가 존재하는 위치에 삽입이 되고 그 결과로 생긴 Java 파일을 컴파일한다. • 일반적인 포함되는 파일의 확장자: jspf • 정적인 특징 (Tomcat은 동적으로 처리해줌) • include Action tag • Page 180 • 포함하는 jsp가 포함되는 jsp로 내부적으로 요청을 한다. • 포함되는 jsp의 실행결과가 요청한 JSP의 출력 버퍼에 포함된다. • 일반적인 포함되는 파일의 확장자: jsp (또는 서블릿) • 어떠한 엔진에서도 동적인 특징을 제공한다. Advanced Web Programming
JSP Directive 교재 198~203 • Include Directive 예제 /2006777888/include.jsp /2006777888/incl.jspf <%@page contentType="text/html;charset=euc-kr"%> <html> <body> hello JSP, <%@include file="incl.jspf"%> </body> </html> <%! String subject = "Web Programming"; %> We are in the class, <%=subject%> Advanced Web Programming
JSP Directive 교재 198~203 • Include Directive 예제 – 변환된 Java 파일 package org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; public final class include_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent { String subject = "Web Programming"; private static java.util.Vector _jspx_dependants; . . public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException { . . out.write("\r\n"); out.write("<html>\r\n"); out.write("<body>\r\n"); out.write("hello JSP\r\n"); out.write("\r\n"); out.write("We are in the class, "); out.print(subject); out.write("\r\n"); out.write("</body>\r\n"); out.write("</html>"); . . . incl.jspf의 내용이 삽입이 되어 있음 Advanced Web Programming
JSP Directive 교재 181~198 • Include Action 예제 /2006777888/include2.jsp <%@page contentType="text/html;charset=euc-kr"%> <html> <body> hello JSP, <jsp:include page="incl.jsp"/> </body> </html> /2006777888/incl.jsp <%@page contentType="text/html;charset=euc-kr"%> <%! String subject = "Web Programming"; %> We are in the class, <%=subject%>!!! <br> Advanced Web Programming
JSP Directive 교재 181~198 • Include Action 예제 – 변환된 Java 파일 . . . out.write("\r\n"); out.write("<html>\r\n"); out.write("<body>\r\n"); out.write("hello JSP,\r\n"); org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response, "incl.jsp", out, false); out.write("\r\n"); out.write("</body>\r\n"); out.write("</html>"); . . . Advanced Web Programming
JSP Directive 교재 181~198 • Include Action에서 파라미터 사용 예제 • Include Directive에서는 파라미터 사용 못함 /2006777888/include2.jsp <%@page contentType="text/html;charset=euc-kr"%> <html> <body> hello JSP, <jsp:include page="incl.jsp"> <jsp:param name="year" value="sophomore"/> </jsp:include> </body> </html> /2006777888/incl.jsp <%@page contentType="text/html;charset=euc-kr"%> <%! String subject = "Web Programming"; %> <% String year = request.getParameter("year"); %> We are in the class, <%=subject%>!!! <br> This class is for <%=year%>. Advanced Web Programming
JSP Declaration (선언) 교재 61~65 • JSP Declaration • <%! … %> • 멤버변수 선언이나 메소드 선언에 사용된다. • 서블릿으로 변환될 때 서블릿의 멤버로 선언된다. /2006777888/decl.jsp <%@page contentType="text/html;charset=euc-kr"%> <%! String name = "JSP"; %> <html> <body> hello <%= name %> </body> </html> D:\jakarta-tomcat-5.0.19\work\Catalina\localhost\2006777888\org\apache\jsp\decl_jsp.java package org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; public final class decl_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent { String name = "JSP"; private static java.util.Vector _jspx_dependants; public java.util.List getDependants() { return _jspx_dependants; } public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException { Advanced Web Programming
JSP Scriptlet 교재 58~61 • JSP Scriptlet • <% … %> • 서블릿으로 변환될 때 변환된 서블릿의 Service Method 내부에 선언된다. • 일반적으로 가장 많이 사용되는 태그이다. • Scriptlet으로 선언된 변수는 메소드 내부의 로컬변수이다. • JSP Scriptlet 내부에서 코딩 • 일반적인 Java 코드 100%가 활용 가능하다. • JSP Expression • <%= … %> • 브라우저에 데이터를 출력할 때 사용한다. • 코드 끝 부분에 ;이 없어야 된다 Advanced Web Programming
JSP Scriptlet 교재 58~61 • JSP Scriptlet vs. JSP Declaration /2006777888/decl2.jsp <%@page contentType="text/html;charset=euc-kr"%> <% String name = "JSP"; %> <html> <body> hello <%= name %> </body> </html> D:\jakarta-tomcat-5.0.19\work\Catalina\localhost\2006777888\org\apache\jsp\decl2_jsp.java public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException { … … out.write('\r'); out.write('\n'); String name = "JSP"; out.write("\r\n"); out.write("<html>\r\n"); out.write("<body>\r\n"); out.write("hello "); out.print( name ); out.write("\r\n"); out.write("</body>\r\n"); out.write("</html>"); } catch (Throwable t) { … … Advanced Web Programming
내장 객체 교재 125~151 • 내장 객체 • JSP가 서블릿으로 변환될 때 자동으로 삽입시켜주는 변수를 의미한다. • 자동으로 생성이 되므로 선언이 필요 없다. • 종류 … public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException { JspFactory _jspxFactory = null; PageContextpageContext = null; HttpSession session = null; ServletContextapplication = null; ServletConfigconfig = null; JspWriterout = null; Object page = this; JspWriter _jspx_out = null; PageContext _jspx_page_context = null; … Advanced Web Programming
내장 객체 교재 125~151 • 내장 객체 • 종류 • Scope과 관련된 내장 객체 Advanced Web Programming
내장 객체 교재 125~151 • Scope과 관련된 내장 객체 Advanced Web Programming
내장 객체 교재 125~151 • request 내장 객체 /2006777888/param.html /2006777888/param.jsp <%@page contentType="text/html;charset=euc-kr"%> <html> <body> <% String name=request.getParameter("name"); String age=request.getParameter("age"); request.setAttribute("name2", name + "-" + age); %> <jsp:forward page="to.jsp"/> </body> </html> <html> <body> <form method="post" action="param.jsp"> 이름 <input type="text" name="name"> 나이 <input type="text" name="age"> <input type="submit"> </form> </body> </html> /2006777888/to.jsp <% String name2 = (String)request.getAttribute("name2"); %> Hello, <%= name2 %> Advanced Web Programming
내장 객체 교재 125~151 • session 내장 객체 Advanced Web Programming
내장 객체 교재 125~151 • application 내장 객체 (1/2) /2006777888/WEB-INF/web.xml Tomcat 재 시작 필요 Advanced Web Programming
내장 객체 교재 125~151 • application 내장 객체 (2/2) Advanced Web Programming
내장 객체 교재 125~151 • out 내장 객체 Advanced Web Programming