質問編集履歴

1

コードを質問本文に記載しました

2021/06/08 03:45

投稿

TK.
TK.

スコア2

test CHANGED
File without changes
test CHANGED
@@ -10,6 +10,10 @@
10
10
 
11
11
 
12
12
 
13
+ ![イメージ説明](2af2001f475223df13669f6fba6205ff.png)
14
+
15
+
16
+
13
17
  Nullになる理由はなぜでしょうか。
14
18
 
15
19
 
@@ -18,4 +22,238 @@
18
22
 
19
23
 
20
24
 
25
+ コードは以下となります。
26
+
27
+ ```Java
28
+
29
+ ・サーブレット
30
+
31
+ @WebServlet("/Main")
32
+
33
+ public class Main extends HttpServlet {
34
+
35
+ private static final long serialVersionUID = 1L;
36
+
37
+
38
+
39
+ /**
40
+
41
+ * @see HttpServlet#HttpServlet()
42
+
43
+ */
44
+
45
+ public Main() {
46
+
47
+ super();
48
+
49
+ // TODO Auto-generated constructor stub
50
+
51
+ }
52
+
53
+
54
+
55
+ /**
56
+
57
+ * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
58
+
59
+ */
60
+
61
+ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
62
+
63
+ // TODO Auto-generated method stub
64
+
65
+ GetMutterListLogic getMutterListLogic = new GetMutterListLogic();
66
+
67
+ List<Mutter> mutterList = getMutterListLogic.execute();
68
+
69
+ request.setAttribute("mutterList", mutterList);
70
+
71
+
72
+
73
+
74
+
75
+ HttpSession session = request.getSession();
76
+
77
+ User loginUser = (User) session.getAttribute("loginUser");
78
+
79
+
80
+
81
+ if(loginUser == null) {
82
+
83
+ response.sendRedirect("/docoTsubu/");
84
+
85
+ }else {
86
+
87
+ RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/main.jsp");
88
+
89
+ dispatcher.forward(request, response);
90
+
91
+ }
92
+
93
+ }
94
+
95
+ /**
96
+
97
+ * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
98
+
99
+ */
100
+
101
+ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
102
+
103
+ // TODO Auto-generated method stub
104
+
21
- ![イメージ説明](074fa4d9c34a410e84d9a7c25ecc9483.png)
105
+ request.setCharacterEncoding("UTF-8");
106
+
107
+ String text = request.getParameter("text");
108
+
109
+
110
+
111
+ if(text !=null && text.length() !=0) {
112
+
113
+
114
+
115
+ HttpSession session = request.getSession();
116
+
117
+ User loginUser = (User)session.getAttribute("loginUser");
118
+
119
+
120
+
121
+ Mutter mutter = new Mutter(loginUser.getName(),text);
122
+
123
+ PostMutterLogic postMutterLogic = new PostMutterLogic();
124
+
125
+ postMutterLogic.execute(mutter);
126
+
127
+
128
+
129
+ } else {
130
+
131
+ request.setAttribute("errorMsg", "入力されていません");
132
+
133
+ }
134
+
135
+ GetMutterListLogic getMutterListLogic = new GetMutterListLogic();
136
+
137
+ List<Mutter> mutterList = getMutterListLogic.execute();
138
+
139
+ request.setAttribute("mutterList", mutterList);
140
+
141
+
142
+
143
+ RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/main.jsp");
144
+
145
+ dispatcher.forward(request, response);
146
+
147
+
148
+
149
+ }
150
+
151
+ }
152
+
153
+
154
+
155
+ ・JSP
156
+
157
+ <%@ page language="java" contentType="text/html; charset=UTF-8"
158
+
159
+ pageEncoding="UTF-8"%>
160
+
161
+ <%@ page import="model.User,model.Mutter,java.util.List" %>
162
+
163
+ <%
164
+
165
+ User loginUser = (User) session.getAttribute("loginUser");
166
+
167
+ List<Mutter>mutterList = (List<Mutter>) application.getAttribute("mutterList");
168
+
169
+ %>
170
+
171
+ <!DOCTYPE html>
172
+
173
+ <html>
174
+
175
+ <head>
176
+
177
+ <meta charset="UTF-8">
178
+
179
+ <title>test</title>
180
+
181
+ </head>
182
+
183
+ <body>
184
+
185
+ <h1>どこつぶメイン</h1>
186
+
187
+ <p>
188
+
189
+ <%= loginUser.getName() %>さん、ログイン中
190
+
191
+ <a href = "/docoTsubu/Logout">ログアウト</a>
192
+
193
+ </p>
194
+
195
+ <p><a href= "/docoTsubu/Main">更新</a></p>
196
+
197
+ <form action = "/docoTsubu/Main" method="post">
198
+
199
+ <input type="text" name = "text">
200
+
201
+ <input type="submit" value= "つぶやく">
202
+
203
+ </form>
204
+
205
+ <% for(Mutter mutter : mutterList) {%>
206
+
207
+ <p><%= mutter.getName() %>:<%= mutter.getText() %></p>
208
+
209
+ <%} %>
210
+
211
+ </body>
212
+
213
+ </html>
214
+
215
+
216
+
217
+ ・クラス
218
+
219
+ public class Mutter implements Serializable{
220
+
221
+
222
+
223
+ private String userName;
224
+
225
+ private String text;
226
+
227
+ private int id;
228
+
229
+
230
+
231
+ public Mutter() {}
232
+
233
+ public Mutter(String userName,String text) {
234
+
235
+ this.userName = userName;
236
+
237
+ this.text = text;
238
+
239
+ }
240
+
241
+ public Mutter(int id,String userName,String text) {
242
+
243
+ this.id = id;
244
+
245
+ this.userName = userName;
246
+
247
+ this.text = text;
248
+
249
+ }
250
+
251
+ public int getId() {return id;}
252
+
253
+ public String getName() {return userName;}
254
+
255
+ public String getText() {return text;}
256
+
257
+ }
258
+
259
+ ```