내장 객체의 영역 : page, request, session, application
- JSP 내장 객체
- JSP 페이지에서 객체를 생성하는 과정 없이 바로 사용할 수 있는 객체
- out
- 서버에서 클라이언트로 열려있는 출력 스트림
- JSP 실행 결과를 클라이언트의 브라우저로 출력할 때 가장 효율적으로 사용함
- 페이지 내용을 담고 있는 출력 스트림 객체
- request
- 클라이언트와 웹 서버간의 요청과 관련된 정보는 request 객체에 저장되어 관리된다.
- response
- 클라이언트에 대한 응답처리를 하는 객체
- 실행결과를 브라우저로 되돌려 줄때 사용하는 객체
- (웹 서버가 브라우저에게 지정한 페이지로 이동하도록 지시)
- SESSION 내부 객체
- 클라이언트 요청에 대한 context 정보의 세션과 관련된 정보를 저장하고 관리하는 객체
- 사용자 정보를 잃지 않고 서버에 보관할 수 있도록 하는 객체
- 모든 웹 서버에서 제공되는 것
- 세션은 서버 측의 데이터 저장장소. 쿠키와는 다르다
- 웹 브라우저마다 세션이 따로 존재한다
- 웹 브라우저와 관련된 정보를 저장함
- (웹 브라우저를 닫기 전까지 페이지를 이동하더라도 사용자의 정보를 잃지 않는다)
- application
- 하나의 웹앱을 관리하고 웹 앱안에서의 자원을 공유함
더보기
세션 값 넘기기 1.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<title>session01</title>
<body>
<%
String name = "세종대왕";
// 세션을 이용해서 데이터를 유지
session.setAttribute("name1", name);
//세션 유지 시간
//session.setMaxInactiveInterval(60*60); //60분. 초단위
session.setMaxInactiveInterval(10);
%>
<a href="session.jsp?name=<%=name %>">페이지 이동</a>
<!-- <a href="session.jsp?name=<%=URLEncoder.encode(name, "UTF-8")%>">session</a> -->
</body>
<title>session</title>
<body>
<%
request.setCharacterEncoding("UTF-8");
String name1 = request.getParameter("name");
String name2 = (String)session.getAttribute("name1");
%>
<p><%=name1 %>님 반갑습니다</p>
<p><%=name2 %>님 로그인 됐습니다.</p>
</body>
더보기
세션으로 데이터 값 넘기기
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<title>one</title>
<body>
<%
String uid = request.getParameter("id");
String pwd = request.getParameter("pw");
%>
<form action="two.jsp">
id: <input type="text" name="id">
pass: <input type="password" name="pw">
<input type="submit" value="로그인">
</form>
</body>
<title>two.jsp</title>
<body>
<%
request.setCharacterEncoding("UTF-8");
String uid = request.getParameter("id");
String pwd = request.getParameter("pw");
// 세션을 이용해서 데이터를 유지
session.setAttribute("li", uid);
//세션 유지 시간
session.setMaxInactiveInterval(60);
%>
<form method="post" action="three.jsp">
가장 좋아하는 음식은?
<input type="radio" name="food" value="피자">피자
<input type="radio" name="food" value="파스타">파스타
<input type="radio" name="food" value="탕수육">탕수육
<input type="submit" value="결과">
</form>
<!-- three.jsp를 만들어 어떤 음식을 선택했는지 알아냄
세션에 설정한 id의 값을 가져온다.
if..id 값이 null이 아니면 (피자)를 가장 좋아한다
-->
</body>
<title>three.jsp</title>
<body>
<%
request.setCharacterEncoding("UTF-8");
String food = request.getParameter("food");
String id = (String)session.getAttribute("li");
if(id != null){
food = "피자";
}
%>
<p><%=id %>님 반갑습니다</p>
<p>좋아하는 음식은 <%=food %>입니다.</p>
</body>
jsp에서 다른 페이지로 이동하기 위한 방법
- Redirect
- 브라우저 URL 변경하도록하는 방법
- sendRedirect("이동하고 싶은 페이지 주소")
- request, response 객체 유지 안됨
- ex) 서영이가 식당에 전화를 걸어서 '예약가능 한지 문의 → ARS 1번으로 다시 문의 주세요 → 서영이가 다시 1번으로 문의를 해서 처리한다'
- Forward
- requestDispatcher 객체로 접근해야한다
- 브라우저는 알아채지 못하고, URL도 변경되지 않는다
- request, response 객체 유지됨
- ex) 서영이가 식당에 전화를 걸어서 '예약가능 한지 문의 → 알바생이 누구한테 물어보고와서 답을 준다 → 알바생은 처리해준다'
- Redirect
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<title>res</title>
<body>
<form action="LoginCheck.jsp" method="post">
<label for="user">아이디 : </label>
<input type="text" name="id" id="user"><br>
<label for="userpw">비번 : </label>
<input type="password" name="pwd" id="userpw"><br>
<input type="submit" value="로그인">
</form>
</body>
<%@page import="java.net.URLEncoder"%>
<title>LoginCheck.jsp</title>
<body>
<%
String id = "pink";
String pw = "1234";
String name = "세종대왕";
request.setCharacterEncoding("UTF-8");
if(id.equals(request.getParameter("id")) &&
pw.equals(request.getParameter("pwd"))) {
//쿼리스트링
response.sendRedirect("main.jsp?name="+
URLEncoder.encode(name, "UTF-8"));
} else {
response.sendRedirect("res.jsp");
}
%>
<%
String aa = request.getParameter("aa");
%>
</body>
<title>main.jsp</title>
<body>
<%
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
%>
<p><%=name %>님 로그인 됐습니다.</p>
</body>
- Forward - requestDispatcher 사용하기
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<title>one2.jsp</title>
<body>
<form action="two2.jsp">
<input type="text" name="age">
<input type="submit" value="로그인">
</form>
</body>
<title>two2.jsp</title>
<body>
<%
int age = Integer.parseInt(request.getParameter("age"));
if(age <15){
%>
<script type="text/javascript">
alert("관람 불가능");
history.go(-1);
</script>
<%
} else {
request.setAttribute("name", "세종대왕");
RequestDispatcher dis =
request.getRequestDispatcher("three2.jsp");
dis.forward(request, response);
}
%>
</body>
<%
request.setCharacterEncoding("UTF-8");
String age = request.getParameter("age");
String name = (String)request.getAttribute("name");
%>
<title>three2.jsp</title>
<body bgcolor=aliceblue>
이동페이지
<%=age %>
<%=name %>
</body>
728x90
728x90
'JSP' 카테고리의 다른 글
JAVABEANS (0) | 2022.07.12 |
---|---|
jsp review (0) | 2022.07.12 |
액션 태그 (0) | 2022.07.11 |
내장 객체의 영역 (0) | 2022.07.11 |
JSP (0) | 2022.07.08 |