JSP response vs request (sendRedirect)

별의개발자커비·2023년 4월 18일
0

JSP

목록 보기
6/31
post-thumbnail

리다이렉트

웹 서버가 웹 브라우저에게 다른 페이지로 이동하라고 응답하는 기능

response.sendRedirect

#first.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
	pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<h1>first.jsp</h1>
	<hr>
	<%
		response.sendRedirect("second.jsp");
	%>
</body>
</html>

#second.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
	pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<h1>second.jsp</h1>
</body>
</html>


#first.jsp파일을 실행했지만 response.sendRedirect("second.jsp");로 인해 second.jsp로 이동된다.

forward vs response

forwoard

  • 주소는 안바뀌고 페이지만 가져와서 바뀜
  • 얘는 그냥 삼각형 형태

response

  • 주소도 거기로 바뀜
  • 클라이언트 / 서버가 있을 때
  • 클라이언트가 서버한테 response 가져와달라고 했더니
  • response를 뜯어봄
  • 서버가 클라이언트한테 '다시 그 주소로 요청해!' 하니까 클라이언트는 google로 요청해서 가는 것
  • 그니까 클->서 갔다가 클<-서 왔다가 클->서 다시 가는 형태

response + request 예제

// response.jsp
	<form action="reponse_process2.jsp" method="post">
		<p>아이디: <input type="text" name="id">
		<p>패스워드: <input type="password" name="pwd">
		<input type="submit" value="전송">
//reponse_process2.jsp
    <%
	String id = request.getParameter("id");
	String pwd = request.getParameter("pwd");

	if (id.equals("admin") && pwd.equals("1234")) {
		response.sendRedirect("success.jsp");
	} else {
		response.sendRedirect("failed.jsp");
	}
	%>
profile
비전공자 독학러. 일단 쌔린다. 개발 공부👊

0개의 댓글