* 내장객체 ( 동적으로 생성하지 않고 바로 사용가능한 객체 )
- out
- java 코드를 web으로 반영
<%
String title = "제목입니다";
out.println("<h2>" + title + "</h2>");
%>
<h3><%=title %></h3>
동일한 효과를 지닌다.
- request (요청)
- parameter 취득. session 취득. Object를 전송, 취득. encoding 설정
- HttpServletRequest 가 원래 명칭.
- session 명은 절대 한글을 쓰면 안된다! 동작을 안함
<%
String name = request.getParameter("name");
String age = request.getParameter("age");
String hobby[] = request.getParameterValues("hobby");
%>
<h3>name:<%=name%></h3>
<h3>age:<%=age%></h3>
<%
for (int i = 0; i < hobby.length; i++) {
%>
<h3>
취미:<%=hobby[i]%></h3>
<%
}
%>
<%
request.setCharacterEncoding("utf-8"); // encoding 설정
request.getSession().setAttribute("member", "hello"); // session 설정
Object obj = request.getSession().getAttribute("member");
String str = (String) obj;
%>
<h3>str:<%=str%></h3>
- response (응답)
- 이동. 많이 사용하지는 않음
- HttpResponse
<%
response.sendRedirect("default.html");
%>
- session
- 서버에 저장.
<%
// session
request.setAttribute("animal", "dog");
session.setAttribute("human", "홍길동");
response.sendRedirect("default.jsp");
%>
sendRedirect 는 데이터를 전달하지 않기 때문에 session을 이용하여 전달한다.
<%
// session
request.setAttribute("animal", "dog");
session.setAttribute("human", "홍길동");
// response
pageContext.forward("default.jsp");
%>
forward 함수는 데이터를 전달한다.
<%
String animal = (String) request.getAttribute("animal");
String human = (String) session.getAttribute("human");
%>
<p><%=animal %></p>
<p><%=human %></p>
sendRedirect는 animal은 null값, human은 값이 나오지만 forward는 둘 다 값이 나온다.
'WEB' 카테고리의 다른 글
[JSP] JSP 개념 (0) | 2022.06.30 |
---|---|
[Server] servlet - servlet 통신 (0) | 2022.06.30 |
[Server] servlet 개념 (0) | 2022.06.30 |
[JQuery] Jquery란? (0) | 2022.06.29 |
[JSON] JSON이란? (0) | 2022.06.28 |