request 내장 객체

조수경·2022년 1월 7일
0

JSP

목록 보기
16/45

request 내장 객체란?

  • JSP 페이지에서 가장 많이 사용되는 기본 내장된 객체
  • 웹 브라우저(크롬)에서 서버(톰캣)의 JSP 페이지로 전달하는 정보를 저장
  • form 페이지로부터 입력된 데이터를 전달하는 요청 파라미터(?name=개똥이)
    값을 JSP 페이지로 가져옴
  • ex) HttpServletRequest 객체 타입의 request 객체

request 내장 객체 종류

1. 사용자가 form에 입력한 요구 사항을 얻어낼 수 있도록 하는 메소드 제공

  • String getParameter(name) : 파라미터 변수 name에 저장된 값을 얻어내는 메소드이며 해당 변수명이 없으면 null을 리턴함
  • String[] getParameterValues(name) : 파라미터 변수
  • Enumeration getParameterNames() : 요청에 의해 넘어오는 모든 파라미터 변수를 java.util.Enumeration 타입으로 리턴함
    - String getProtocol() : 웹 서버로 요청 시 사용 중인 프로토콜 리턴
  • String getServerName() : 서버의 도메인 이름 리턴
    - String getMethod() : 요청에 사용된 요청 방식(get, post 등) 리턴
  • String getQueryString() : 요청에 사용된 QueryString 리턴
  • String getRequestURL() : 요청에 사용된 url 주소 리턴
  • String getRequestURI() : 요청에 사용된 uri로부터 url값 리턴
  • String getRemoteHost() : 웹 서버로 정보를 요청한 웹 브라우저의 host이름 리턴

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>request 내장 객체</title>
</head>
<body>
<!-- process.jsp?name=개똥이 -->
 <form method="post" action="process.jsp">
   <p>
             이름 : <input type="text" name="name" />&nbsp;
      <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>request 내장 객체</title>
</head>
<body>
<%
   //HttpServletRequest request 
   //form에서 입력한 한글이 request 객체에서 정상적으로 처리되려면
   //반드시 request 인코딩 세팅을 해야함
   request.setCharacterEncoding("UTF-8");
   String name = request.getParameter("name");
%>
<p>이름 : <%=name%>
</body>
</html>

리퀘에서 개똥이를 입력하고 전송버튼을 누르면 프로세스로 넘어가서 개똥이가 나옴!

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>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>

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>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>

아이디와 패스워드 이름을 입력받으면 process 파라미터로 넘어가서 출력됨

request 내장 객체 특징

웹 브라우저(크롬)는 HTTP 헤더에 부가적인 정보를 담아 서버로 전송함
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>request 내장 객체</title>
</head>
<body>

<%
  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 = 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>

profile
신입 개발자 입니다!!!

0개의 댓글