본문 바로가기
Web/JSP/Servlet/JDBC

request 객체/request란?/request예제 /HttpServletRequest

by 나비와꽃기린 2016. 6. 22.

5.2 request 객체

-사용자의 요청에 관련된 정보를 얻기 위해 사용하는 객체

-클라이언트에서 서버로 보내는 요청을 담고있는 객체

-요청범위를 가지며 javax.servlet.http.HttpServletRequest 클래스의 한 인스턴스

-요청 파라미터와 관련된 메소드들

-서블릿의 service 메소드의 아규먼트인 HttpServletRequest와 동일하게 사용됨

***실습16

requestTest1_Form.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"

    pageEncoding="EUC-KR"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">

<title>Insert title here</title>

</head>

<body>

       

<form action="requestTest1_Form.jsp" method="post">

<table>

<tr border="1">

        <td>이름</td>

        <td><input type="text" name="name"></td>

</tr>

<tr>

        <td>성별</td>

        <td>

                 <input type="radio" name="gender" value="male"/>

                 <input type="radio" name="gender" value="female"/>

        </td>

</tr>

<tr>

        <td>취미</td>

        <td>

                 독서<input type="checkbox" name="hobby" value="독서">

                 게임<input type="checkbox" name="hobby" value="게임">

                 tv시청<input type="checkbox" name="hobby" value="tv시청">

                 축구<input type="checkbox" name="hobby" value="축구">

                 기타<input type="checkbox" name="hobby" value="기타">     

        </td>

</tr>

<tr>

        <td colspan="2"><input type="submit" value="전송"></td>

        <td></td>

</tr>

</table>

 

 

</form>

       

</body>

</html>

 

requestTest1_Form.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"

    pageEncoding="EUC-KR"%>

    <% request.setCharacterEncoding("euc-kr"); %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">

<title>Insert title here</title>

</head>

<body>

       

       

<table>

<tr border="1">

        <td>이름</td>

        <td><%=request.getParameter("name") %></td>

</tr>

<tr>

        <td>성별</td>

        <td>

                 <% if( request.getParameter("gender").equals("male") ){ %>

                         남자

                 <% }else{%>

                         여자

                 <% } %>

        </td>

</tr>

<tr>

        <td>취미</td>

        <td>

        <%

                 String[] hobby=request.getParameterValues("hobby");

                 for(int i=0; i<hobby.length; i++){

        %>

                 <%=hobby[i]%>&nbsp;&nbsp;        

                 <% } %>

 

        </td>

</tr>

 

</table>

 

       

</body>

</html>

 

 

***실습17 HTTP 헤더 정보와 관련된 메소드들

requestTest2.jsp

<%@page import="java.util.Enumeration"%>

<%@ page language="java" contentType="text/html; charset=EUC-KR"

    pageEncoding="EUC-KR"%>

<% request.setCharacterEncoding("euc-kr"); %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">

<title>Insert title here</title>

</head>

<body>

       

       

<table border="1">

<tr >

        <td>헤더이름</td>

        <td>헤더값</td>

</tr>

 

<%

        Enumeration<String> e=request.getHeaderNames();

        while(e.hasMoreElements()){

                 String headerName=(String)e.nextElement();

 

%>

<tr>

        <td><%=headerName %></td>

        <td><%=request.getHeader(headerName) %></td>

</tr>

                 <%       }%>

 

</table>

 

       

</body>

</html>

 

 

***실습18 "쿠키, URL/URI, 요청 방식과 관련된 메소드들"

requestTest3.jsp

 

<%@ page language="java" contentType="text/html; charset=EUC-KR"

    pageEncoding="EUC-KR"%>

<html>

<head>

<title>Request Test3</title>

</head>

<body>

<h1>쿠키, URL/URI, 요청방식에 관련된 정보 예제</h1>

<table border="1">

        <tr>

                 <td>쿠키정보</td>

<%

Cookie[] cookie=request.getCookies();

if(cookie==null){

%>

                 <td>쿠기가 존재하지 않습니다</td>

<%

} else {

        for(int i=0; i<cookie.length;i++){

                 %>

                 <td><%=cookie[i].getName()%>(<%=cookie[i].getValue()%>)&nbsp;&nbsp;</td>

                 <%

        }

}

%>

        </tr>

        <tr>

                 <td>서버 도메인명</td>

                 <td><%=request.getServerName() %>

        </td>

        <tr>

                 <td>서버 포트번호</td>

                 <td><%=request.getServerPort() %>

        </td>

        <tr>

                 <td>요청 URL</td>

                 <td><%=request.getRequestURL() %>

        </td>

        <tr>

                 <td>요청 URI</td>

                 <td><%=request.getRequestURI() %>

        </td>

        <tr>

                 <td>요청 쿼리</td>

                 <td><%=request.getQueryString() %>

        </td>

        <tr>

                 <td>클라이언트 호스트명</td>

                 <td><%=request.getRemoteHost() %>

        </td>

        <tr>

                 <td>클라이언트 IP 주소</td>

                 <td><%=request.getRemoteAddr() %>

        </td>

        <tr>

                 <td>프로토콜</td>

                 <td><%=request.getProtocol() %>

        </td>

        <tr>

                 <td>요청방식</td>

                 <td><%=request.getMethod() %>

        </td>

        <tr>

                 <td>컨텍스트 경로</td>

                 <td><%=request.getContextPath() %>

        </td>

</table>

</body>

</html>

 

 

댓글0