KDT/WEB

231205 WEB - JSP6(객체 범위, 액션태그, 쿠키)

001cloudid 2023. 12. 5. 12:52
728x90

영역 객체와 속성

그림 1. 영역 객체와 속성

값을 저장 메서드 setAttribute(), 값을 가져오는 메서드 getAttribute()

<!-- 요청 정보 -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jsp1/scopeForm.jsp</title>
</head>
<body>
<h1>jsp1/scopeForm.jsp</h1>
<form action="scopePro1.jsp" method="get">
아이디 : <input type="text" name="id"><br>
비밀번호 : <input type="password" name="pw"><br>
<input type="submit" value="전송">
</form>
</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jsp1/scopePro1.jsp</title>
</head>
<body>
<h1>jsp1/scopePro1.jsp</h1>
<%
//사용자가 입력한 요청 정보를 서버에 전달하면 request 내장객체에 저장
//request에 가져와서 변수에 저장 → 출력
String id = request.getParameter("id");
// 변수명 태그이름
String pw = request.getParameter("pw");
//내장객체 값 저장
pageContext.setAttribute("page", "pageContextValue");
request.setAttribute("req", "requestValue");
session.setAttribute("sess", "sessionValue");
application.setAttribute("app", "applicationValue");
%>
아이디 : <%=id %><br>
비밀번호 : <%=pw %><br>
<!-- 내장객체 값 가져오기 -->
pageContext값 : <%=pageContext.getAttribute("page") %><br>
request값 : <%=request.getAttribute("req") %><br>
session값 : <%=session.getAttribute("sess") %><br>
application값 : <%=application.getAttribute("app") %><br>

<a href="scopePro2.jsp">scopePro2.jsp 이동</a><br>
<!-- get방식 형태 http://localhost:8080/webProject/jsp1/scopePro1.jsp?id=admin&pw=1234 -->
<a href="scopePro2.jsp?id=<%=id %>&pw=<%=pw %>">scopePro2.jsp로 get방식으로 데이터 요청하면서 이동</a><br>
<%
// response.sendRedirect("scopePro2.jsp");
//get방식으로 데이터 요청
response.sendRedirect("scopePro2.jsp?id="+id+"&pass="+pw);
%>
</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jsp1/scopePro2.jsp</title>
</head>
<body>
<h1>jsp1/scopePro2.jsp</h1>
<%
String id = request.getParameter("id");
String pw = request.getParameter("pw");
%>
아이디 : <%=id %><br>
비밀번호 : <%=pw %><br>
<!-- 하이퍼링크는 id, pw를 가져가지 않음. -->

<!-- 내장객체 값 가져오기 -->
pageContext값 : <%=pageContext.getAttribute("page") %><br>
request값 : <%=request.getAttribute("req") %><br>
session값 : <%=session.getAttribute("sess") %><br>
application값 : <%=application.getAttribute("app") %><br>
<!-- 페이지가 바뀜으로서 pageContext값, request값은 사라짐 -->

</body>
</html>

 

4. 액션 태그

JSP문법으로 태그처럼 사용하도록 만든 문법, %밖에 사용

그림 1. 액션태그

jsp:forword

페이지(주소줄) 변경 없이 request값을 가지고 이동

< jsp:forward  page="" />
==
<jsp:forward page=""/>

 

jsp:include

반복되는 코드 가져오기

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jsp2/include1.jsp</title>
</head>
<body>
<h1>jsp2/include1.jsp</h1>
<table border="1" width="600" height="600">
<tr height="100"><td>
<jsp:include page="includeTop.jsp"/>
</td></tr>
<tr height="400"><td><h1>jsp2/include1.jsp 본문내용</h1></td></tr>
<tr height="100"><td>회사주소 부산광역시 전화번호 051-123-1234</td></tr>
</table>
</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jsp2/include2.jsp</title>
</head>
<body>
<h1>jsp2/include2.jsp</h1>
<table border="1" width="600" height="600">
<tr height="100"><td>
<jsp:include page="includeTop.jsp"/>
</td></tr>
<tr height="400"><td><h1>jsp2/include2.jsp 본문내용</h1></td></tr>
<tr height="100"><td>회사주소 부산광역시 전화번호 051-123-1234</td></tr>
</table>
</body>
</html>

 

...

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jsp2/include5.jsp</title>
</head>
<body>
<h1>jsp2/include5.jsp</h1>
<table border="1" width="600" height="600">
<tr height="100"><td>
<jsp:include page="includeTop.jsp"/>
</td></tr>
<tr height="400"><td><h1>jsp2/include5.jsp 본문내용</h1></td></tr>
<tr height="100"><td>회사주소 부산광역시 전화번호 051-123-1234</td></tr>
</table>
</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

<!-- 반복되는 부분을 파일로 가져와서 쓸 수 있게 파일을 만듦 -->
<a href="include1.jsp">메뉴1</a>
<a href="include2.jsp">메뉴2</a>
<a href="include3.jsp">메뉴3</a>
<a href="include4.jsp">메뉴4</a>
<a href="include5.jsp">메뉴5(includeTop에서 수정)</a>

 

5. 쿠키

클라이언트측에서 관리되는 정보를 뜻한다.

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jsp2/cookieTest1.jsp</title>
</head>
<body>
<h1>jsp2/cookieTest1.jsp</h1>
<!--
쿠키(Cookie 자바내장객체)
:JSP 내장객체가 아니며, 객체생성해서 사용. 페이지 상관없이, 서버 상관없이, 사용자 컴퓨터 상관 없이 값을 유지.
서버에서 값을 만들어서 사용자 컴퓨터 하드웨어에 저장
ex) 아이디, 비밀번호 저장
쿠키, 세션(연결정보를 저장)의 공통점과 차이점
세션은 연결정보를 저장, 페이지 상관없이 값이 유지되며, 쿠키는 페이지, 서버, 사용자 컴퓨터에 상관없이 값을 유지
즉, 값을 유지하는 점이 공통점

세션은 서버측에서 저장 관리되며 보안이 필요한 값을 저장(로그인 정보),
쿠키는 사용자에 정보가 저장되며 보안이 취약한 면이 있기 때문에 보안과 상관없는 값을 저장, 서버에 부하없이 사용할 수 있음
쿠키 생성 방법 1) http 헤더를 이용한 쿠키 설정, 2) 서블릿 API를 이용한 쿠키 설정(Cookie 자바내장객체)
-->

<%
//쿠키값 만들기 Cookie 객체 생성 → 서버에 생성
Cookie cookie = new Cookie("cname", "cookieValue");
//Cookie 유지시간설정(초)
cookie.setMaxAge(1800);
//서버에 만들어진 쿠키값 → 사용자 컴퓨터에 전달해서 파일로 저장
response.addCookie(cookie);
%>
쿠키 : <%=cookie %><br>
쿠키이름 : <%=cookie.getName() %><br>
쿠키값 : <%=cookie.getValue() %><br>
쿠키설정시간 : <%=cookie.getMaxAge() %><br>

</body>
</html>

 

그림 2. cookie

 

728x90