JAVA/JSP

[JSP] 내장 객체

아잠만_ 2024. 6. 28. 17:28

내장객체(Implicit Object)

JSP페이지에서 사용할 수 있도록 JSP컨테이너에서 미리 정의된 객체

별도의 import문 없이 자유롭게 사용 가능

종류

  • request
    요청정보 저장
  • response
    응답정보 저장
  • out
    출력 스트림
  • session
    동일한 웹브라우저에서 저장
  • application
    다른 웹브라우저 끼리의 저장
  • pageContext
    정보저장
  • page
    페이지 자체
  • config
    설정정보
  • exception
    예외 발생 처리

request 내장 객체

request.jsp

form을 통해 process.jsp로 name 값을 보내는 jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Implicit Objects</title>
</head>
<body>
	<!-- 
	form 페이지
	요청URL : process.jsp
	요청파라미터 : name=이름(input양식 name = value)
	요청방식 : post
	 -->
	 <!-- 
	 	action 기본값 현재 페이지(request.jsp)
		method 기본값 get	 
	 -->
	<form action="process.jsp" method="post">
		<!-- 폼데이터 -->
		<p>
			<label>이름</label>
			<!-- required : 필수 입력 -->
			<input type="text" name="name" required="required" placeholder="이름"/>
			<input type="submit" value="전송"/>
		</p>
	</form>
</body>
</html>
process.jsp

이전 페이지에서 보낸 파라미터를 request 내장객체로 받아 출력하는 jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<!-- 
	요청URL : process.jsp
	요청파라미터 : name=이름(input양식 name = value)
	요청방식 : post
	 -->
<% 
	//내장객체.메서드(인코딩)
	request.setCharacterEncoding("UTF-8");
	// 		변수	    내장객체.폼페이지에서 입력된 데이터를 전달하는 요청파라미터값을 가져오는 메서드
	String name = request.getParameter("name"); %>
	<p>이름 : <%=name %></p>
</body>
</html>

로그인 예제

request01.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
	<!-- 폼페이지 
	요청 URI : request01_process.jsp
	요청파라미터 : {id : a001, password : java}
	요청방식 : post
	-->
	<form action="request01_process.jsp" method="post">
		<!-- 폼데이터 -->
		<p>아이디 : <input type="text" name="id"></p>
		<p>비밀번호 : <input type="password" name="password"></p>
		<p><input type="submit" value="전송"></p>
	</form>
</body>
</html>
request01_process.jsp
<%@page import="java.util.Enumeration"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<!-- 
	요청 URI : request01_process.jsp
	요청파라미터 : {id : a001, password : java}
	요청방식 : post
 -->
<% 	
	// 문자인코딩 유형(한글 처리)
	request.setCharacterEncoding("utf-8");
	// request 내장 객체로 전달된 요청 파라미터 정보를 받음
	String id = request.getParameter("id");
	String pw = request.getParameter("password");
%>
<!-- 표현문 -->
<p>아이디 : <%=id %></p>
<p>비밀번호 : <%=pw %></p>
<!-- 
	웹브라우저는 HTTP 헤더에 부가 정보를 담아서 서버로 전송함. 
	request 내장 객체를 통해 헤더 정보, 쿠키 관련 정보를 얻을 수 있음.
-->
<%
	String hostValue = request.getHeader("host");
	String alValue = request.getHeader("accept-language");
	String clValue = request.getHeader("content-length");
%>
<p><%=hostValue %></p>
<p><%=alValue %></p>
<p><%=clValue %></p>
<hr />
<%
	// 모든 헤더 이름을 열거형으로 저장
	Enumeration en = request.getHeaderNames();
	// 저장된 헤더 이름이 있을 때 까지 반복
	while(en.hasMoreElements()){
		//Object 타입
		String name = (String) en.nextElement();
		out.println("<h3>"+name + " : " +request.getHeader(name)+"</h3>");
	}
%>
<hr />
	<!-- 웹 브라우저 / 서버 관련 메서드를 사용해보기 -->
	<p>요청 IP주소 : <%=request.getRemoteAddr() %></p>
	<p>요청 파라미터 길이 : <%=request.getContentLength() %></p>
	<p>문자 인코딩 : <%=request.getCharacterEncoding() %></p>
	<p>콘텐츠 유형(MIME) : <%=request.getContentType() %></p>
	<p>요청 프로토콜 : <%=request.getProtocol() %></p>
	<p>요청 메서드 : <%=request.getMethod() %></p>
	<p>요청 URI경로 : <%=request.getRequestURI() %></p>
	<p>contextPath : <%=request.getContextPath() %></p>
	<p>서버 이름 : <%=request.getServerName() %></p>
	<p>서버 포트번호 : <%=request.getServerPort() %></p>
	<!-- get방식으로 왔을 때 나옴 post는 null -->
	<p>쿼리스트링(요청파라미터) : <%=request.getQueryString() %></p>
</body>
</html>

response 내장객체

response01.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Implicit Object</title>
</head>
<body>
	<!-- 폼 페이지 
	get  : select(DB 변경 X)
	post : insert/update/delete(DB변경O)
	
	요청 URI : /ch05/response01_process.jsp
	요청파라미터 : request{id : a001, password : java}
	요청방식 : post
	-->
	<form action="/ch05/response01_process.jsp" method="post">
		<p>아이디 : <input type="text" name="id" placeholder="아이디" title="아이디" required></p>
		<p>비밀번호 : <input type="password" name="password" placeholder="비밀번호" title="비밀번호" required></p>
		<p><input type="submit" value="전송"></p>
	</form>
</body>
</html>
response01_process.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Implicit Object</title>
</head>
<body>
<!-- 
	요청 URI : /ch05/response01_process.jsp
	요청파라미터 : request{id : a001, password : java}
	요청방식 : post
	-->
<% 	
	// 문자인코딩 유형(한글 처리)
	request.setCharacterEncoding("utf-8");
	// request 내장 객체로 전달된 요청 파라미터 정보를 받음
	String id = request.getParameter("id");
	String pw = request.getParameter("password");
%>
<!-- 표현문 -->
<p>아이디 : <%=id %></p>
<p>비밀번호 : <%=pw %></p>
<%	
	if(id.equals("a001")&&pw.equals("java")){ // 로그인 성공 시 이동
		response.sendRedirect("/welcome.jsp");
	} else{ // 로그인 실패시 로그인 화면으로 이동
		response.sendRedirect("/ch05/response01.jsp");
	}
%>
</body>
</html>

response02.jsp

새로고침 setIntHeader("Refresh", (sec) )

<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Implicit Objects</title>
</head>
<body>
	<p>이 페이지는 5초마다 새로고침 됩니다</p>
	<%
		//response 내장 객체
		/*
			서버(톰캣) > 응답 > 클라이언트 (크롬)
			MIME 유형, 문자 인코딩, 오류메세지 (404, 500),
			상태코드를 설정 및 가져올 수 있음
		*/
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=utf-8");
		response.setIntHeader("Refresh", 5);
	
	%>
	<p><%=new Date() %></p>
	<p>문자 인코딩 : <%=response.getCharacterEncoding() %></p>
	<p>콘텐츠 유형 : <%=response.getContentType() %></p>
	
</body>
</html>

response03.jsp

오류메세지 출력 sendError

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
	<%
		response.sendError(404, "요청할 페이지를 찾을 수 없습니다");
		//response.sendError(500, "개발자 오류");
	%>
</body>
</html>

out 내장 객체

out01.jsp

print()

<%@page import="java.util.Calendar"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Implicit Object</title>
</head>
<body>
	<%
		out.print("오늘의 날짜 및 시간 : <br />");
		out.print("<p>"+ Calendar.getInstance().getTime() + "</p>");
		out.print("&copy 오리");
	%>
</body>
</html>

out02.jsp
<%@page import="java.util.Calendar"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Implicit Object</title>
</head>
<body>
	<!-- 
	요청 URI : out02_process.jsp
	요청파라미터(HTTP파라미터, QueryString) : request{id : admin, password : java}
	요청방식 : post
	-->
	<form action="out02_process.jsp" method="post">
		<p>아이디 : <input type="text" name="id" placeholder="아이디" title="아이디" required></p>
		<p>비밀번호 : <input type="password" name="password" placeholder="비밀번호" title="비밀번호" required></p>
		<p><input type="submit" value="전송"></p>
	</form>
</body>
</html>
out02_process.jsp
<%@page import="java.util.Calendar"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Implicit Object</title>
</head>
<body>
	<!-- 
		요청 URI : out02_process.jsp
		요청파라미터(HTTP파라미터, QueryString) : request{id : admin, password : java}
		요청방식 : post
	-->
	<%
		//request 내장 객체. 문자 인코딩 유형을  UTF-8로 작성
		request.setCharacterEncoding("utf-8");
		String id = request.getParameter("id"); // admin
		String pw = request.getParameter("password"); //java
		
		/*
		out.print("<p>아이디 : "+id+"</p>");
		out.print("<p>비밀번호 : "+pw+"</p>");
		*/
	%>
	<p>아이디 : <%out.print(id);%></p>
	<p>비밀번호 : <%out.print(pw);%></p>
	
	<!-- 표현문 -->
	<p>아이디 : <%=id %></p>
	<p>비밀번호 : <%=pw %></p>
</body>
</html>