request
ํด๋ผ์ด์ธํธ์ ์์ฒญ ์ ๋ณด๋ฅผ ๋ด๊ณ ์๋ ๊ฐ์ฒด
<%@ page contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>
<h3>request ๋ด์ฅ ๊ฐ์ฒด : ํด๋ผ์ด์ธํธ์ ์์ฒญ ์ ๋ณด๋ฅผ ๋ด๊ณ ์๋ ๊ฐ์ฒด</h3>
<!-- request๋ HttpServletRequest ๊ฐ์ฒด ์ด๋ค.-->
<p>
<a href="ex10_ok.jsp"> request ์ ๋ณดํ์ธ</a>
</p>
</body>
</html>
<%@ page contentType="text/html; charset=UTF-8"%>
<%
String protocol = request.getProtocol();
String serverName = request.getServerName();
int port = request.getServerPort();
String remoteAddr = request.getRemoteAddr(); //โ
String method = request.getMethod();
String agent = request.getHeader("User-Agent");
String fileType = request.getHeader("Accept");
String referer = request.getHeader("Referer"); //์ ์ฃผ์ ํ์ธ ๊ฐ๋ฅ
if(referer==null){
referer="";
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>
<h3>request : ์์ฒญ ์ ๋ณด</h3>
<p>ํ๋กํ ์ฝ : <%=protocol %></p>
<p>์๋ฒ์ด๋ฆ : <%=serverName %></p>
<p>์๋ฒํฌํธ๋ฒํธ : <%=port %></p>
<p>ํด๋ผ์ด์ธํธ์์ดํผ์ฃผ์ : <%=remoteAddr %></p>
<p>method : <%=method %></p>
<p>ํด๋ผ์ด์ธํธ OS ๋ฐ ๋ธ๋ผ์ฐ์ : <%=agent %></p>
<p>๋ธ๋ผ์ฐ์ ๊ฐ ์ง์ํ๋ ๋งค์ฒด ํ์
: <%=fileType %></p>
<p>์ด์ ์ ํด๋ผ์ด์ธํธ URL : <%=referer %></p>
</body>
</html>
response
์์ฒญ์ ๋ํ ์๋ต ์ ๋ณด๋ฅผ ๋ด๊ณ ์๋ ๊ฐ์ฒด
<%@ page contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>
<h3>response : ํด๋ผ์ด์ธํธ์ ์์ฒญ(request)์ ๋ํ ์๋ต(response) ์ ๋ณด๋ฅผ ๋ด๊ณ ์๋ ๊ฐ์ฒด</h3>
<%--
- response : HttpServletResponse ๊ฐ์ฒด
- redirect
: ์น ์ปจํ
์ด๋๋ ์น ๋ธ๋ผ์ฐ์ ์๊ฒ ์ฃผ์ด์ง ํ์ด์ง๋ก ์ด๋ํ๋๋ก ๋ช
๋ น์ ๋ด๋ฆผ
(302์๋ต์ฝ๋)
: ์น ๋ธ๋ผ์ฐ์ ๋ ์ฃผ์ด์ง ์ฃผ์์ ์๋ก์ด ํ์ด์ง์ ์ ์ํ๋ค.
: request, response ๊ฐ์ฒด๋ ์ด๊ธฐํ ๋๊ณ ๋ค์ ์์ฑ๋๋ค.
: ์ผ๋ฐ์ ์ผ๋ก redirect ํ๋ ๊ฒฝ์ฐ
๋ก๊ทธ์ธํ, ๋ก๊ทธ์์ํ, DB์ INSERT, UPDATE, DELETE ํ
--%>
<p>
<a href="ex12_ok.jsp">๋ฆฌ๋ค์ด๋ ํธ</a>
<a href="ex12_ok2.jsp">์๋ฌ๋ฐ์</a>
</p>
</body>
</html>
<%@ page contentType="text/html; charset=UTF-8"%>
<%
response.sendError(400); // ์๋ฌ์ฝ๋๋ฅผ ํด๋ผ์ด์ธํธ์๊ฒ ์ ์ก
%>
<%@ page contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body style="background: yellow;">
<p>์ด ํ๋ฉด์ ๋ณด์ด์ง ์์ต๋๋ค.</p>
<%
response.sendRedirect("ex12_result.jsp"); // ๋ฆฌ๋ค์ด๋ ํธ
%>
</body>
</html>
OUT
ํ์ด์ง ์์ฒญ์ ๋ํ ์๋ต ์ถ๋ ฅ ์คํธ๋ฆฝ ๊ฐ์ฒด
pageContext
JSP ํ์ด์ง ์คํ์ ํ์ํ Context ์ ๋ณด๋ฅผ ๋ด๊ณ ์๋ ๊ฐ์ฒด
<%@ page contentType="text/html; charset=UTF-8"%>
<%
// ํ์ฌ JSP์ ์ฃผ์ด์ง ์ด๋ฆ์ผ๋ก ์์ฑ๊ฐ์ ์ ์ฅ
pageContext.setAttribute("msg", "pageContext ๊ฐ์ฒด ์์ ์
๋๋ค.");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>
<h3>pageContext : JSP ํ์ด์ง ์คํ์ ๋ํ Context ์ ๋ณด๋ฅผ ๋ด๊ณ ์๋ ๊ฐ์ฒด</h3>
<!-- setAttribute()๋ก ์ค์ ํ ์์ฑ๊ฐ ๊ฐ์ ธ์ค๊ธฐ -->
<p><%=pageContext.getAttribute("msg") %></p>
<p> ${msg} </p>
<!--JspWriter getOut() : ์ถ๋ ฅ ์คํธ๋ฆผ -->
<%
pageContext.getOut().print("<p>๋ฐ๊ฐ์์</p>");
%>
</body>
</html>
exception
JSP ํ์ด์ง์์ ๋ฐ์ํ ์์ธ๋ฅผ ์ฒ๋ฆฌํ ํ์ด์ง๋ฅผ ์ง์ ํ ๊ฒฝ์ฐ ์๋ฌ ํ์ด์ง์ ์ ๋ฌ๋๋ ์์ธ ๊ฐ์ฒด
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page errorPage="error.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>
<h3>exception ๋ด์ฅ ๊ฐ์ฒด</h3>
<!--
JSP ํ์ด์ง์์ ๋ฐ์ํ ์์ธ๋ฅผ ์ฒ๋ฆฌํ ํ์ด์ง๋ฅผ ์ง์ ํ ๊ฒฝ์ฐ
์๋ฌ ํ์ด์ง์ ์ ๋ฌ๋๋ ์์ธ ๊ฐ์ฒด
-->
<%
String name = request.getParameter("name").toUpperCase();
%>
<p>
์ด๋ฆ : <%=name %>
</p>
</body>
</html>
์ก์ ํ๊ทธ๋?
JSP ์ก์ ํ๊ทธ๋ ํด๋ผ์๋ํธ ํน์ ์๋ฒ์๊ฒ ์ด๋ ํ ๋์์ ํํ๋๋ก ๋ช ๋ น์ ๋ด๋ฆฌ๋ ํ๊ทธ
JSP ํ์ด์ง์์ ํ์ด์ง์ ๋ชจ๋ํ์ ํ๋ฆ์ ์ด๋ฅผ ์ํด
include, forward ์ก์ ํ๊ทธ๋ฅผ ์ ๊ณตํ๊ณ ,
์๋ฐ ๋น์ ์ฌ์ฉ์ ์ํด useBean, setProperty,getProperty ๋ฑ์
์ก์ ํ๊ทธ๋ฅผ ์ ๊ณตํ๋ค.
ํน์ง |
JSP ์ก์ ํ๊ทธ๋ XML ๊ตฌ๋ฌธ ํ์์ผ๋ก ์์ฑํ๋ค. ์์ ํ๊ทธ๊ฐ ์์ผ๋ฉด ๋ฐ๋์ ๋๋๋ ํ๊ทธ๊ฐ ์์ด์ผ ํ๋ฉฐ, ๋์๋ฌธ์๋ฅผ ๊ตฌ๋ถํ๋ค. |
์ก์ ํ๊ทธ๋ ์ ๋์ด 'jsp:'์ด ๋ถ์ด์ '<jsp:'์ผ๋ก ์์ํด์ผ ํ๋ค. |
์ก์ ํ๊ทธ์ ์์ฑ ๊ฐ๋ง ์ง์ ํ๊ณ ๋ด์ฉ์ด ์์ ๊ฒฝ์ฐ์๋, XML ๊ท์น์ ์ํด ๋๋๋ ํ๊ทธ๋ฅผ ๋ฐ๋ก ํ์ง ์๊ณ ์์ ํ๊ทธ์ ๋ง์ง๋ง ๋ถ๋ถ์ '/>'์ผ๋ก ๋ง๋ฌด๋ฆฌ ํ๋ค. |
<%@ page contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>
<form action="ex02_ok.jsp" method="post">
<p>๋ฒํธ : <input type="text" name="no"></p>
<p>์ด๋ฆ : <input type="text" name="name"></p>
<p>๋์ด : <input type="text" name="age"></p>
<p>์ข์ํ๋ ๊ณผ๋ชฉ : <input type="text" name="subject"></p>
<p>
<button type="submit">์ ์กํ๊ธฐ</button>
</p>
</form>
</body>
</html>
<%@ page contentType="text/html; charset=UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
%>
<%--๋น์์ฑ UserVo vo = new UserVo(); ๊ณผ ์ ์ฌ --%>
<jsp:useBean id="vo" class="ch05.p1.UserVO"/>
<%--์์ฑ๋ ๋น์ ์์ฑ(ํ๋ผ๋ฏธํฐ)๊ฐ ์ค์ --%>
<%--๋น์ ํ๋๋ช
๊ณผ form ํ๊ทธ ์์ input ์์์ ์ด๋ฆ์ด ๋์ผํ ๊ฒ๋ง ์ค์ --%>
<jsp:setProperty name="vo" property="*"/>
<%--๋น์ ํ๋๋ช
๊ณผ form ํ๊ทธ ์์ input ์์์ ์ด๋ฆ์ด ๋ค๋ฅธ ๊ฒฝ์ฐ --%>
<jsp:setProperty name="vo" property="num" param="no"/>
<%
String s="์ฑ์ธ";
if(vo.getAge()<19) s = "๋ฏธ์ฑ๋
์";
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>
<h3> useBean ์ก์
ํ๊ทธ</h3>
<p>๋ฒํธ : <%= vo.getNum() %></p>
<p>์ด๋ฆ : <%= vo.getName() %></p>
<p>๋์ด : <%= vo.getAge() %>,<%=s %></p>
<p>๊ณผ๋ชฉ : <%= vo.getSubject() %></p>
</body>
</html>
ํฌ์๋ฉ(forwarding)
ํด๋ผ์ด์ธํธ์์ ์์ฒญ ๋ฐ์ ์๋ฒ์ ์๋ธ๋ฆฟ ๋๋ JSP๋ ์๋ฒ์ ๋ค๋ฅธ ์์(์๋ธ๋ฆฟ ๋๋ JSP๋ฑ)์ผ๋ก ์์ฒญ ๋ฐ์ ์ ๋ณด๋ฅผ ์ ๋ฌํ์ฌ ์ฒ๋ฆฌํ๋ ๊ฒ์ ์๋ฏธํ๋ค. ํฌ์๋ฉ์ Servlet Container์์์ ์ผ์ด๋๋ ๊ฒ์ผ๋ก ์น ๋ธ๋ผ์ฐ์ ๋ ํฌ์๋ฉ ๋ ์ฌ์ค์ ์ ์๊ฐ ์๋ค. ๋ฐ๋ผ์ ์น ๋ธ๋ผ์ฐ์ ์๋ ์ด์ต์ ํธ์ถํ URL์ด ํ์๋๊ณ ํฌ์๋ฉ๋ ํ์ด์ง์ URL ์ ๋ณด๋ ๋ณผ ์ ์๋ค.
<%@ page contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>
<h3>forward</h3>
<!--
- ํด๋ผ์ด์ธํธ๋ก ๋ถํฐ ์์ฒญ ๋ฐ์ ์๋ธ๋ฆฟ ๋๋ JSP์์ ๋ค๋ฅธ ์์(์๋ธ๋ฆฟ ๋๋ JSP)
์๊ฒ ์์ฒญ ์ ๋ณด๋ฅผ ์ ๋ฌํ์ฌ ์ฒ๋ฆฌ
- ํฌ์๋ฉ์ ์๋ธ๋ฆฟ ์ปจํ
์ด๋์์ ์ด๋ฃจ์ด ์ง๋ฏ๋ก ํด๋ผ์ด์ธํธ๋ ์์ ์๋ค.
๋ธ๋ผ์ฐ์ ์ ์ฃผ์๊ฐ ๋ณ๊ฒฝ๋์ง ์๋๋ค.
์์ฒญ ๋ฐ์ ํ์ด์ง๋ ํฌ์๋ฉ๋๋ ํ์ด์ง(์์)์ request,response ๊ฐ์ฒด๋ฅผ
๋๊ฒจ์ค๋ค.
- ์์ฒญ ๋ฐ์ ํ์ด์ง์์ ํฌ์๋ฉ ๋๋ ํ์ด์ง์ ์ ๋ณด๋ฅผ ์ ๋ฌํ๊ธฐ ์ํด์๋
request.setAttribute("์ด๋ฆ",๊ฐ)์ ์ด์ฉํ๋ค.
- ํฌ์๋ฉ ๋๋ ํ์ด์ง์์๋ request.setAttribute("์ด๋ฆ",๊ฐ)๋
Object getAttribute("์ด๋ฆ") ์ผ๋ก ๊ฐ์ ๋๊ฒจ ๋ฐ๋๋ค.
-->
<form action="ex04_ok.jsp" method="post">
<p>์ด๋ฆ : <input type="text" name="name"></p>
<p>๋์ด : <input type="text" name="age"></p>
<p>
<button type="submit">์ ์กํ๊ธฐ</button>
</p>
</form>
</body>
</html>
<%@ page contentType="text/html; charset=UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
int age = Integer.parseInt(request.getParameter("age"));
String s = age >= 19 ? "์ฑ์ธ" : "๋ฏธ์ฑ๋
์";
request.setAttribute("state", s);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>
<h3>ํฌ์๋ฉํ๋ฉด ์ดํ์ด์ง๋ ๋ณด์ด์ง ์๋๋ค.</h3>
<!-- JSP ์ก์
ํ๊ทธ๋ฅผ ์ด์ฉํ์ฌ ํฌ์๋ฉ -->
<jsp:forward page="ex04_result.jsp"/>
</body>
</html>
<%@ page contentType="text/html; charset=UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
/*
- ํฌ์๋ฉํ๋ฉด ํฌ์๋ฉ ํ์ด์ง์์ request, response ๊ฐ์ฒด๊ฐ ๋์ด ์ค๋ฏ๋ก
ํด๋ผ์ด์ธํธ๊ฐ ๋ณด๋ธ ์ ๋ณด๋ฅผ ํฌ์๋ฉ๋ ํ์ด์ง์์ ๋๊ฒจ ๋ฐ์ ์ ์๋ค.
*/
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
//ํฌ์๋ฉํ ํ์ด์ง์์ ์ค์ ํ ์์ฑ๊ฐ ๊ฐ์ ธ์ค๊ธฐ
//Object request.getAttribute("์ด๋ฆ")
String s= (String)request.getAttribute("state");
//์๋ก ๊ณ ์นจํ๋ฉด DB์ ๊ณ์ ์ ์ฅ๋จ
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>
<h3>์คํ ๊ฒฐ๊ณผ</h3>
<p> ์ด๋ฆ : <%=name %></p>
<p> ๋์ด : <%=age %>, <%=s %></p>
</body>
</html>
'๐ป > JSP' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
(56) [JSP] 2์ผ JSPRequest parameter ๋ฐ ๋ด๋ถ๊ฐ์ฒด (0) | 2024.05.08 |
---|