romworld

JSP 07 - request 본문

WEB/JSP

JSP 07 - request

inderrom 2022. 12. 28. 11:39

내장객체

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

JSP 페이지가 서블릿 프로그램으로 번역될 때 JSP 컨테이너가 자동으로 내장 객체를

멤버 변수, 매소드 매개변수 등의 각종 참조 변수(객체)로 포함

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

 

\

자주 사용하는 것 (나머지는 거의 사용 X)

 ==> request, session

 

 

setAttribute(String name, Object value)

getAttribute(String name)

 


request 연습

 

<request.jsp>

 

<%@ 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>Implicit Objects(내장 객체)</title>
</head>
<body>
	<!--  폼 요소(태그) -->
	<!--  process.jsp?name=개똥이
	process.jsp : URL
	name=개똥이 : 요청파라미터(HTTP파라미터, 쿼리스트링) -->
	<form action="process.jsp" method="post">
		<!--  폼 데이터 -->
		<p>
			이름 : <input type="text" name="name"/>
				<input type="submit" value="전송"/>
		</p>
	</form>
</body>
</html>

<process.jsp>

<%@ 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>Implicit Objects(내장 객체)</title>
</head>
<body>
<!--  process.jsp?name=개똥이 -->
<% //스크립틀릿
	// 한글 처리
	request.setCharacterEncoding("UTF-8");
	String name = request.getParameter("name");
%>
	<!--  표현문 -->
	<p>이름 : <%=name %></p>
	<p>요청 정보 길이 : <%=request.getContentLength() %> </p>
	<p>클라이언트 전송 방식 (method가 post/get/put) :<%=request.getMethod() %> </p>
	<p>요청 URI : <%=request.getRequestURI() %></p>
	<p>서버 이름 :<%=request.getServerName() %></p>
	<p>서버 포트 :<%=request.getServerPort() %></p>

</body>
</html>

실행시켜보면

결과가 나온다.

 

request 내장 객체

- 요청 HTTP 헤더 관련 메소드

- 웹브라우저는 HTTP헤더에 부가적인 정보를 담아 서버로 전송

 

 


request 연습 : ID, 비밀번호

 

<request01.jsp>

<%@ 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>Implicit Object(내장 객체)</title>
</head>
<body>
	<!--  폼페이지 
		request01_process.jsp?id=a001&basswd=java
		id=a001&basswd=java : 요청(HTTP) 파라미터 => request객체에 들어간다.
	-->
	<form action="request01_process.jsp" method="post">
		<p>아이디 : <input type="text" name="id"></p>
		<p>비밀번호 : <input type="text" name="passwd"></p>
		<p><input type="submit" value="전송"></p>
	</form>
</body>
</html>

<request01_process.jsp>

<%@ 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>Implicit Objects(내장 객체)</title>
</head>
<body>
	<%  // 스크립틀릿
		// 파라미터의 값은 int라도 String 으로 바뀜
		request.setCharacterEncoding("UTF-8");
		String id = request.getParameter("id");
		String pw = request.getParameter("passwd");
		
		// 헤더(부수적인 정보를 담고 있음)에 있는 host라는 name에 매핑되어 있는 값을 보자
		String hostValue = request.getHeader("host");
		// 헤더에 있는 accept-language라는 name에 매핑되어 있는 값을 보자
		String alValue = request.getHeader("accept-language");
	%>
	<p>호스트명 : <%=hostValue %></p>
	<p>설정된 언어 :<%=alValue %> </p>
	<p>아이디 :<%=id%></p>
	<p>비밀번호 :<%=pw%></p>
</body>
</html>

 

**실행시키면

 

 


request 연습

<%@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>Implicit Object(내장 객체)</title>
</head>
<body>
<%  //스크립틀릿
	// 모든 헤더(웹브라우저가 요청 시 헤더정보를 보냄)의 이름을 가져와보자
	// ==> 리턴타입 : Enumeration(열거형)
	Enumeration en = request.getHeaderNames();
	
	// has : 있니?
	// More : 더
	// Elements : 요소들이
	// 값이 있을 때에만 반복
	while(en.hasMoreElements()){
		// 현재 헤더 이름을 가져옴(Object(컵) -> String(머그컵)으로 downcasting)
		String headerName = (String)en.nextElement();
		String headerValue = request.getHeader(headerName);
		// out : 내장객체 중 하나로써 화면에 데이터를 출력해줌
		out.print("<p>headerName : " + headerName+
				", headerValue : " + headerValue + "</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>Implicit Objects</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>요청 정보 전송 방식 : <%=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>

 

'WEB > JSP' 카테고리의 다른 글

JSP 09 - out  (0) 2022.12.28
JSP 08 - response  (0) 2022.12.28
JSP 06 - 쇼핑몰 사이트 만들기(2), ERWin  (0) 2022.12.26
JSP 05 - 디렉티브 태그 ( taglib02 )  (0) 2022.12.26
JSP 04 - 디렉티브 태그 ( include, taglib)  (0) 2022.12.26
Comments