質問編集履歴
3
コードあげてみました。すいません
test
CHANGED
File without changes
|
test
CHANGED
@@ -246,7 +246,7 @@
|
|
246
246
|
|
247
247
|
|
248
248
|
|
249
|
-
<form action="
|
249
|
+
<form action="two.html" method="post">
|
250
250
|
|
251
251
|
氏名:
|
252
252
|
|
2
コードあげてみました。すいません
test
CHANGED
File without changes
|
test
CHANGED
@@ -156,19 +156,165 @@
|
|
156
156
|
|
157
157
|
javabeans?
|
158
158
|
|
159
|
-
|
159
|
+
package emp;
|
160
|
+
|
161
|
+
|
162
|
+
|
160
|
-
|
163
|
+
import java.util.Scanner;
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
public class ContentsFact{
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
private static String baseContents = null;
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
public static String getContents() {
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
if(baseContents == null) {
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
Scanner scanner = new Scanner(ContentsFact.class.getResourceAsStream("one.html"),"UTF-8");
|
190
|
+
|
191
|
+
StringBuilder result = new StringBuilder("");
|
192
|
+
|
193
|
+
while(scanner.hasNextLine()) { String line = scanner.nextLine();
|
194
|
+
|
195
|
+
result.append(line).append("\n");
|
196
|
+
|
197
|
+
}
|
198
|
+
|
199
|
+
baseContents = result.toString();
|
200
|
+
|
201
|
+
scanner.close();
|
202
|
+
|
203
|
+
}
|
204
|
+
|
205
|
+
return new String(baseContents);
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
}
|
210
|
+
|
211
|
+
public static void main(String[]args) {
|
212
|
+
|
213
|
+
String text = ContentsFact.getContents();
|
214
|
+
|
215
|
+
System.out.println(text);
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
|
222
|
+
|
161
|
-
```
|
223
|
+
```one.html
|
224
|
+
|
162
|
-
|
225
|
+
コードpackage emp;
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
<!DOCTYPE html>
|
230
|
+
|
231
|
+
<html>
|
232
|
+
|
233
|
+
<head>
|
234
|
+
|
235
|
+
<meta charset="UTF-8">
|
236
|
+
|
237
|
+
<title>Servlet</title>
|
238
|
+
|
239
|
+
</head>
|
240
|
+
|
241
|
+
<body>
|
242
|
+
|
243
|
+
servlet
|
244
|
+
|
245
|
+
<p/>
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
<form action="page2.html" method="post">
|
250
|
+
|
163
|
-
|
251
|
+
氏名:
|
252
|
+
|
253
|
+
<input type="text" name="name" required value="%oldName%">
|
254
|
+
|
255
|
+
<br/>
|
256
|
+
|
257
|
+
<input type="submit" value="次ページ">
|
258
|
+
|
259
|
+
</form>
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
</body>
|
264
|
+
|
265
|
+
</html>
|
266
|
+
|
267
|
+
|
164
268
|
|
165
269
|
```
|
166
270
|
|
271
|
+
コード```web.xml
|
272
|
+
|
273
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
274
|
+
|
275
|
+
<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" id="WebApp_ID" version="3.1">
|
276
|
+
|
277
|
+
<display-name>PracticeMVC</display-name>
|
278
|
+
|
279
|
+
<servlet>
|
280
|
+
|
281
|
+
<servlet-name>XServlet</servlet-name>
|
282
|
+
|
283
|
+
<servlet-class>emp.XServlet</servlet-class>
|
284
|
+
|
285
|
+
</servlet>
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
<servlet-mapping>
|
290
|
+
|
291
|
+
<servlet-name>XServlet</servlet-name>
|
292
|
+
|
293
|
+
<url-pattern>/xServlet</url-pattern>
|
294
|
+
|
295
|
+
</servlet-mapping>
|
296
|
+
|
297
|
+
</web-app>
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
|
310
|
+
|
167
311
|
### 試したこと
|
168
312
|
|
169
|
-
|
313
|
+
<form action="two.html">にしてみたりしましたがいまいちわかりません
|
314
|
+
|
170
|
-
|
315
|
+
また、two.htmlは、one.htmlと同じくformに入力する感じにしたいです。
|
316
|
+
|
171
|
-
|
317
|
+
two.htmlからブラウザーで戻るとone.htmlに戻るといった内容です
|
172
318
|
|
173
319
|
|
174
320
|
|
1
すいませんMVCの感じで作ってます。ド素人であまり理解できてないのですが、 今高校の部活でプログラミングに取り組んでます
test
CHANGED
File without changes
|
test
CHANGED
@@ -34,17 +34,129 @@
|
|
34
34
|
|
35
35
|
|
36
36
|
|
37
|
-
```
|
37
|
+
```サーブレット
|
38
38
|
|
39
39
|
ソースコード
|
40
40
|
|
41
|
+
package emp;
|
41
42
|
|
43
|
+
import java.io.IOException;
|
44
|
+
|
45
|
+
import java.io.PrintWriter;
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
import javax.servlet.ServletException;
|
50
|
+
|
51
|
+
//import javax.servlet.annotation.WebServlet;
|
52
|
+
|
53
|
+
import javax.servlet.http.HttpServlet;
|
54
|
+
|
55
|
+
import javax.servlet.http.HttpServletRequest;
|
56
|
+
|
57
|
+
import javax.servlet.http.HttpServletResponse;
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
/**
|
70
|
+
|
71
|
+
* Servlet implementation class XServlet
|
72
|
+
|
73
|
+
*/
|
74
|
+
|
75
|
+
//@WebServlet("/XServlet")
|
76
|
+
|
77
|
+
public class XServlet extends HttpServlet {
|
78
|
+
|
79
|
+
private static final long serialVersionUID = 1L;
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
/**
|
84
|
+
|
85
|
+
* @see HttpServlet#HttpServlet()
|
86
|
+
|
87
|
+
*/
|
88
|
+
|
89
|
+
public XServlet() {
|
90
|
+
|
91
|
+
super();
|
92
|
+
|
93
|
+
// TODO Auto-generated constructor stub
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
/**
|
100
|
+
|
101
|
+
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
|
102
|
+
|
103
|
+
*/
|
104
|
+
|
105
|
+
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
|
106
|
+
|
107
|
+
String contents = ContentsFactory.getContents();
|
108
|
+
|
109
|
+
contents = contents.replace("%oldName","");
|
110
|
+
|
111
|
+
out( res, contents );
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
/**
|
120
|
+
|
121
|
+
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
|
122
|
+
|
123
|
+
*/
|
124
|
+
|
125
|
+
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
|
126
|
+
|
127
|
+
String contents = ContentsFactory.getContents();
|
128
|
+
|
129
|
+
String name = req.getParameter("name");
|
130
|
+
|
131
|
+
contents = contents.replace("%oldName",name);
|
132
|
+
|
133
|
+
out( res, contents );
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
private void out(HttpServletResponse res, String contents) throws IOException,ServletException {
|
140
|
+
|
141
|
+
res.setContentType("text/html; charset=UTF-8");
|
142
|
+
|
143
|
+
PrintWriter out = res.getWriter();
|
144
|
+
|
145
|
+
out.println(contents);
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
}
|
42
152
|
|
43
153
|
```
|
44
154
|
|
45
155
|
```ここに言語を```ここに言語を入力
|
46
156
|
|
47
|
-
|
157
|
+
javabeans?
|
158
|
+
|
159
|
+
|
48
160
|
|
49
161
|
```入力
|
50
162
|
|