곽로그
jsp 에서 <%!(선언문) 로 선언하는 것과 <% (스크립트릿)로 선언하는 것의 차이 본문
반응형
jsp로 계산기를 만든다고 할 때 코드는 아래와 같다.
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%!
private String calculte(int a, int b, String op){
int result = 0;
switch (op){
case "+":
result = a+b;
break;
case "-":
result = a-b;
break;
case "*" :
result = a*b;
break;
case "/":
result = a/b;
break;
}
return Integer.toString(result);
}
%>
<%
String v1 = "";
String v2 = "";
String result ="";
String[] selected ={"","","",""};
if(request.getParameter("v1")!=null){
v1 = request.getParameter("v1");
v2 = request.getParameter("v2");
String op = request.getParameter("op");
result = calculte(Integer.parseInt(v1), Integer.parseInt(v2), op);
}
%>
<html>
<head>
<title>Calculator</title>
</head>
<body>
<h2>계산기</h2>
<form action = 'Calculator.jsp' method="post">
<input type ="text" name="v1" size="4" value="<%=v1%>">
<select name ="op">
<option value="+" <%=selected[0]%>>+</option>
<option value="-" <%=selected[1]%>>+</option>
<option value="*" <%=selected[2]%>>+</option>
<option value="/" <%=selected[3]%>>+</option>
</select>
<input type ="text" name="v2" size="4" value="<%=v2%>">
<input type="submit" value="=">
<input type="text" size="8" value="<%=result%>"><br/>
</form>
</body>
</html>
이 jsp 파일의 class파일을 보면 <%로 선언하는 것과 <%!로 선언하는 것의 차이를 명확하게 볼 수 있다. class파일로 변환된 jsp 파일의 경로는 톰캣을 실행했을 때 나오는 CATALINA_BASE의 경로에서 \work\Catalina\localhost\webWorkBook\org\apache\jsp\패키지명 으로가면 class 파일로 변환된 jsp 파일을 볼 수 있다.
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package org.apache.jsp.calc;
import java.io.IOException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.el.ExpressionFactory;
import javax.servlet.DispatcherType;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.SkipPageException;
import org.apache.jasper.runtime.HttpJspBase;
import org.apache.jasper.runtime.InstanceManagerFactory;
import org.apache.jasper.runtime.JspSourceDependent;
import org.apache.jasper.runtime.JspSourceImports;
import org.apache.tomcat.InstanceManager;
public final class Calculator_jsp extends HttpJspBase implements JspSourceDependent, JspSourceImports {
private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();
private static Map<String, Long> _jspx_dependants;
private static final Set<String> _jspx_imports_packages = new HashSet();
private static final Set<String> _jspx_imports_classes;
private volatile ExpressionFactory _el_expressionfactory;
private volatile InstanceManager _jsp_instancemanager;
static {
_jspx_imports_packages.add("javax.servlet");
_jspx_imports_packages.add("javax.servlet.http");
_jspx_imports_packages.add("javax.servlet.jsp");
_jspx_imports_classes = null;
}
public Calculator_jsp() {
}
private String calculte(int a, int b, String op) {
int result = 0;
switch(op.hashCode()) {
case 42:
if (op.equals("*")) {
result = a * b;
}
break;
case 43:
if (op.equals("+")) {
result = a + b;
}
break;
case 45:
if (op.equals("-")) {
result = a - b;
}
break;
case 47:
if (op.equals("/")) {
result = a / b;
}
}
return Integer.toString(result);
}
public Map<String, Long> getDependants() {
return _jspx_dependants;
}
public Set<String> getPackageImports() {
return _jspx_imports_packages;
}
public Set<String> getClassImports() {
return _jspx_imports_classes;
}
public ExpressionFactory _jsp_getExpressionFactory() {
if (this._el_expressionfactory == null) {
synchronized(this) {
if (this._el_expressionfactory == null) {
this._el_expressionfactory = _jspxFactory.getJspApplicationContext(this.getServletConfig().getServletContext()).getExpressionFactory();
}
}
}
return this._el_expressionfactory;
}
public InstanceManager _jsp_getInstanceManager() {
if (this._jsp_instancemanager == null) {
synchronized(this) {
if (this._jsp_instancemanager == null) {
this._jsp_instancemanager = InstanceManagerFactory.getInstanceManager(this.getServletConfig());
}
}
}
return this._jsp_instancemanager;
}
public void _jspInit() {
}
public void _jspDestroy() {
}
public void _jspService(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
if (!DispatcherType.ERROR.equals(request.getDispatcherType())) {
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(405, "JSPs only permit GET, POST or HEAD. Jasper also permits OPTIONS");
return;
}
}
JspWriter out = null;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;
try {
response.setContentType("text/html;charset=UTF-8");
PageContext pageContext = _jspxFactory.getPageContext(this, request, response, (String)null, true, 8192, true);
_jspx_page_context = pageContext;
pageContext.getServletContext();
pageContext.getServletConfig();
pageContext.getSession();
out = pageContext.getOut();
out.write("\r\n");
out.write("\r\n");
out.write(13);
out.write(10);
String v1 = "";
String v2 = "";
String result = "";
String[] selected = new String[]{"", "", "", ""};
if (request.getParameter("v1") != null) {
v1 = request.getParameter("v1");
v2 = request.getParameter("v2");
String op = request.getParameter("op");
result = this.calculte(Integer.parseInt(v1), Integer.parseInt(v2), op);
}
out.write("\r\n");
out.write("<html>\r\n");
out.write(" <head>\r\n");
out.write(" <title>Calculator</title>\r\n");
out.write(" </head>\r\n");
out.write(" <body>\r\n");
out.write(" <h2>계산기</h2>\r\n");
out.write(" <form action = 'Calculator.jsp' method=\"post\">\r\n");
out.write(" <input type =\"text\" name=\"v1\" size=\"4\" value=\"");
out.print(v1);
out.write("\">\r\n");
out.write(" <select name =\"op\">\r\n");
out.write(" <option value=\"+\" ");
out.print(selected[0]);
out.write(">+</option>\r\n");
out.write(" <option value=\"-\" ");
out.print(selected[1]);
out.write(">+</option>\r\n");
out.write(" <option value=\"*\" ");
out.print(selected[2]);
out.write(">+</option>\r\n");
out.write(" <option value=\"/\" ");
out.print(selected[3]);
out.write(">+</option>\r\n");
out.write(" </select>\r\n");
out.write(" <input type =\"text\" name=\"v2\" size=\"4\" value=\"");
out.print(v2);
out.write("\">\r\n");
out.write(" <input type=\"submit\" value=\"=\">\r\n");
out.write(" <input type=\"text\" size=\"8\" value=\"");
out.print(result);
out.write("\"><br/>\r\n");
out.write("\r\n");
out.write(" </form>\r\n");
out.write(" </body>\r\n");
out.write("</html>\r\n");
} catch (Throwable var17) {
if (!(var17 instanceof SkipPageException)) {
out = (JspWriter)_jspx_out;
if (_jspx_out != null && ((JspWriter)_jspx_out).getBufferSize() != 0) {
try {
if (response.isCommitted()) {
out.flush();
} else {
out.clearBuffer();
}
} catch (IOException var16) {
}
}
if (_jspx_page_context == null) {
throw new ServletException(var17);
}
_jspx_page_context.handlePageException(var17);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
}
클래스 파일을 보면 <%!으로 선언된 calculate 함수는 Calculator_jsp클래스의멤버함수로, <%으로 선언된 코드는 Calculator_jsp클래스의 _jspService메서드 안에 선언되어 있는 것을 확인할 수 있다.
반응형
'WEB > JSP' 카테고리의 다른 글
인텔리제이에서 톰캣 실행 시 jsp에서 서블릿으로 변환된 java파일 경로 (0) | 2021.01.19 |
---|
Comments