Web/JSP
request, response
surhommejk
2017. 12. 1. 15:16
request 객체
실습 : html 에서 입력한 정보들을 jsp에서 받아서 출력해보기
test_doRequest.html (정보를 받아서 보내는 page)
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<form action = "test_getRequest.jsp" method="post">
이 름 : <input type = "text" name="name" size = "20"><br/>
아 이 디 : <input type = "text" name="id" size = "20"><br/>
비밀번호 : <input type = "password" name="pw" size = "20"><br/>
취 미 :
<input type = "checkbox" name="hobby" value="soccer">축구
<input type = "checkbox" name="hobby" value="cook">요리
<input type = "checkbox" name="hobby" value="game">게임
<input type = "checkbox" name="hobby" value="read">독서<br/>
전 공 :
<input type="radio" name="subject" value="design">디자인
<input type="radio" name="subject" value="math">수학
<input type="radio" name="subject" value="eng">영어<br/>
<input type="submit" value="전송">
<input type="reset" value="초기화">
</form>
</body>
</html>
test_getRequest.jsp (html로부터 정보를 받는 처리)
<%@ page import="java.util.*"%>
<%@ 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>
<%!
String name, id, pw, subject;
String[] hobby;
%>
<%
request.setCharacterEncoding("euc-kr"); // 받는 측에서 encoding 설정
// 결국 여기서 출력을 해야하니까!
name = request.getParameter("name");
id = request.getParameter("id");
pw = request.getParameter("pw");
subject = request.getParameter("subject");
hobby = request.getParameterValues("hobby");
out.print("이 름 : " + name + "<br/>");
out.print("아 이 디 : " + id + "<br/>");
out.print("비밀번호 : " + pw + "<br/>");
out.print("전공과목 : " + subject + "<br/>");
out.print("취 미 : " + Arrays.toString(hobby) + "<br/>");
%>
</body>
</html>
response 객체
response 실습
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<form action="redirectpage.jsp">
나이는 ? <input type="text" name = "age" size = "5">
<input type="submit" value="login">
</form>
</body>
</html>
<%@ 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>
<%
String temp = request.getParameter("age");
int age = Integer.parseInt(temp);
if (age >= 20) {
response.sendRedirect("adult.jsp");
} else {
response.sendRedirect("kid.jsp");
}
%>
</body>
</html>
adult.jsp와 kid.jsp는 너무 간단해서 생략