-
JSP 페이지 스크립트 요소jsp(Model1) 2020. 5. 10. 18:18
●스크립트 릿 <% %>
-스크립트 릿은 가장 일반적으로 JSP페이지에서 많이 쓰이는 스크립트요소로 주로 프로그래밍의 로직을 기술할 때 많이 쓰인다.
-스크립트 릿에서 선언한 변수는 JSP 페이지가 서블릿으로 변환될 때 지역변수로 사용된다.
▶문법
<%
자바 코드 삽입
%>
●선언문(declaration) <%! %>
-선언문은 일반적으로 JSP페이지에서 자바의 멤버변수(전역변수) 또는 멤버 메소드로 사용하고자 할 때 주로 사용되는 프로그래밍의 로직 기술이다.
-선언문의 변수는 서블릿으로 변환될 때 멤버변수로 변환되고 선언문에서의 메소드는 서블릿에서 메소드로 만들어 진다.
▶문법
<%!
자바 코드 삽입
%>
●표현식(Expression) <%= %>
-표현식은 일반적으로 JSP페이지에서 자바의 System.out.println()과 유사하게 사용된다. 데이터 출력을 할 때 주로 사용되는 프로그래밍의 로직 기술이다.
-System.out.println()은 콘솔에 표시할 때 쓰지만 표현식은 브라우저에 표현할 때 쓴다(화면 출력).
▶문법
<%=
자바 코드 삽입
%>
*jsp코드를 만들면 서블릿 코드도 자동으로 만들어 진다. (JVM에 의해.워크스페이스의 .metadata에서 검색해보면 나온다.확장자가 Count10_jsp.java로 나온다.)
Count10.jsp의 서블릿으로 바뀐 코드(Count10_jsp.java)
/* * Generated by the Jasper component of Apache Tomcat * Version: Apache Tomcat/9.0.34 * Generated at: 2020-05-10 08:39:17 UTC * Note: The last modified time of this file was set to * the last modified time of the source file after * generation to assist with modification tracking. */ package org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; public final class Count10_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent, org.apache.jasper.runtime.JspSourceImports { private static final javax.servlet.jsp.JspFactory _jspxFactory = javax.servlet.jsp.JspFactory.getDefaultFactory(); private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants; private static final java.util.Set<java.lang.String> _jspx_imports_packages; private static final java.util.Set<java.lang.String> _jspx_imports_classes; static { _jspx_imports_packages = new java.util.HashSet<>(); _jspx_imports_packages.add("javax.servlet"); _jspx_imports_packages.add("javax.servlet.http"); _jspx_imports_packages.add("javax.servlet.jsp"); _jspx_imports_classes = null; } private volatile javax.el.ExpressionFactory _el_expressionfactory; private volatile org.apache.tomcat.InstanceManager _jsp_instancemanager; public java.util.Map<java.lang.String,java.lang.Long> getDependants() { return _jspx_dependants; } public java.util.Set<java.lang.String> getPackageImports() { return _jspx_imports_packages; } public java.util.Set<java.lang.String> getClassImports() { return _jspx_imports_classes; } public javax.el.ExpressionFactory _jsp_getExpressionFactory() { if (_el_expressionfactory == null) { synchronized (this) { if (_el_expressionfactory == null) { _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory(); } } } return _el_expressionfactory; } public org.apache.tomcat.InstanceManager _jsp_getInstanceManager() { if (_jsp_instancemanager == null) { synchronized (this) { if (_jsp_instancemanager == null) { _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig()); } } } return _jsp_instancemanager; } public void _jspInit() { //제일먼저 jsp를 실행하게 되면 init메서드가 실행이 된다. 여기서는 아무 내용이 없기 때문에 아무것도 안함. } public void _jspDestroy() { } //init이 실행되고 난 후 Service가 실행이 된다. 그리고 난 후 jspService를 더이상 사용하지 않는다고 하면 Destroy메서드가 실행되고 끝난다. public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException { if (!javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) { final java.lang.String _jspx_method = request.getMethod(); if ("OPTIONS".equals(_jspx_method)) { response.setHeader("Allow","GET, HEAD, POST, OPTIONS"); return; } if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method)) { response.setHeader("Allow","GET, HEAD, POST, OPTIONS"); response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSP�뱾�� �삤吏� GET, POST �삉�뒗 HEAD 硫붿냼�뱶留뚯쓣 �뿀�슜�빀�땲�떎. Jasper�뒗 OPTIONS 硫붿냼�뱶 �삉�븳 �뿀�슜�빀�땲�떎."); return; } } final javax.servlet.jsp.PageContext pageContext; javax.servlet.http.HttpSession session = null; final javax.servlet.ServletContext application; final javax.servlet.ServletConfig config; javax.servlet.jsp.JspWriter out = null; final java.lang.Object page = this; javax.servlet.jsp.JspWriter _jspx_out = null; javax.servlet.jsp.PageContext _jspx_page_context = null; try { response.setContentType("text/html; charset=EUC-KR"); 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("\r\n"); out.write("<!DOCTYPE html>\r\n"); out.write("<html>\r\n"); out.write("<head>\r\n"); out.write("<meta charset=\"EUC-KR\">\r\n"); out.write("<title>Insert title here</title>\r\n"); out.write("</head>\r\n"); out.write("<body>\r\n"); out.write("\r\n"); out.write("<h2>1~10源뚯��쓽 �닽�옄瑜� �솕硫댁뿉 �몴�떆</h2>\r\n"); out.write("\r\n"); //1~10源뚯��쓽 �닽�옄瑜� �솕硫댁뿉 異쒕젰�븯�떆�삤. for(int i = 1; i<=10; i++){ System.out.println(i); //�씠嫄� 肄섏넄�뿉 �몴�떆�븯�뒗 寃� out.write('\r'); out.write('\n'); out.write(' '); out.print( i ); out.write("<br>\r\n"); } out.write("\r\n"); out.write("\r\n"); out.write("\r\n"); out.write("\r\n"); out.write("<h2>援ш뎄�떒</h2>\r\n"); for(int i = 2; i<= 9; i++){ for(int j = 1; j<=9; j++){ System.out.print(i+ "*"+ j + "= "+i*j + " "); out.write("\r\n"); out.write("\r\n"); out.print( i + "*" + j + "= " + i*j ); out.write(" \r\n"); } System.out.println(); out.write("\r\n"); out.write("<br>\r\n"); } out.write("\r\n"); out.write("\r\n"); out.write("</body>\r\n"); out.write("</html>"); } catch (java.lang.Throwable t) { if (!(t instanceof javax.servlet.jsp.SkipPageException)){ out = _jspx_out; if (out != null && out.getBufferSize() != 0) try { if (response.isCommitted()) { out.flush(); } else { out.clearBuffer(); } } catch (java.io.IOException e) {} if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); else throw new ServletException(t); } } finally { _jspxFactory.releasePageContext(_jspx_page_context); } } }
▶선언문을 사용해야 할 때
>선언문을 사용하지 않고 스크립트 릿 안에 메서드를 만들면
-선언문을 사용하지 않고 스크립트 릿안에 메서드를 만든 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> <%-- <% public void inCrement(){ } %> 스크립트 릿 안에 소스코드를 적어주면 서블릿으로 변환 될 때 _jspService메서드 안에 만들어 지게 되는데 _jspService메서드안에 메서드를 또 만드는 것이기 때문에 오류가 발생하게 된다. 그래서 선언문으로 만들어 줘야한다.(선언문으로 메서드를 만들면 멤버메서드로 만들어 지게 된다.) --%> </body> </html>
-선언문을 사용하지 않고 스크립트 릿안에 메서드를 만든 jsp가 변환된 서블릿
/* * Generated by the Jasper component of Apache Tomcat * Version: Apache Tomcat/9.0.34 * Generated at: 2020-05-10 08:57:51 UTC * Note: The last modified time of this file was set to * the last modified time of the source file after * generation to assist with modification tracking. */ package org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; public final class JSPBank_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent, org.apache.jasper.runtime.JspSourceImports { private static final javax.servlet.jsp.JspFactory _jspxFactory = javax.servlet.jsp.JspFactory.getDefaultFactory(); private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants; private static final java.util.Set<java.lang.String> _jspx_imports_packages; private static final java.util.Set<java.lang.String> _jspx_imports_classes; static { _jspx_imports_packages = new java.util.HashSet<>(); _jspx_imports_packages.add("javax.servlet"); _jspx_imports_packages.add("javax.servlet.http"); _jspx_imports_packages.add("javax.servlet.jsp"); _jspx_imports_classes = null; } private volatile javax.el.ExpressionFactory _el_expressionfactory; private volatile org.apache.tomcat.InstanceManager _jsp_instancemanager; public java.util.Map<java.lang.String,java.lang.Long> getDependants() { return _jspx_dependants; } public java.util.Set<java.lang.String> getPackageImports() { return _jspx_imports_packages; } public java.util.Set<java.lang.String> getClassImports() { return _jspx_imports_classes; } public javax.el.ExpressionFactory _jsp_getExpressionFactory() { if (_el_expressionfactory == null) { synchronized (this) { if (_el_expressionfactory == null) { _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory(); } } } return _el_expressionfactory; } public org.apache.tomcat.InstanceManager _jsp_getInstanceManager() { if (_jsp_instancemanager == null) { synchronized (this) { if (_jsp_instancemanager == null) { _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig()); } } } return _jsp_instancemanager; } public void _jspInit() { } public void _jspDestroy() { } public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException { if (!javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) { final java.lang.String _jspx_method = request.getMethod(); if ("OPTIONS".equals(_jspx_method)) { response.setHeader("Allow","GET, HEAD, POST, OPTIONS"); return; } if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method)) { response.setHeader("Allow","GET, HEAD, POST, OPTIONS"); response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSP�뱾�� �삤吏� GET, POST �삉�뒗 HEAD 硫붿냼�뱶留뚯쓣 �뿀�슜�빀�땲�떎. Jasper�뒗 OPTIONS 硫붿냼�뱶 �삉�븳 �뿀�슜�빀�땲�떎."); return; } } final javax.servlet.jsp.PageContext pageContext; javax.servlet.http.HttpSession session = null; final javax.servlet.ServletContext application; final javax.servlet.ServletConfig config; javax.servlet.jsp.JspWriter out = null; final java.lang.Object page = this; javax.servlet.jsp.JspWriter _jspx_out = null; javax.servlet.jsp.PageContext _jspx_page_context = null; try { response.setContentType("text/html; charset=EUC-KR"); 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("\r\n"); out.write("<!DOCTYPE html>\r\n"); out.write("<html>\r\n"); out.write("<head>\r\n"); out.write("<meta charset=\"EUC-KR\">\r\n"); out.write("<title>Insert title here</title>\r\n"); out.write("</head>\r\n"); out.write("<body>\r\n"); out.write("\r\n"); public void inCrement(){ //이부분!!! 메서드 안에 메서드가 또 만들어지게 된다. } out.write("\r\n"); out.write("\r\n"); out.write("</body>\r\n"); out.write("</html>"); } catch (java.lang.Throwable t) { if (!(t instanceof javax.servlet.jsp.SkipPageException)){ out = _jspx_out; if (out != null && out.getBufferSize() != 0) try { if (response.isCommitted()) { out.flush(); } else { out.clearBuffer(); } } catch (java.io.IOException e) {} if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); else throw new ServletException(t); } } finally { _jspxFactory.releasePageContext(_jspx_page_context); } } }
-선언문을 사용하여 메서드를 만든 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> <%! public void increment(){ } %> </body> </html>
-선언문을 사용하여 메서드를 만든 jsp가 변환된 서블릿
/* * Generated by the Jasper component of Apache Tomcat * Version: Apache Tomcat/9.0.34 * Generated at: 2020-05-10 09:07:28 UTC * Note: The last modified time of this file was set to * the last modified time of the source file after * generation to assist with modification tracking. */ package org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; public final class JSPBank_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent, org.apache.jasper.runtime.JspSourceImports { public void increment(){ //이부분!!! 메서드 바깥에 멤버메서드(전역메서드)로 //메서드가 만들어 지게 된다. } private static final javax.servlet.jsp.JspFactory _jspxFactory = javax.servlet.jsp.JspFactory.getDefaultFactory(); private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants; private static final java.util.Set<java.lang.String> _jspx_imports_packages; private static final java.util.Set<java.lang.String> _jspx_imports_classes; static { _jspx_imports_packages = new java.util.HashSet<>(); _jspx_imports_packages.add("javax.servlet"); _jspx_imports_packages.add("javax.servlet.http"); _jspx_imports_packages.add("javax.servlet.jsp"); _jspx_imports_classes = null; } private volatile javax.el.ExpressionFactory _el_expressionfactory; private volatile org.apache.tomcat.InstanceManager _jsp_instancemanager; public java.util.Map<java.lang.String,java.lang.Long> getDependants() { return _jspx_dependants; } public java.util.Set<java.lang.String> getPackageImports() { return _jspx_imports_packages; } public java.util.Set<java.lang.String> getClassImports() { return _jspx_imports_classes; } public javax.el.ExpressionFactory _jsp_getExpressionFactory() { if (_el_expressionfactory == null) { synchronized (this) { if (_el_expressionfactory == null) { _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory(); } } } return _el_expressionfactory; } public org.apache.tomcat.InstanceManager _jsp_getInstanceManager() { if (_jsp_instancemanager == null) { synchronized (this) { if (_jsp_instancemanager == null) { _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig()); } } } return _jsp_instancemanager; } public void _jspInit() { } public void _jspDestroy() { } public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException { if (!javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) { final java.lang.String _jspx_method = request.getMethod(); if ("OPTIONS".equals(_jspx_method)) { response.setHeader("Allow","GET, HEAD, POST, OPTIONS"); return; } if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method)) { response.setHeader("Allow","GET, HEAD, POST, OPTIONS"); response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSP�뱾�� �삤吏� GET, POST �삉�뒗 HEAD 硫붿냼�뱶留뚯쓣 �뿀�슜�빀�땲�떎. Jasper�뒗 OPTIONS 硫붿냼�뱶 �삉�븳 �뿀�슜�빀�땲�떎."); return; } } final javax.servlet.jsp.PageContext pageContext; javax.servlet.http.HttpSession session = null; final javax.servlet.ServletContext application; final javax.servlet.ServletConfig config; javax.servlet.jsp.JspWriter out = null; final java.lang.Object page = this; javax.servlet.jsp.JspWriter _jspx_out = null; javax.servlet.jsp.PageContext _jspx_page_context = null; try { response.setContentType("text/html; charset=EUC-KR"); 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("\r\n"); out.write("<!DOCTYPE html>\r\n"); out.write("<html>\r\n"); out.write("<head>\r\n"); out.write("<meta charset=\"EUC-KR\">\r\n"); out.write("<title>Insert title here</title>\r\n"); out.write("</head>\r\n"); out.write("<body>\r\n"); out.write("\r\n"); out.write("\r\n"); out.write("\r\n"); out.write("</body>\r\n"); out.write("</html>"); } catch (java.lang.Throwable t) { if (!(t instanceof javax.servlet.jsp.SkipPageException)){ out = _jspx_out; if (out != null && out.getBufferSize() != 0) try { if (response.isCommitted()) { out.flush(); } else { out.clearBuffer(); } } catch (java.io.IOException e) {} if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); else throw new ServletException(t); } } finally { _jspxFactory.releasePageContext(_jspx_page_context); } } }
그냥 스크립트 릿<% %>안에 변수와 메서드를 만들면 지역변수, 지역메서드로 만들어지고
선언문 <%! %>안에 변수와 메서드를 만들면 멤버변수(전역변수), 멤버메서드(전역메서드)로 만들어 지게 된다.
선언문은 개념정도만 알아두면 좋다.
참조 : 인프런 - JSP웹 쇼핑몰 프로그래밍 기본 과정(JSP WEB Programming) - JSP 페이지 디렉티브
'jsp(Model1)' 카테고리의 다른 글
JSP와 데이터베이스연동2(DAO패턴 적용) (0) 2020.05.17 JSP와 데이터베이스 연동1 (0) 2020.05.16 JSP 액션태그 (0) 2020.05.15 JSP 페이지 디렉티브 - Include (0) 2020.05.15 페이지 디렉티브 (0) 2020.05.10