728x90
JSP
JSP란 Java Server Pages의 약자이며 HTML 코드에 Java 코드를 넣어 동적 웹페이지를 생성하는 웹 어플리케이션 도구입니다. JSP가 실행되면 자바 서블릿(Servlet)으로 변환되며 웹 어플리케이션 서버에서 동작되면서 필요한 기능을 수행하고 그렇게 생성된 데이터를 웹페이지와 함께 클라이언트로 응답합니다.
JSP 동작 과정
- 브라우저가 웹 서버에게 JSP에 대한 요청 정보를 전달한다.
- 브라우저가 요청한 JSP가 최초로 요청했을 경우에만 JSP로 작성된 코드가 서블릿 코드로 변환된다.(.java파일 생성)
- 서블릿 코드를 컴파일하여 실행가능한 bytecode로 변환한다.(.class파일 생성)
- 서블릿이 실행되어 요청을 처리하고 응답 정보를 생성한다.
D:\PJH\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\project\org\apache\jsp
.java 파일이 컴파일된 servlet 파일 경로
/*
* Generated by the Jasper component of Apache Tomcat
* Version: Apache Tomcat/9.0.76
* Generated at: 2023-08-09 01:24:53 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 teaList_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;
static {
_jspx_dependants = new java.util.HashMap<java.lang.String,java.lang.Long>(7);
_jspx_dependants.put("/WEB-INF/lib/taglibs-standard-impl-1.2.5.jar", Long.valueOf(1690367818619L));
_jspx_dependants.put("/WEB-INF/tags/favicon.tag", Long.valueOf(1690367818622L));
_jspx_dependants.put("/WEB-INF/tags/Head.tag", Long.valueOf(1690367818623L));
_jspx_dependants.put("jar:file:/D:/PJH/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/varchar_semi_ver9.1/WEB-INF/lib/taglibs-standard-impl-1.2.5.jar!/META-INF/c.tld", Long.valueOf(1425946270000L));
_jspx_dependants.put("/WEB-INF/tags/Humberger.tag", Long.valueOf(1690367818625L));
_jspx_dependants.put("/WEB-INF/tags/Hero.tag", Long.valueOf(1690367818624L));
_jspx_dependants.put("/WEB-INF/tags/Footer.tag", Long.valueOf(1690367818622L));
}
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 org.apache.jasper.runtime.TagHandlerPool _005fjspx_005ftagPool_005fc_005fif_0026_005ftest;
private org.apache.jasper.runtime.TagHandlerPool _005fjspx_005ftagPool_005fc_005fforEach_0026_005fvar_005fitems;
private org.apache.jasper.runtime.TagHandlerPool _005fjspx_005ftagPool_005fc_005fforEach_0026_005fvar_005fend_005fbegin;
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() {
_005fjspx_005ftagPool_005fc_005fif_0026_005ftest = org.apache.jasper.runtime.TagHandlerPool.getTagHandlerPool(getServletConfig());
_005fjspx_005ftagPool_005fc_005fforEach_0026_005fvar_005fitems = org.apache.jasper.runtime.TagHandlerPool.getTagHandlerPool(getServletConfig());
_005fjspx_005ftagPool_005fc_005fforEach_0026_005fvar_005fend_005fbegin = org.apache.jasper.runtime.TagHandlerPool.getTagHandlerPool(getServletConfig());
}
public void _jspDestroy() {
_005fjspx_005ftagPool_005fc_005fif_0026_005ftest.release();
_005fjspx_005ftagPool_005fc_005fforEach_0026_005fvar_005fitems.release();
_005fjspx_005ftagPool_005fc_005fforEach_0026_005fvar_005fend_005fbegin.release();
}
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=UTF-8");
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("\r\n");
out.write("\r\n");
out.write("<!DOCTYPE html>\r\n");
out.write("<html lang=\"zxx\">\r\n");
out.write("\r\n");
out.write("<head>\r\n");
out.write("\r\n");
out.write("<meta charset=\"UTF-8\">\r\n");
out.write("<meta name=\"description\" content=\"Ogani Template\">\r\n");
out.write("<meta name=\"keywords\" content=\"Ogani, unica, creative, html\">\r\n");
out.write("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\r\n");
out.write("<meta http-equiv=\"X-UA-Compatible\" content=\"ie=edge\">\r\n");
out.write("<link rel=\"stylesheet\" href=\"https://cdn.jsdelivr.net/npm/sweetalert2@11.4.10/dist/sweetalert2.min.css\">\r\n");
out.write("<script src=\"https://cdn.jsdelivr.net/npm/sweetalert2@11.4.10/dist/sweetalert2.min.js\"></script>\r\n");
if (_jspx_meth_try_005ffavicon_005f0(_jspx_page_context))
return;
out.write("\r\n");
out.write("<title>상품 목록 페이지</title>\r\n");
out.write("\r\n");
out.write("<!-- Google Font -->\r\n");
out.write("<link\r\n");
out.write(" href=\"https://fonts.googleapis.com/css2?family=Cairo:wght@200;300;400;600;900&display=swap\"\r\n");
out.write(" rel=\"stylesheet\">\r\n");
out.write("\r\n");
out.write("<!-- Css Styles -->\r\n");
out.write("<link rel=\"stylesheet\" href=\"css/bootstrap.min.css\" type=\"text/css\">\r\n");
out.write("<link rel=\"stylesheet\" href=\"css/font-awesome.min.css\" type=\"text/css\">\r\n");
out.write("<link rel=\"stylesheet\" href=\"css/elegant-icons.css\" type=\"text/css\">\r\n");
out.write("<link rel=\"stylesheet\" href=\"css/nice-select.css\" type=\"text/css\">\r\n");
out.write("<link rel=\"stylesheet\" href=\"css/jquery-ui.min.css\" type=\"text/css\">\r\n");
out.write("<link rel=\"stylesheet\" href=\"css/owl.carousel.min.css\" type=\"text/css\">\r\n");
out.write("<link rel=\"stylesheet\" href=\"css/slicknav.min.css\" type=\"text/css\">\r\n");
out.write("<link rel=\"stylesheet\" href=\"css/style.css\" type=\"text/css\">\r\n");
out.write("\r\n");
out.write("<style>\r\n");
out.write("#liFavor{\r\n");
out.write(" cursor:pointer;\r\n");
out.write("}\r\n");
out.write("\r\n");
out.write("</style>\r\n");
out.write("\r\n");
out.write("</head>\r\n");
out.write("\r\n");
out.write("<body>\r\n");
out.write("\r\n");
out.write(" <!-- Page Preloder -->\r\n");
out.write(" <div id=\"preloder\">\r\n");
out.write(" <div class=\"loader\"></div>\r\n");
out.write(" </div>\r\n");
out.write("\r\n");
out.write(" <!-- Humberger Begin -->\r\n");
out.write(" ");
if (_jspx_meth_try_005fHumberger_005f0(_jspx_page_context))
return;
out.write("\r\n");
out.write(" <!-- Humberger End -->\r\n");
out.write("\r\n");
out.write(" <!-- Header Section Begin -->\r\n");
out.write(" ");
if (_jspx_meth_try_005fHead_005f0(_jspx_page_context))
return;
out.write("\r\n");
out.write(" <!-- Header Section End -->\r\n");
out.write("\r\n");
out.write(" <!-- Hero Section Begin -->\r\n");
out.write(" ");
if (_jspx_meth_try_005fHero_005f0(_jspx_page_context))
return;
out.write("\r\n");
out.write(" <!-- Hero Section End -->\r\n");
out.write("\r\n");
out.write(" <!-- Breadcrumb Section Begin -->\r\n");
out.write(" <section class=\"breadcrumb-section set-bg\"\r\n");
out.write(" data-setbg=\"https://images.unsplash.com/photo-1594135549290-f3a299aee716?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1074&q=80\">\r\n");
out.write(" <div class=\"container\">\r\n");
out.write(" <div class=\"row\">\r\n");
out.write(" <div class=\"col-lg-12 text-center\">\r\n");
out.write(" <div class=\"breadcrumb__text\">\r\n");
out.write(" <h2>상품 목록</h2>\r\n");
out.write(" <div class=\"breadcrumb__option\">\r\n");
out.write(" <a href=\"main.jsp\">Home</a> <span>TeaList</span>\r\n");
out.write(" </div>\r\n");
out.write(" </div>\r\n");
out.write(" </div>\r\n");
out.write(" </div>\r\n");
out.write(" </div>\r\n");
out.write(" </section>\r\n");
out.write(" <!-- Breadcrumb Section End -->\r\n");
out.write("\r\n");
out.write(" <!-- Product Section Begin -->\r\n");
out.write(" <section class=\"product spad\">\r\n");
out.write(" <div class=\"container\">\r\n");
out.write(" <div class=\"row\">\r\n");
out.write(" <div class=\"col-lg-3 col-md-5\">\r\n");
out.write(" <div class=\"sidebar\">\r\n");
out.write(" <div class=\"sidebar__item\">\r\n");
out.write(" <h4>상품</h4>\r\n");
out.write(" <ul>\r\n");
out.write(" <li><a href=\"teaListPage.do\">전체</a></li>\r\n");
out.write(" <li><a href=\"teaListPage.do?teaCategory=녹차\">녹차</a></li>\r\n");
out.write(" <li><a href=\"teaListPage.do?teaCategory=홍차\">홍차</a></li>\r\n");
out.write(" <li><a href=\"teaListPage.do?teaCategory=루이보스\">루이보스</a></li>\r\n");
out.write(" <li><a href=\"teaListPage.do?teaCategory=우롱차\">우롱차</a></li>\r\n");
out.write(" <li><a href=\"teaListPage.do?teaCategory=허브차\">허브차</a></li>\r\n");
out.write(" </ul>\r\n");
out.write(" </div>\r\n");
out.write(" </div>\r\n");
out.write(" </div>\r\n");
out.write(" <!-- 범인 색출 -->\r\n");
out.write(" <div class=\"col-lg-9 col-md-7\">\r\n");
out.write(" <div class=\"product__discount\">\r\n");
out.write(" <div class=\"section-title product__discount__title\">\r\n");
out.write(" <h2>상품 목록</h2>\r\n");
out.write(" </div>\r\n");
out.write(" </div>\r\n");
out.write(" <div class=\"row\">\r\n");
out.write(" ");
if (_jspx_meth_c_005fif_005f0(_jspx_page_context))
return;
out.write("\r\n");
out.write(" ");
if (_jspx_meth_c_005fif_005f1(_jspx_page_context))
return;
out.write("\r\n");
out.write(" </div>\r\n");
out.write(" <div class=\"product__pagination\">\r\n");
out.write(" ");
if (_jspx_meth_c_005fif_005f4(_jspx_page_context))
return;
out.write("\r\n");
out.write(" ");
if (_jspx_meth_c_005fforEach_005f1(_jspx_page_context))
return;
out.write("\r\n");
out.write(" ");
if (_jspx_meth_c_005fif_005f5(_jspx_page_context))
return;
out.write("\r\n");
out.write(" <!-- 페이징\r\n");
out.write(" <a href=\"#\"><i class=\"fa fa-long-arrow-left\"></i></a>\r\n");
out.write(" <a href=\"#\">1</a>\r\n");
out.write(" <a href=\"#\">2</a>\r\n");
out.write(" <a href=\"#\">3</a>\r\n");
out.write(" <a href=\"#\"><i class=\"fa fa-long-arrow-right\"></i></a>\r\n");
out.write(" -->\r\n");
out.write(" </div>\r\n");
out.write(" </div>\r\n");
out.write(" </div>\r\n");
out.write(" </div>\r\n");
out.write(" </section>\r\n");
out.write(" <!-- Product Section End -->\r\n");
out.write("\r\n");
out.write(" <!-- Footer Section Begin -->\r\n");
out.write(" ");
if (_jspx_meth_try_005fFooter_005f0(_jspx_page_context))
return;
out.write("\r\n");
out.write(" <!-- Footer Section End -->\r\n");
out.write("\r\n");
out.write(" <!-- Js Plugins -->\r\n");
out.write(" <script src=\"js/jquery-3.3.1.min.js\"></script>\r\n");
out.write(" <script src=\"js/bootstrap.min.js\"></script>\r\n");
out.write(" <script src=\"js/jquery.nice-select.min.js\"></script>\r\n");
out.write(" <script src=\"js/jquery-ui.min.js\"></script>\r\n");
out.write(" <script src=\"js/jquery.slicknav.js\"></script>\r\n");
out.write(" <script src=\"js/mixitup.min.js\"></script>\r\n");
out.write(" <script src=\"js/owl.carousel.min.js\"></script>\r\n");
out.write(" <script src=\"js/main.js\"></script>\r\n");
out.write("\r\n");
out.write("\r\n");
out.write("</body>\r\n");
out.write("\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);
}
}
private boolean _jspx_meth_try_005ffavicon_005f0(javax.servlet.jsp.PageContext _jspx_page_context)
throws java.lang.Throwable {
javax.servlet.jsp.PageContext pageContext = _jspx_page_context;
javax.servlet.jsp.JspWriter out = _jspx_page_context.getOut();
// try:favicon
org.apache.jsp.tag.web.favicon_tag _jspx_th_try_005ffavicon_005f0 = new org.apache.jsp.tag.web.favicon_tag();
_jsp_getInstanceManager().newInstance(_jspx_th_try_005ffavicon_005f0);
try {
_jspx_th_try_005ffavicon_005f0.setJspContext(_jspx_page_context);
_jspx_th_try_005ffavicon_005f0.doTag();
} finally {
_jsp_getInstanceManager().destroyInstance(_jspx_th_try_005ffavicon_005f0);
}
return false;
}
private boolean _jspx_meth_try_005fHumberger_005f0(javax.servlet.jsp.PageContext _jspx_page_context)
throws java.lang.Throwable {
javax.servlet.jsp.PageContext pageContext = _jspx_page_context;
javax.servlet.jsp.JspWriter out = _jspx_page_context.getOut();
// try:Humberger
org.apache.jsp.tag.web.Humberger_tag _jspx_th_try_005fHumberger_005f0 = new org.apache.jsp.tag.web.Humberger_tag();
_jsp_getInstanceManager().newInstance(_jspx_th_try_005fHumberger_005f0);
try {
_jspx_th_try_005fHumberger_005f0.setJspContext(_jspx_page_context);
_jspx_th_try_005fHumberger_005f0.doTag();
} finally {
_jsp_getInstanceManager().destroyInstance(_jspx_th_try_005fHumberger_005f0);
}
return false;
}
private boolean _jspx_meth_try_005fHead_005f0(javax.servlet.jsp.PageContext _jspx_page_context)
throws java.lang.Throwable {
javax.servlet.jsp.PageContext pageContext = _jspx_page_context;
javax.servlet.jsp.JspWriter out = _jspx_page_context.getOut();
// try:Head
org.apache.jsp.tag.web.Head_tag _jspx_th_try_005fHead_005f0 = new org.apache.jsp.tag.web.Head_tag();
_jsp_getInstanceManager().newInstance(_jspx_th_try_005fHead_005f0);
try {
_jspx_th_try_005fHead_005f0.setJspContext(_jspx_page_context);
_jspx_th_try_005fHead_005f0.doTag();
} finally {
_jsp_getInstanceManager().destroyInstance(_jspx_th_try_005fHead_005f0);
}
return false;
}
private boolean _jspx_meth_try_005fHero_005f0(javax.servlet.jsp.PageContext _jspx_page_context)
throws java.lang.Throwable {
javax.servlet.jsp.PageContext pageContext = _jspx_page_context;
javax.servlet.jsp.JspWriter out = _jspx_page_context.getOut();
// try:Hero
org.apache.jsp.tag.web.Hero_tag _jspx_th_try_005fHero_005f0 = new org.apache.jsp.tag.web.Hero_tag();
_jsp_getInstanceManager().newInstance(_jspx_th_try_005fHero_005f0);
try {
_jspx_th_try_005fHero_005f0.setJspContext(_jspx_page_context);
_jspx_th_try_005fHero_005f0.doTag();
} finally {
_jsp_getInstanceManager().destroyInstance(_jspx_th_try_005fHero_005f0);
}
return false;
}
}
변환된 코드
JSP 구성 요소
- 지시자(Directive)
- <%@ %>
- JSP 페이지의 속성을 지정함
- 주석(Comment)
- <%-- -->
- JSP 페이지 설명을 넣음
- 선언(Declaration)
- <%! %>
- 변수와 메서드를 선언함
- 표현식(Expression)
- <%= %>
- 계산식이나 함수를 호출한 결과를 문자열 형태로 출력함
- 스크립틀릿(Scriptlet)
- <% %>
- 자바 코드를 기술함
- 액션 태그(Action Tag)
- <jsp:action> </jsp:action>
- 자바 빈이나 애플릿 서버 모듈과 연결함
JSP 내장 객체
JSP 내장 객체는 JSP 페이지에서 사용할 수 있도록 JSP 컨테이너에 미리 정의된 객체를 의미합니다. JSP 페이지가 서블릿 프로그램으로 번역될 때 JSP 컨테이너가 자동으로 내장 객체를 멤버 변수, 메서드 매개 변수 등의 각종 참조 변수로 포함합니다. JSP 페이지에 별도의 import문 없이 자유롭게 사용 가능하며 스크립틀릿이나 표현식에 선언을 하거나 객체를 생성하지 않고도 직접 호출하여 사용 가능합니다.
JSP 내장 객체의 종류
- request : 웹 브라우저의 HTTP 요청 정보를 저장합니다.
- response : 웹 브라우저의 HTTP 요청에 대한 응답 정보를 저장합니다.
- out : JSP 페이지에 출력할 내용을 담고 있는 출력 스트림입니다.
- session : 웹 브라우저의 정보를 유지하기 위한 세션 정보를 저장합니다.
- application : 웹 애플리케이션의 콘텍스트 정보를 저장합니다.
- pageContext : JSP 페이지의 정보를 저장합니다.
- page : JSP 페이지를 구현한 자바 클래스로 JSP 페이지 자체를 나타냅니다.
- config : JSP 페이지의 설정 정보를 저장합니다.
- exception : JSP 페이지의 예외 발생을 처리합니다.
실습
실습 1
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<jsp:useBean id="cb" class="test.CalcBean"/>
<%--
액션태그 == 객체화
test.CalcBean cb = new test.CalcBean();
CalcBean cb = new CalcBean();
--%>
<jsp:setProperty property="*" name="cb"/>
<%-- cb라는 객체의 멤버변수 초기화 --%>
<%-- 객체의 멤버변수 이름과 파라미터 이름을 일치시키기 --%>
<%-- 멤버변수를 초기화할때에 name값을 보고 자동호출
자동 형변환 처리됨
setter 메서드를 전부 호출 --%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>코드 분리하기</title>
</head>
<body>
<% cb.calculate(); %>
<form method="post">
<input type="number" name="num1">
<select name="op">
<option>+</option>
<option>-</option>
</select>
<input type="number" name="num2">
<input type="submit" value="결과확인">
</form>
<hr>
계산결과 : <jsp:getProperty property="result" name="cb"/>
<%= cb.getResult() %>
</body>
</html>
실습 2
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<jsp:useBean id="lb" class="test.LoginBean"/>
<jsp:setProperty property="*" name="lb"/>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>로그인</title>
</head>
<body>
<% lb.check(); %>
<form method="POST">
아이디 <input type="text" name="mid" placeholder="아이디" required><br>
비밀번호 <input type="password" name="mpw" placeholder="비밀번호" required><br>
<input type="submit" value="로그인">
</form>
<jsp:getProperty property="result" name="lb"/>
<%= lb.getResult() %>
</body>
</html>
실습 3
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="test.MemberVO"%>
<jsp:useBean id="mDAO" class="test.MemberDAO"/>
<jsp:useBean id="mVO" class="test.MemberVO"/>
<jsp:setProperty property="*" name="mVO"/>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="https://code.jquery.com/jquery-3.7.0.min.js" integrity="sha256-2Pmvv0kuTBOenSvLm6bvfBSSHrUJ+3A7x6P5Ebd07/g=" crossorigin="anonymous"></script>
<title>실습3</title>
</head>
<body>
<script type="text/javascript">
<%
if (request.getMethod().equals("POST")) {
if (request.getParameter("action").equals("login")) {
MemberVO data = mDAO.selectOne(mVO);
if (data != null) {
out.println("alert('" + data.getMid() + "님, 로그인 성공!');");
}
else {
out.println("alert('로그인 실패...');");
}
}
// action값이 signup
else if (request.getParameter("action").equals("signup")) {
// 회원가입
boolean flag = mDAO.insert(mVO);
if (flag) {
out.println("alert('회원가입 성공!')");
} else {
out.println("alert('회원가입 실패...')");
}
}
}
%>
</script>
<script type="text/javascript">
function signup() {
var ans = confirm('입력하신 정보로 회원가입할까요?');
if (ans == true) {
// form 태그 내부에있는 action의 값이 signup이 될수있게
// 그 상태로 submit 진행
document.test.action.value="signup";
document.test.submit();
}
else {
return;
}
}
</script>
<form method="POST" name="test">
<input type="hidden" name="action" value="login">
아이디<input type="text" placeholder="아이디" name="mid" required><br>
비밀번호<input type="password" placeholder="비밀번호" name="mpw" required><br>
<input type="submit" value="로그인">
<input type="button" value="회원가입" onClick="signup()">
</form>
<hr>
<h2>회원목록</h2>
<div id="wrap">
<ul>
<%
for(MemberVO v : mDAO.selectAll(null)) {
// out.println("<li>" + v + "</li>");
%>
<li><%= v %></li>
<%
}
%>
</ul>
</div>
</body>
</html>
실습 4
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="test.MemberVO"%>
<jsp:useBean id="mDAO" class="test.MemberDAO"/>
<jsp:useBean id="mVO" class="test.MemberVO"/>
<jsp:setProperty property="*" name="mVO"/>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="https://code.jquery.com/jquery-3.7.0.min.js" integrity="sha256-2Pmvv0kuTBOenSvLm6bvfBSSHrUJ+3A7x6P5Ebd07/g=" crossorigin="anonymous"></script>
<title>실습3</title>
</head>
<body>
<script type="text/javascript">
$(document).ready(function() {
<%
if (request.getMethod().equals("POST")) {
MemberVO data = mDAO.selectOne(mVO);
if (data != null) {
out.println("alert('" + data.getMid() + "님, 로그인 성공!');");
}
else {
out.println("alert('로그인 실패...');");
}
}
%>
$('#btn3').click(function() {
<%
for(MemberVO v : mDAO.selectAll(null)) {
%>
$('#list').append('<li>' + <%= v %> + '</li>');
<%
}
%>
});
});
</script>
<form method="POST">
아이디<input type="text" placeholder="아이디" name="mid" required><br>
비밀번호<input type="password" placeholder="비밀번호" name="mpw" required><br>
<input type="submit" value="로그인">
<button>회원가입</button>
</form>
<button id="btn3">회원목록</button>
<hr>
<h2>회원목록</h2>
<div id="wrap">
<ul id="list">
<%-- <%
for(MemberVO v : mDAO.selectAll(null)) {
// out.println("<li>" + v + "</li>");
%>
<li><%= v %></li>
<%
}
%> --%>
</ul>
</div>
</body>
</html>
GitHub
https://github.com/Qkrwnsgus0522/JSP
728x90