티스토리 뷰
JSP란?
JavaServer Pages
One of the original Java web technologies, JavaServer Pages is still widely used to build dynamic web pages that connect to the Java backend
참고: www.infoworld.com/article/3336161/what-is-jsp-introduction-to-javaserver-pages.html
JSP와 Servlet은 차이점
JSP:
HTML 코드 안에 JAVA 코드
-> 코드 읽고 쓰기 편리
Servlet:
JAVA 코드 안에 HTML
-> 코드 읽고 쓰기 불편
JSP사용 방법 및 실행과정
HTML문서를 JSP라는 이름으로 만들면 된다.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<Style>
input{
width:50px;
height:50px;
}
.output{
height:50px;
backgroud: #e9e9e9;
font-size:24px;
font-width:bold;
text-align:right;
padding:0px 5px;
}
</Style>
</head>
<body>
<form action="calc3" method="post">
<table>
<tr>
<td class="output" colspan="4">7+5</td>
</tr>
<tr>
<td><input type="submit" name="operator" value="CE"></td>
<td><input type="submit" name="operator" value="C"></td>
<td><input type="submit" name="operator" value="BS"></td>
<td><input type="submit" name="operator" value="/"></td>
</tr>
<tr>
<td><input type="submit" name="number" value="7"></td>
<td><input type="submit" name="number" value="8"></td>
<td><input type="submit" name="number" value="9"></td>
<td><input type="submit" name="operator" value="*"></td>
</tr>
<tr>
<td><input type="submit" name="number" value="4"></td>
<td><input type="submit" name="number" value="5"></td>
<td><input type="submit" name="number" value="6"></td>
<td><input type="submit" name="operator" value="-"></td>
</tr>
<tr>
<td><input type="submit" name="number" value="1"></td>
<td><input type="submit" name="number" value="2"></td>
<td><input type="submit" name="number" value="3"></td>
<td><input type="submit" name="operator" value="+"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="number" value="0"></td>
<td><input type="submit" name="dot" value="."></td>
<td><input type="submit" name="operator" value="="></td>
</tr>
</table>
</form>
</body>
</html>
이전에 만들었던 calc3.html 계산기 프로그램이다. 그대로 복사해서 calculator.jsp로 만들었다.
JSP가 알아서 다 해준다.
보면 7+5라고 출력이 되고있다. 그렇다면 JSP의 진가를 보기위해서 코드를 조금 수정해보자.
위의 코드 중 7+5 출력 부분을 ${7+5}로 수정(코드 사용법은 조금있다 보겠다.)
<td class="output" colspan="4">${7+5}</td>
JSP내에 자바코드가 삽입되고 문자열 7+5가 아닌 계산된 12가 출력된다. 대단하다!
순서: JSP를 실행 -> JSP가 서블릿 생성 -> 컴파일해서 CLASS파일을 만들어 결과 -> 웹에 반환
그렇다면 자바파일이랑은 다 어디있지??
우리가 보는 이 화면의 src는 실행파일들이 있는 곳이 아니라 개발자가 보는 코드들이 있는 곳이다.
그렇다면 실제 JSP에서 생성된 실행 파일들은 어디에 있을까?
경로를 보면 이렇게 파일이 생성되어 있는 것을 확인 할 수있다.
/*
* Generated by the Jasper component of Apache Tomcat
* Version: Apache Tomcat/9.0.45
* Generated at: 2021-04-26 05:39:18 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 calculator_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");
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=\"UTF-8\">\r\n");
out.write("<title>Insert title here</title>\r\n");
out.write("<Style>\r\n");
out.write("input{\r\n");
out.write("\twidth:50px;\r\n");
out.write("\theight:50px;\r\n");
out.write("}\r\n");
out.write(".output{\r\n");
out.write("\theight:50px;\r\n");
out.write("\tbackgroud: #e9e9e9;\r\n");
out.write("\tfont-size:24px;\r\n");
out.write("\tfont-width:bold;\r\n");
out.write("\ttext-align:right;\r\n");
out.write("\tpadding:0px 5px;\r\n");
out.write("}\r\n");
out.write("\r\n");
out.write("\t\r\n");
out.write("</Style>\r\n");
out.write("\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.write("\t<form action=\"calc3\" method=\"post\">\r\n");
out.write("\t\t<table>\r\n");
out.write("\t\t\t<tr>\r\n");
out.write("\t\t\t\t<td class=\"output\" colspan=\"4\">");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${5+7}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
out.write("</td>\r\n");
out.write("\t\t\t</tr>\r\n");
out.write("\t\t\t<tr>\r\n");
out.write("\t\t\t\t<td><input type=\"submit\" name=\"operator\" value=\"CE\"></td>\r\n");
out.write("\t\t\t\t<td><input type=\"submit\" name=\"operator\" value=\"C\"></td>\r\n");
out.write("\t\t\t\t<td><input type=\"submit\" name=\"operator\" value=\"BS\"></td>\r\n");
out.write("\t\t\t\t<td><input type=\"submit\" name=\"operator\" value=\"/\"></td>\r\n");
out.write("\t\t\t</tr>\r\n");
out.write("\t\t\t<tr>\r\n");
out.write("\t\t\t\t<td><input type=\"submit\" name=\"number\" value=\"7\"></td>\r\n");
out.write("\t\t\t\t<td><input type=\"submit\" name=\"number\" value=\"8\"></td>\r\n");
out.write("\t\t\t\t<td><input type=\"submit\" name=\"number\" value=\"9\"></td>\r\n");
out.write("\t\t\t\t<td><input type=\"submit\" name=\"operator\" value=\"*\"></td>\r\n");
out.write("\t\t\t</tr>\r\n");
out.write("\t\t\t<tr>\r\n");
out.write("\t\t\t\t<td><input type=\"submit\" name=\"number\" value=\"4\"></td>\r\n");
out.write("\t\t\t\t<td><input type=\"submit\" name=\"number\" value=\"5\"></td>\r\n");
out.write("\t\t\t\t<td><input type=\"submit\" name=\"number\" value=\"6\"></td>\r\n");
out.write("\t\t\t\t<td><input type=\"submit\" name=\"operator\" value=\"-\"></td>\r\n");
out.write("\t\t\t</tr>\r\n");
out.write("\t\t\t<tr>\r\n");
out.write("\t\t\t\t<td><input type=\"submit\" name=\"number\" value=\"1\"></td>\r\n");
out.write("\t\t\t\t<td><input type=\"submit\" name=\"number\" value=\"2\"></td>\r\n");
out.write("\t\t\t\t<td><input type=\"submit\" name=\"number\" value=\"3\"></td>\r\n");
out.write("\t\t\t\t<td><input type=\"submit\" name=\"operator\" value=\"+\"></td>\r\n");
out.write("\t\t\t</tr>\r\n");
out.write("\t\t\t<tr>\r\n");
out.write("\t\t\t\t<td></td>\r\n");
out.write("\t\t\t\t<td><input type=\"submit\" name=\"number\" value=\"0\"></td>\r\n");
out.write("\t\t\t\t<td><input type=\"submit\" name=\"dot\" value=\".\"></td>\r\n");
out.write("\t\t\t\t<td><input type=\"submit\" name=\"operator\" value=\"=\"></td>\r\n");
out.write("\t\t\t</tr>\r\n");
out.write("\t\t\r\n");
out.write("\t\t\r\n");
out.write("\t\t</table>\r\n");
out.write("\t</form>\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가 만든 결과물은 절때 만지지 말아야한다.
그렇다면 이제 JSP내에서 코드 작성 방법을 알아보자.
JSP내에서 변수를 선언하고 싶다면 어떻게 해야할까?
int x =3; int y = 4; 한번 선언해보자
int x = 3;
int y = 4;
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<Style>
input{
width:50px;
height:50px;
}
.output{
height:50px;
backgroud: #e9e9e9;
font-size:24px;
font-width:bold;
text-align:right;
padding:0px 5px;
}
</Style>
</head>
<body>
<form action="calc3" method="post">
<table>
<tr>
<td class="output" colspan="4">${5+7}</td>
</tr>
<tr>
<td><input type="submit" name="operator" value="CE"></td>
<td><input type="submit" name="operator" value="C"></td>
<td><input type="submit" name="operator" value="BS"></td>
<td><input type="submit" name="operator" value="/"></td>
</tr>
<tr>
<td><input type="submit" name="number" value="7"></td>
<td><input type="submit" name="number" value="8"></td>
<td><input type="submit" name="number" value="9"></td>
<td><input type="submit" name="operator" value="*"></td>
</tr>
<tr>
<td><input type="submit" name="number" value="4"></td>
<td><input type="submit" name="number" value="5"></td>
<td><input type="submit" name="number" value="6"></td>
<td><input type="submit" name="operator" value="-"></td>
</tr>
<tr>
<td><input type="submit" name="number" value="1"></td>
<td><input type="submit" name="number" value="2"></td>
<td><input type="submit" name="number" value="3"></td>
<td><input type="submit" name="operator" value="+"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="number" value="0"></td>
<td><input type="submit" name="dot" value="."></td>
<td><input type="submit" name="operator" value="="></td>
</tr>
</table>
</form>
</body>
</html>
문자열 그대로 출력된다.
JSP가 만든 코드를 보면 그냥 문자열로 출력해준다.
그렇다면 이제 이 부분은 어떻게 해결해야 할지 다음 글에서 알아보자.
'Servlet JSP' 카테고리의 다른 글
JSP 내장객체 (0) | 2021.04.26 |
---|---|
JSP 코드 블록 (0) | 2021.04.26 |
서블릿으로 합치기(계산기 진짜 완성) (0) | 2021.04.26 |
GET, POST에 특화된 서비스 함수 (0) | 2021.04.26 |
클리어(쿠키삭제)추가_계산기 만들기(완성2) (0) | 2021.04.26 |