scope = 영역 / 메모리
pageScope : page 객체를 참조하는 객체
requestScope : request 객체를 참조하는 객체
sessionScope : session 객체를 참조하는 객체
applicationScope : application 객체를 참조하는 객체
param : 요청 파라미터를 참조하는 객체
paramValues : 요청 파라미터(배열)를 참조하는 객체
initParam : 초기화 파라미터를 참조하는 객체
cookie : cookie 객체를 참조하는 객체
[저장되어 있는 시간]
applicationScope : 실행 중에만 데이터 저장됨 톰캣이 어플리케이션 날릴 때까지
pageContext : 자기자신만
session : 시간에 구애받는 것 (장바구니 / 로그인 시간 정함)
request : 포워딩까지
[계산 scope_test1.jsp] <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>스코프 테스트</h1> applicationScope : ${ applicationScope.application_name }<br /> sessionScope : ${ sessionScope.session_name }<br /> pageScope : ${ pageScope.page_name }<br /> requestScope : ${ requestScope.request_name } </body> </html>
[결과값] pageScope와 requestScope는 나오지 않는다.
[계산 scope_el.jsp] <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="objelOk.jsp" method="get"> 아이디 : <input type="text" name="id"><br /> 패스워드 : <input type="password" name="pw"><br/> <input type="submit" value="login"> </form> <% application.setAttribute("application_name", "application_value"); session.setAttribute("session_name", "session_value"); pageContext.setAttribute("page_name", "page_value"); request.setAttribute("request_name", "request_value"); %> </body> </html>
[결과값]
pageScope : ${pageScope.page_name}<br /> <jsp:forward page="objelOk.jsp" /> <%-- 포워딩 --%> 를 추가하면 form 페이지를 넘긴다.
[계산 objelOk.jsp] <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% String id = request.getParameter("id"); String pw = request.getParameter("pw"); %> 아이디 :<%=id%><br /> 비밀번호 :<%=pw%><br /> <hr> applicationScope : ${ applicationScope.application_name }<br /> sessionScope : ${ sessionScope.session_name }<br /> pageScope : ${ pageScope.page_name }<br /> requestScope : ${ requestScope.request_name } </body> </html>
[결과값]
다운방법
http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/
C:\Users\USER\Documents\JSTL\jakarta-taglibs-standard-1.1.2\lib
jstl.jar / standard.jar 파일 복사
eclipse lib에 붙여넣기
[계산 jstl_example.jsp] <%@page import="edu.global.ex.vo.EmpVO"%> <%@page import="edu.global.ex.dao.EmpDAO"%> <%@page import="java.util.*"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@page import="java.sql.*"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <%-- <%out.print("AAA");%><br> <%="AAA" %> ${"AAA"} <br> <hr>--%> <%-- <c:out> 태그 --%> <!-- 출력문을 만드는 태그다. --> <h3> out태그</h3> </br> <c:out value="hello world" /> <br> <c:out value="hello"></c:out> <br> <c:out value='${"안녕하세요"}'></c:out> <br> <hr> <%-- <c:set> 태그 --%> <!-- 변수를 다룰때 사용한다. --> <h3> set태그</h3> </br> <!-- var name1 = "홍길동"; --> <c:set var="name1" value="홍길동" /> <c:set var="name2" value="홍길순" /> <% pageContext.setAttribute("name1", "바뀔까"); %> ${name1}</br> ${name2}</br> <hr> <%-- <c:remove> 태그 --%> <h3> remove태그</h3> </br> <c:remove var="name1" /> ${name1}</br> ${name2}</br> <hr> <%-- <c:if> 태그 --%> <h3> if태그</h3> </br> <c:if test="${10 > 20}" var="result1"> 10은 20보다 크다.<br> </c:if> result1 : ${result1}<br> <c:if test="${10 < 20}" var="result2"> 20은 10보다 크다.<br> </c:if> result2 : ${result2}<br> <hr> <%-- <c:choose> 태그 --%> <h3> choose태그</h3> </br> <c:set var="user" value="abcd132" /> <c:choose> <c:when test="${user=='admin'}"> 관리자 페이지 </c:when> <c:otherwise> ${user}님 반갑습니다. </c:otherwise> </c:choose> <%-- <c:forEach> -배열 --%> <!-- items에는 다음을 저장할 수 있다. --> <!-- 배열 --> <!-- java.util.Collection 구현체(ArrayList, LinkedList, EnumSet,...) --> <!-- java.util.Iterator 구현체 --> <!-- java.util.Enumeration 구현체 --> <!-- java.util.Map 구현체 --> <!-- 콤마(,) 구분자로 나열된 문자열 --> <hr> <!-- $(달러 문법(el)을 많이 헷갈려함) --> <!-- $(el)은 메모리에 올라간 것을 가져올 때 쓰는 것이라 외움 --> <h3> forEach배열</h3> </br> <% pageContext.setAttribute("numList",new String[]{"1","2","3","4","5"});%> <ul> <c:forEach var="num" items="${numList}" begin="1" end="3"> <li>${num}</li> </c:forEach> </ul> <hr> <%-- <% List<String> fruits = new ArrayList<>(); fruits.add("apple"); fruits.add("banana"); fruits.add("pineapple"); fruits.add("kiwi"); fruits.add("melon"); pageContext.setAttribute("fruits", fruits); %> <ul> <c:forEach var="fruit" items="${fruits}"> <li>${fruit}</li> </c:forEach> </ul> --%> <% pageContext.setAttribute("fruitsList", new String[]{"apple","banana","pineapple","kiwi","melon"});%> <ul> <c:forEach var="fruits" items="${fruitsList}"> <li>${fruits}</li> </c:forEach> </ul> <hr> <% EmpDAO dao = new EmpDAO(); List<EmpVO> emps = dao.empSelect(); pageContext.setAttribute("emps", emps); %> <ul> <c:forEach var="emp" items="${emps}"> <li>${emp.ename}</li> </c:forEach> </ul> </body> </html>
[결과값]
[계산 jstl_구구단.jsp] <%@page import="edu.global.ex.vo.EmpVO"%> <%@page import="edu.global.ex.dao.EmpDAO"%> <%@page import="java.util.*"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@page import="java.sql.*"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <table border="1"> <c:forEach var="i" begin="1" end="9"> <tr> <c:forEach var="j" begin="1" end="9"> <td>${j} x ${i} = ${i*j}</td> </c:forEach> </tr> </c:forEach> </table> </body> </html>
[결과값]
Model1 : View와 Controller가 같이 있는 형태
Model2 : Model, View 그리고 Controller가 모두 모듈화 되어 있는 형태
Dummy Data : insert into mvc_board(bid,bname,btitle,bcontent,bhit,bgroup,bstep,bindent) values(mvc_board_seq.nextval,'abcd','is title','is content', 0, mvc_board_seq.currval,0,0);
src폴더에 package 생성
[계산 BoardController.java] package edu.global.board.controller; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import edu.global.board.command.BCommand; /** * Servlet implementation class BoardController */ @WebServlet("/*.do") public class BoardController extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public BoardController() { super(); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } private void actionDo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("actionDo() .."); request.setCharacterEncoding("UTF-8"); String viewPage = null; BCommand command = null; } } --------------------------------------------------------------------- [계산 BCommand.java] package edu.global.board.command; import java.io.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public interface BCommand{ // 인터페이스 public abstract void execute(HttpServletRequest request, HttpServletResponse response); } --------------------------------------------------------------------- [계산 BListCommand.java] package edu.global.board.command; import java.io.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import edu.global.board.dao.BDao; public class BListCommand implements BCommand{ //실행파일 @Override public void execute(HttpServletRequest request, HttpServletResponse response) { BDao dao = new BDao(); } } --------------------------------------------------------------------- [계산 BDao.java] package edu.global.board.dao; import javax.naming.*; import javax.sql.DataSource; public class BDao { private DataSource dataSource; public BDao() { try { Context context = new InitialContext(); dataSource = (DataSource) context.lookup("java:comp/env/jdbc/oracle"); } catch (Exception e) { e.printStackTrace(); } } }
[결과값]