JSP and Servlet

JSP(4) getRequestDispatcher

Jini0 2018. 8. 20. 11:59
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>sample04</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
   <servlet-name>hello</servlet-name>
   <servlet-class>sample04.HelloServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
   <servlet-name>hello</servlet-name>
   <url-pattern>/hello</url-pattern>
  </servlet-mapping>
  
</web-app>
 
 
 
 
===================================================================
 
 
 
 
 
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
 
<form action="hello" method="get">
 
<table>
<tr>
 <td>이름</td>
 <td>
  <input type="text" size="20" name="name" value="">
 </td>
</tr>
<tr>
 <td>나이</td>
 <td>
  <input type="text" size="5" name="age">  
 </td>
</tr>
<tr>
 <td>좋아하는 과일</td>
 <td>
  <select name="food" size="3" multiple="multiple">
   <option value="apple">사과</option>
   <option value="melon">멜론</option>
   <option value="grape">포도</option>
  </select>
 </td>
</tr>
</table>
<input type="submit" value="전송">
 
</form>
 
</body>
</html>
 
 
 
 
 
 
 
 
==================================================================
 
 
 
 
 
package dto;
 
import java.util.Arrays;
 
public class Human {
 
 private String name;
 private int age;
 private String arrFood[];
 
 public Human() {
 }
 
 public Human(String name, int age, String[] arrFood) {
  super();
  this.name = name;
  this.age = age;
  this.arrFood = arrFood;
 }
 
 public String getName() {
  return name;
 }
 
 public void setName(String name) {
  this.name = name;
 }
 
 public int getAge() {
  return age;
 }
 
 public void setAge(int age) {
  this.age = age;
 }
 
 public String[] getArrFood() {
  return arrFood;
 }
 
 public void setArrFood(String[] arrFood) {
  this.arrFood = arrFood;
 }
 
 @Override
 public String toString() {
  return "Human [name=" + name + ", age=" + age + ", arrFood=" + Arrays.toString(arrFood) + "]";
 }
 
}
 
 
 
 
 
===================================================================
 
 
 
 
 
 
 
 
 
<%@page import="dto.Human"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 
<%
 
Human human = (Human)request.getAttribute("human");
System.out.println(human.toString());
 
String foodArr[] = human.getArrFood(); 
 
%>
 
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
 
이름:<input type="text" value="<%=human.getName() %>">
<br><br>
나이:<input type="text" value="<%=human.getAge() %>">
<br><br>
 
<%
for(int i = 0;i < foodArr.length; i++){
%>
과일:<input type="text" value="<%=foodArr[i] %>">
<br>
<%
}
%>
 
</body>
</html>
 
 
 
 
 
 
 
 
 
 
 
===================================================================
 
 
 
 
 
 
 
 
 
 
 
package sample04;
 
import java.io.IOException;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import dto.Human;
 
public class HelloServlet extends HttpServlet {
 
 @Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  

 

//값들을 얻어오는 곳
  String name = req.getParameter("name");
  String age = req.getParameter("age");  
  String arr[] = req.getParameterValues("food");
  
  System.out.println("name = " + name);
  
  int _age = Integer.parseInt(age.trim());
  
  Human h = new Human(name, _age, arr);
  
  req.setAttribute("human", h);
  
  req.getRequestDispatcher("NewFile.jsp").forward(req, resp);  
 }
 
 @Override
 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  
 }
 
 
 
}
 
 
cs