CASE1: 보내는 곳 request/session/application
RequestDispatcher dispatcher=
request.getRequestDispatcher("jsp0126_02.jsp");
dispatcher.forward(request,response);
/*request에 담겨있는걸 reset하지말고 들고가게 해줄려고 */
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>request</title>
</head>
<body>
<%
request.setAttribute("repId", "aaa");
session.setAttribute("sessionId", "easyliving");
application.setAttribute("appId", "easy");
// response.sendRedirect("jsp0126_02.jsp");
RequestDispatcher dispatcher= request.getRequestDispatcher("jsp0126_02.jsp");
dispatcher.forward(request,response);/*request에 담겨있는건 reset하지말고 들고가 */
%>
</body>
</html>
받아오는 법 request/session/application
request:<%= request.getAttribute("repId")%>
${requestScope.repId}
${repId}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>request</title>
</head>
<body>
<h2>
request:<%= request.getAttribute("repId")%>
</h2>
<h2>
session:<%= session.getAttribute("sessionId")%>
</h2>
<h2>
application:<%= application.getAttribute("appId")%>
</h2>
<hr>
<h2>${requestScope.repId}</h2>
<h2>${sessionScope.sessionId}</h2>
<h2>${applicationScope.appId}</h2>
<hr>
<h2>${repId}</h2>
<h2>${sessionId}</h2>
<h2>${appId}</h2>
</body>
</html>
CASE2: 회원객체 리스트를 넣어서 보내기
package com.java.www;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/SendData02")
public class SendData02 extends HttpServlet {
protected void doAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doAction");
request.setCharacterEncoding("UTF-8");
ArrayList<MemberDto> list=new ArrayList<>();
list.add(new MemberDto("aaa","1111","홍길동","010-1111-1111","male","golf"));
list.add(new MemberDto("bbb","1111","유관순","010-2222-2222","female","game"));
list.add(new MemberDto("ccc","1111","이순신","010-3333-3333","male","book"));
request.setAttribute("titleName", "전체회원정보");
request.setAttribute("list", list);
RequestDispatcher dispatcher= request.getRequestDispatcher("jsp0126_03.jsp");
dispatcher.forward(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doGet");
doAction(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doPost");
doAction(request, response);
}
}
회원객체 리스트를 <C:forEach items="${}">로 받아오기
ArrayList<MemberDto> list=new ArrayList<>();
여기서 list로해서 받아 온 객체들을
mdto라는 변수안에 객체를 넣어서 for문으로 가져오고 싶은 내용을 받아온다.
<c:forEach items="${list}" var="mdto">
<tr>
<td>${mdto.id}</td>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>dispatcher</title>
<style>
table{width:920px; margin:50px auto;}
table,th,td{border:1px solid black; border-collaspe: collapse;}
th,td{width:150px; height:40px; text-algin: center;}
</style>
</head>
<body>
<table>
<tr>
<th>아이디</th>
<th>비밀번호</th>
<th>이름</th>
<th>전화번호</th>
<th>성별</th>
<th>취미</th>
</tr>
<c:forEach items="${list}" var="mdto">
<tr>
<td>${mdto.id}</td>
<td>${mdto.pw}</td>
<td>${mdto.name}</td>
<td>${mdto.phone}</td>
<td>${mdto.gender}</td>
<td>${mdto.hobby}</td>
</tr>
</c:forEach>
</table>
</body>
</html>