JSP 페이지에서 가장 많이 사용되는 기본 내장된 객체
웹 브라우저(크롬)에서 서버(톰캣포함)의 JSP페이지로 전달하는 정보를 저장
form 페이지로부터 입력된 데이터를 전달하는 요청 파라미터(?name=개똥이)값을 JSP페이지로 가져옴
ex) HttpServletRequest 객체 타입의 request 객체
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<title>request 내장 객체</title>
</head>
<body>
<!--
* request 내장 객체란?
- JSP 페이지에서 가장 많이 사용되는 기본 내장된 객체
- 웹 브라우저(크롬)에서 서버(톰캣포함)의 JSP 페이지로 전달하는 정보를 저장
- form 페이지로부터 입력된 데이터를 전달하는 요청 파라미터 (?name=개똥이)값을 JSP페이지로 가져옴
- ex) HttpServletRequest 객체 타입의 request 객체
-->>
<%
// HttpServletRequest request
// form에서 입력한 한글이 request 객체에서 정상적으로 처리되려면
// 반드시 request 인코딩 세팅을 해야함
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
%>
<p>이 름 : <%=name%></p>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<title>request 내장 객체</title>
</head>
<body>
<!-- process.jsp로 용량 제한없이 가려서 보낼거야~ -->
<form method="post" action="process.jsp">
<p>
이 름 : <input type="text" name="name" />
<input type="submit" value="전송" />
</p>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<title>request 객체</title>
</head>
<body>
<form method="post" action="request01_process.jsp">
<p>아 이 디 : <input type="text" name="id" /></p>
<p>비밀번호 : <input type="text" name="passwd" /></p>
<p><input type="submit" value="전송" /></p>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<title>request 내장 객체</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String userId = request.getParameter("id");
String passWd = request.getParameter("passwd");
%>
<p>아이디 : <%=userId%> / ${param.id}</p>
<p>비밀번호 : <%=passWd%> / ${param.passwd}</p>
</body>
</html>
<%@page import="java.util.Enumeration"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<title>request 내장 객체</title>
</head>
<body>
<!--
웹 브라우저(크롬)는 HTTP 헤더에 부가적인 정보를 담아 서버로 전송함
request 내장 객체는 헤더 정보나 쿠키 관련 정보를 얻을 수 있는 메소드를 제공함
-->
<%
// 헤더 정보 가져오기
String hostValue = request.getHeader("host");
String alValue = request.getHeader("accept-language");
out.print("호스트명 : " + hostValue + "<br>");
out.print("설정된 언어 : " + alValue + "<br>");
//모든 헤더 이름을 가져옴(Enumeration 객체 타입으로 리턴)
Enumeration en = request.getHeaderNames();
while(en.hasMoreElements()){
String headerName = (String)en.nextElement();
String headerValue = (String)request.getHeader(headerName);
out.print(headerName + " : " + headerValue + "<br>");
}
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<title>request 내장 객체</title>
</head>
<body>
<p>클라이언트 IP : <%=request.getRemoteAddr() %></p>
<p>요청 정보 길이 : <%=request.getContentLength() %></p>
<p>요청 정보 인코딩 : <%=request.getCharacterEncoding() %></p>
<p>요청 정보 콘텐츠 유형 : <%=request.getContentType() %></p>
<p>요청 정보 프로토콜 : <%=request.getProtocol() %></p>
<p>요청 정보 전송 방식(GET, POST) : <%=request.getMethod() %></p>
<p>요청 URI(***) : <%=request.getRequestURI() %></p>
<p>콘텍스트 경로(***) : <%=request.getContextPath() %></p>
<p>서버 이름(*) : <%=request.getServerName() %></p>
<p>서버 포트(*) : <%=request.getServerPort() %></p>
<p>쿼리문 : <%=request.getQueryString() %></p>
</body>
</html>
포워드 방식과 리다이렉트 방식의 차이?
1) 포워드 방식 : 최초 요청 정보가 이동된 URL에서도 유효
2) 리다이렉트 방식 : 최초 요청 정보가 이동된 URL에서 유효하지 않음
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<title>response 내장 객체</title>
</head>
<body>
<%
//페이지를 이동하고 싶을 때...
//설정한 URL 페이지로 강제 이동 시킨다
response.sendRedirect("request03.jsp");
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<title>response 내장 객체</title>
</head>
<body>
<form method="post" action="response01_process.jsp">
<p>아이디 : <input type="text" name="id" /></p>
<p>비밀번호 : <input type="text" name="pass" /></p>
<p><input type="submit" value="전송" /></p>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<title>response 내장 객체</title>
</head>
<body>
<h2>떼잉~~로그인 실패!</h2>
<p><a href="response01.jsp">로그인 가기</a></p>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<title>response 내장 객체</title>
</head>
<body>
<!--
* 포워드 방식과 리다이렉트 방식의 차이?
1) 포워드 방식 : 최초 요청 정보가 이동된 URL에서도 유효
- 왜? 이동할 URL로 요청 정보를 그대로 전달
2) 리다이렉트 방식 : 최초 요청 정보가 이동된 URL에서 유효하지 않음
- 왜? 새로운 요청을 생성
-->
<%
request.setCharacterEncoding("UTF-8");
String userId = request.getParameter("id");
String passWord = request.getParameter("pass");
//관리자 로그인 성공
if(userId.equals("admin")&&passWord.equals("1234")){
response.sendRedirect("response01_success.jsp");
}else{ //관리자 로그인 실패 시...
%>
<jsp:forward page="response01_failed.jsp" />
<%
// response.sendRedirect("response01_failed.jsp");
}
%>
</body>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<title>response 내장 객체</title>
</head>
<body>
<h2>로그인 성공!</h2>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="java.util.Date"%>
<!DOCTYPE html>
<html>
<head>
<title>response 내장 객체</title>
</head>
<body>
<p>이 페이지는 5초마다 새로고침 됩니다.</p>
<%
// response.setIntHeader("Refresh", 5);
//헤더이름 name에 value를 추가함
// response.addHeader("ContentType", "text/html; charset=UTF-8");
//설정한 헤더 이름 name에 날짜/시간을 설정(1L : long date(초))
// response.setDateHeader("date", 2L);
response.setContentType("text/html;charset=UTF-8");
%>
<p><%=new Date() %></p>
<%-- contentType : <%=response.getHeader("ContentType") %><br> --%>
<%-- date : <%=response.getHeader("date") %> --%>
</body>
</html>