- ✔ main.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> <% System.out.println("중간 태그"); %> <script> console.log("콘솔 태그") </script> <jsp:forward page="sub.jsp"/> // 액션태그 - 어떤 동작을 지시하는 태그 <h1>꼬리말 입니다.</h1> // forward를 만나면 sub.jsp파일이 열리면서 꼬리말은 출력 안됨 (include와의 차이) </body> </html>
- ✔ sub.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> 서브 페이지 입니다. (sub.jsp) </body> </html>
- 결과
- ✔ include.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> <%@include file="include01.jsp" %> <main> <h1>여기는 메인입니다.</h1> </main> </body> </html>
- ✔ include01.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> <header> <h1>메뉴입니다.</h1> </header> </body> </html>
- 결과
- ✔ forward.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> <jsp:forward page="forward_param.jsp"> <jsp:param name="id" value="abcde" /> <jsp:param name="pw" value="1234" /> </jsp:forward> </body> </html>
- ✔ forward_param.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, pw; %> <% id = request.getParameter("id"); pw = request.getParameter("pw"); %> 아이디:<%=id %><br> 패스워드:<%=pw %><br> </body> </html>
- 결과
액션 태그 (페이지 전환) | 특징 |
---|---|
Forward | • Forward는 Web Container 차원에서 페이지의 이동만 존재 • 웹 브라우저에는 최초에 호출한 URL이 표시되고, 이동한 페이지의 URL 정보는 확인할 수 없음 • 현재 실행중인 페이지와 forward에 의해 호출될 페이지는 Request 객체와 Response 객체를 공유 • 시스템에 변화가 생기지 않는 단순 조회 요청(글 목록 보기, 검색)의 경우 forward로 응답하는 것이 바람직 |
Redirect | • Web Container로 명령이 들어오면, 웹 브라우저에게 다른 페이지로 이동하라고 명령 -> 웹 브라우저는 URL을 지시된 주소로 바꾸고 해당 주소로 이동 • 다른 웹 컨테이너에 있는 주소로 이동하며 새로운 페이지에서는 Request와 Response객체가 새롭게 생성 • 시스템에 변화가 생기는 요청(회원가입, 글쓰기 등)의 경우에는 redirection을 사용 |
Include | •포워드처럼 해당 URL로 제어권을 넘기지만 include된 페이지의 처리가 끝나면 다시 제어권은 원래의 페이지로 돌아온다. • include한 페이지의 내용을 원래 페이지에 삽입하는 것과 같음 |
✔ 참고: https://doublesprogramming.tistory.com/63
https://mangkyu.tistory.com/51
- ✔ login.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="loginOk.jsp" method="post"> 아이디 : <input type="text" name="id" size="10"><br /> 패스워드 : <input type="password" name="pw" size="10"><br /> <input type="submit" value="전송"><br /> </form> </body> </html>
- ✔ loginOk.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, pw; %> <% id = request.getParameter("id"); pw = request.getParameter("pw"); if(id.equals("abcd") && pw.equals("1234")) { Cookie cookie = new Cookie("id",id); cookie.setMaxAge(60); ⭐ // 쿠키 유효기간 설정 - 60분 / 값을 0 으로 주게되면 쿠키가 삭제된다!! response.addCookie(cookie); response.sendRedirect("welcome.jsp"); ⭐ // Redirect는 user가 다시 welcome.jsp로 접속하게 하는 것 } else { response.sendRedirect("login.jsp"); } %> </body> </html>
- ✔ welcome.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>Welcome jsp입니다.</h1> <% Cookie[] cookies = request.getCookies(); for (int i = 0; i < cookies.length; i++) { out.println(cookies[i].getValue() + "<br>"); } %> </body> </html>
- 결과
서버와의 관계를 유지하기 위한 수단
쿠키와 달리 클라이언트의 특정 위치에 저장되는 것이 아니라, 서버상에 객체로 존재한다.
세션은 서버에서만 접근이 가능하여 보안이 좋고, 저장할 수 있는 데이터에 한계가 없다. 저장 시간은 서버에 따라 다르다.(30분~60분)
- ✔ login.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="loginOk_session.jsp" method="post"> 아이디 : <input type="text" name="id" size="10"><br /> 패스워드 : <input type="password" name="pw" size="10"><br /> <input type="submit" value="전송"><br /> </form> </body> </html>
- ✔ loginOk_session.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, pw; %> <% id = request.getParameter("id"); pw = request.getParameter("pw"); if(id.equals("abcd") && pw.equals("1234")) { /* Cookie cookie = new Cookie("id",id); cookie.setMaxAge(60); response.addCookie(cookie); */ session.setAttribute("id",id); response.sendRedirect("welcome_session.jsp"); // } else { response.sendRedirect("login.jsp"); } %> </body> </html>
- ✔ welcome_session.jsp
<%@page import="java.util.Enumeration"%> <%@ 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>Welcome jsp입니다.</h1> <% String id = (String)session.getAttribute("id"); %> 환영합니다~ <%=id %> 님 <br/> 당신의 패스워드는 <%=pw %> 입니다. <br/> <hr> <% Enumeration enumeration = session.getAttributeNames(); while(enumeration.hasMoreElements()){ String sName = enumeration.nextElement().toString(); String sValue = (String)session.getAttribute(sName); out.println(sName + ":" + sValue + "<br>"); } out.println("======================<br/>"); ⭐ // 세션 특정 데이터 삭제 session.removeAttribute("pw"); enumeration = session.getAttributeNames(); ⭐ // 다시 가져와서 사용 while(enumeration.hasMoreElements()){ String sName = enumeration.nextElement().toString(); String sValue = (String)session.getAttribute(sName); out.println(sName + ":" + sValue + "<br>"); } out.println("======================<br/>"); ⭐ // 세션 모든 데이터 삭제 session.invalidate(); out.println("======================<br/>"); ⭐ // 유효한 세션 아이디 존재 여부 if(request.isRequestedSessionIdValid()){ out.print("session valid"); }else{ out.print("session Invalid"); } %> </body> </html>
- 결과
<%@ 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="grade_result.jsp" method="post"> 국어 : <input type="text" name="kor" size="10"><br /> 영어 : <input type="text" name="eng" size="10"><br /> 수학 : <input type="text" name="math" size="10"><br /> <input type="submit" value="전송"><br /> </form> </body> </html>
- ✔ grade_result.jsp
<%@page import="edu.global.ex.getGrade"%> <%@ 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 kor = request.getParameter("kor"); double k = Double.valueOf(kor); String eng = request.getParameter("eng"); double e = Double.valueOf(eng); String math = request.getParameter("math"); double m = Double.valueOf(math); %> <% getGrade g = new getGrade(k,e,m); out.println("총점 : " +g.getSum() + "<br>"); out.println("평균 : " +g.getAvg() + "<br>"); out.println("등급 : " +g.getGrd() + "<br>"); %> </body> </html>
- 결과
- ✔ 사원의 부서 번호를 이름으로 설정하는 쿼리문
select deptno, decode(deptno,10,'A',20,'B','default') from emp order by deptno;
- 결과
- if문과 비슷한 기능을 제공
여러 가지 경우에서 하나를 선택하는 함수
DECODE 함수는 조건이 일치(=비교 연산자)하는 경우에 대해서만 적용하지만 CASE 함수는 다양한 비교 연산자를 이용하여 조건 제시 및 범위 지정할 수 있다는 점이 차이점
- ✔ 부서 번호에 해당하는 부서명을 구하는 쿼리문
SELECT ename, deptno, CASE WHEN deptno = 10 THEN 'ACCOUNTING' WHEN deptno = 20 THEN 'RESEARCH' WHEN deptno = 30 THEN 'SALES' WHEN deptno = 40 THEN 'OPERATIONS' END AS dept FROM emp;
- 결과