質問するログイン新規登録

質問編集履歴

13

ソースの編集

2017/01/24 06:24

投稿

n000n00
n000n00

スコア25

title CHANGED
File without changes
body CHANGED
@@ -48,6 +48,7 @@
48
48
  </c:if>
49
49
 
50
50
 
51
+ ```
51
52
  ``````JSP
52
53
  <h1>${param.name}の編集</h1>
53
54
  <form action="/wiki/update">

12

ソースの編集 内容の編集

2017/01/24 06:24

投稿

n000n00
n000n00

スコア25

title CHANGED
File without changes
body CHANGED
@@ -1,3 +1,15 @@
1
+ ◆追記 1/24
2
+ UpdateUserCheck を作成しました。
3
+
4
+ ページを離れた事の判定ですが、
5
+ 現在は、ListServletに戻った時点で、mapからremoveする処理を追加しました。
6
+
7
+ 恐らく、これで対応できると思いますが、
8
+ 一点、編集中の画面をブラウザの閉じるボタンを実施したときの対応について悩んでおります。
9
+
10
+ 引き続き、知恵を貸していただけないでしょうか。
11
+ よろしくお願いします。
12
+
1
13
  ◆追記 1/23
2
14
  Mapを利用した、ソースを書いてみました。UpdateUserCheck_N
3
15
  (まだ、動かしてません)
@@ -18,7 +30,6 @@
18
30
 
19
31
  ◆実現したいこと
20
32
  ログイン後に既存ページを更新中に、別のユーザーから更新ページにアクセスがあった場合に、更新ページに入らせない。
21
-
22
33
  ユーザーIDはsessionで保持。
23
34
  一覧ページから編集したい項目の名前を押して、編集を押すと、パラメータをUsercheckServletに渡し、同一ユーザーかチェックし、同一ユーザーであれば、更新ページ(update.jsp)に飛ぶという流れにしようと思っています。
24
35
 
@@ -28,68 +39,89 @@
28
39
 
29
40
  ※UsercheckServletは編集中のものです。
30
41
 
31
- ```jsp
42
+ ```ここに言語を入力
32
43
  <h1>${wikiPage.name}</h1>
33
44
  ${wikiPage.formatedContent}
34
45
  <hr>
35
46
  <c:if test="${login == 'OK' && wikiPage.name != 'welcome'}">
36
47
  <a href="/wiki/UpdateUserCheck?id=${account.id}&name=${wikiPage.name}&content=${wikiPage.content}">このページを更新</a>
37
48
  </c:if>
49
+
50
+
51
+ ``````JSP
52
+ <h1>${param.name}の編集</h1>
53
+ <form action="/wiki/update">
54
+ <input type="hidden" name="cmd" value="update">
55
+ <input type="hidden" name="name" value="${param.name}">
56
+ <textarea rows="15" cols="60" name="content">${param.content}</textarea>
57
+ ※200文字以内
58
+ <br>
59
+ <input type="submit" value="更新ˆ">
60
+ <input type="submit" value="削除" onclick="cmd.value='delete'">
61
+ <input type="button" value="キャンセル" onclick="location.href='list?name=${param.name}&id=${param.id}'">
62
+ </form>
63
+
38
64
  ```
39
65
  ```Servlet
40
66
  package wiki;
41
67
 
42
68
  import java.io.IOException;
69
+ import java.util.List;
70
+ import java.util.Map;
43
71
 
44
72
  import javax.servlet.ServletContext;
45
73
  import javax.servlet.ServletException;
46
74
  import javax.servlet.http.HttpServlet;
47
75
  import javax.servlet.http.HttpServletRequest;
48
76
  import javax.servlet.http.HttpServletResponse;
49
- import javax.servlet.http.HttpSession;
50
77
 
51
- import Account.AccountBean;
78
+ public class ListServlet extends HttpServlet {
52
79
 
53
- public class UpdateUserCheck extends HttpServlet {
54
-
55
80
  @Override
56
- public synchronized void doGet(HttpServletRequest request, HttpServletResponse response)
81
+ public void doGet(HttpServletRequest request, HttpServletResponse response)
57
82
  throws ServletException, IOException {
58
83
 
59
- HttpSession session = request.getSession(true);
60
84
  ServletContext sc = getServletContext(); // アプリケーションスコープの取得
85
+ String name = request.getParameter("name");
61
86
 
87
+ /* リストページアクセス時に編集ページにアクセスの履歴があれば、処理を実施 */
88
+ if (sc.getAttribute("tmpMap") != null) {
89
+ int id = 0;
90
+ if (request.getParameter("id") == null || request.getParameter("id").isEmpty()) {
91
+ id = -1;
92
+ } else {
62
- WikiBean wiki = (WikiBean) session.getAttribute("wikiPage");
93
+ id = Integer.parseInt(request.getParameter("id"));
63
- AccountBean account = (AccountBean) session.getAttribute("account");
94
+ }
64
95
 
65
- /* 比較のため、アプリケーションスコープ変数がnullなら現在のログインIDとwikiの名前をtmpにセット */
66
- int tmpId = 0;
67
- if (sc.getAttribute("tmpId") == null) {
96
+ Map<String, Integer> map = autoCast(sc.getAttribute("tmpMap"));
97
+ if (map.containsKey(name)) {
98
+ if (map.get(name) == id) {
99
+ map.remove(name);
68
- sc.setAttribute("tmpId", account.getId());
100
+ sc.setAttribute("tmpMap", map);
101
+ }
102
+ }
69
103
  }
70
- tmpId = Integer.parseInt(sc.getAttribute("tmpId").toString());
71
104
 
105
+ try {
72
- String tmpName = null;
106
+ List<WikiBean> list = WikiPageDAO.getInstance().findAll();
73
- if (sc.getAttribute("tmpName") == null) {
107
+ request.setAttribute("list", list);
108
+ } catch (Exception e) {
74
- sc.setAttribute("tmpName", wiki.getName());
109
+ throw new ServletException(e);
75
110
  }
76
- tmpName = sc.getAttribute("tmpName").toString();
77
111
 
78
- /* 同じユーザーかチェックし、異なれば編集中の旨を表示する */
79
- String message = null;
80
- if (tmpId != account.getId() && tmpName.equals(wiki.getName())) {
81
- message = "他のユーザーにて編集中です";
82
- request.setAttribute("message", message);
83
- request.setAttribute("name", wiki.getName());
84
- request.getRequestDispatcher("/list").forward(request, response);
85
- } else {
86
- request.getRequestDispatcher("/wikiView/update.jsp").forward(request, response);
112
+ request.getRequestDispatcher("/wikiView/list.jsp").forward(request, response);
87
- }
88
113
  }
114
+
115
+ /* 戻り値の型に合わせてキャスト */
116
+ @SuppressWarnings("unchecked")
117
+ public static <T> T autoCast(Object obj) {
118
+ T castedObject = (T) obj;
119
+ return castedObject;
120
+ }
89
121
  }
90
122
  ```
91
123
 
92
- ```servlet
124
+ ```Servlet
93
125
  package wiki;
94
126
 
95
127
  import java.io.IOException;
@@ -104,7 +136,7 @@
104
136
 
105
137
  import Account.AccountBean;
106
138
 
107
- public class UpdateUserCheck_N extends HttpServlet {
139
+ public class UpdateUserCheck extends HttpServlet {
108
140
 
109
141
  @Override
110
142
  public synchronized void doGet(HttpServletRequest request, HttpServletResponse response)
@@ -113,49 +145,40 @@
113
145
  HttpSession session = request.getSession(false);
114
146
  ServletContext sc = getServletContext(); // アプリケーションスコープの取得
115
147
 
116
- WikiBean wiki = (WikiBean) session.getAttribute("wikiPage");
148
+ String name = request.getParameter("name"); // 編集中のページ名を取得
117
149
  AccountBean account = (AccountBean) session.getAttribute("account");
118
150
 
119
151
  /* アプリケーションスコープがnullであれば、新しくMapを取得する */
120
152
  HashMap<String, Integer> map = null;
121
- if (sc.getAttribute("map") == null) {
153
+ if (sc.getAttribute("tmpMap") == null) {
122
154
  map = new HashMap<String, Integer>();
123
- map.put(wiki.getName(), account.getId());
124
155
  } else {
125
- map = (HashMap) sc.getAttribute("tmpMap");
156
+ map = autoCast(sc.getAttribute("tmpMap")); // メソッドでキャスト
126
157
  }
127
158
 
128
159
  String message = null;
129
- if (map.containsKey(wiki.getName())) {
160
+ if (map.containsKey(name)) {
130
- if (map.get(wiki.getName()) == account.getId()) {
161
+ if (map.get(name) != account.getId()) {
131
- request.getRequestDispatcher("/wikiView/update.jsp").forward(request, response);
132
- } else {
133
162
  message = "他のユーザーにて編集中です";
134
163
  request.setAttribute("message", message);
135
- request.setAttribute("name", wiki.getName());
136
- request.getRequestDispatcher("/list").forward(request, response);
164
+ request.getRequestDispatcher("list").forward(request, response);
165
+ } else {
166
+ map.put(name, account.getId());
167
+ sc.setAttribute("tmpMap", map);
168
+ request.getRequestDispatcher("/wikiView/update.jsp").forward(request, response);
137
169
  }
138
170
  } else {
171
+ map.put(name, account.getId());
172
+ sc.setAttribute("tmpMap", map);
139
173
  request.getRequestDispatcher("/wikiView/update.jsp").forward(request, response);
140
174
  }
175
+ }
141
176
 
177
+ /* 戻り値の型に合わせてキャスト */
178
+ @SuppressWarnings("unchecked")
142
- sc.setAttribute("map", map); // アプリケーションスコープにセット
179
+ public static <T> T autoCast(Object obj) {
180
+ T castedObject = (T) obj;
181
+ return castedObject;
143
182
  }
144
183
  }
145
-
146
- ```
147
-
148
-
149
- ```jsp
150
- <h1>${param.name}の編集</h1>
151
- <form action="/wiki/update">
152
- <input type="hidden" name="cmd" value="update">
153
- <input type="hidden" name="name" value="${param.name}">
154
- <textarea rows="15" cols="60" name="content">${param.content}</textarea>
155
- ※200文字以内
156
- <br>
157
- <input type="submit" value="更新ˆ">
158
- <input type="submit" value="削除" onclick="cmd.value='delete'">
159
- <input type="button" value="キャンセル" onclick="location.href='/wiki/refer?name=${param.name}">
160
- </form>
161
184
  ```

11

ソースの追加

2017/01/24 06:21

投稿

n000n00
n000n00

スコア25

title CHANGED
File without changes
body CHANGED
@@ -1,3 +1,10 @@
1
+ ◆追記 1/23
2
+ Mapを利用した、ソースを書いてみました。UpdateUserCheck_N
3
+ (まだ、動かしてません)
4
+
5
+ 引き続き、良い方法がないか知恵を貸していただきたいです。
6
+
7
+
1
8
  ◆追記
2
9
  UpdateUserCheckを編集しました。
3
10
  最初にページに入ったログインユーザIDとページネームをtmpに格納して、比較しようと考えています。
@@ -7,7 +14,6 @@
7
14
  ------------------
8
15
 
9
16
 
10
-
11
17
  現在、ServletとJSPで掲示板を作成しています。
12
18
 
13
19
  ◆実現したいこと
@@ -83,7 +89,63 @@
83
89
  }
84
90
  ```
85
91
 
92
+ ```servlet
93
+ package wiki;
86
94
 
95
+ import java.io.IOException;
96
+ import java.util.HashMap;
97
+
98
+ import javax.servlet.ServletContext;
99
+ import javax.servlet.ServletException;
100
+ import javax.servlet.http.HttpServlet;
101
+ import javax.servlet.http.HttpServletRequest;
102
+ import javax.servlet.http.HttpServletResponse;
103
+ import javax.servlet.http.HttpSession;
104
+
105
+ import Account.AccountBean;
106
+
107
+ public class UpdateUserCheck_N extends HttpServlet {
108
+
109
+ @Override
110
+ public synchronized void doGet(HttpServletRequest request, HttpServletResponse response)
111
+ throws ServletException, IOException {
112
+
113
+ HttpSession session = request.getSession(false);
114
+ ServletContext sc = getServletContext(); // アプリケーションスコープの取得
115
+
116
+ WikiBean wiki = (WikiBean) session.getAttribute("wikiPage");
117
+ AccountBean account = (AccountBean) session.getAttribute("account");
118
+
119
+ /* アプリケーションスコープがnullであれば、新しくMapを取得する */
120
+ HashMap<String, Integer> map = null;
121
+ if (sc.getAttribute("map") == null) {
122
+ map = new HashMap<String, Integer>();
123
+ map.put(wiki.getName(), account.getId());
124
+ } else {
125
+ map = (HashMap) sc.getAttribute("tmpMap");
126
+ }
127
+
128
+ String message = null;
129
+ if (map.containsKey(wiki.getName())) {
130
+ if (map.get(wiki.getName()) == account.getId()) {
131
+ request.getRequestDispatcher("/wikiView/update.jsp").forward(request, response);
132
+ } else {
133
+ message = "他のユーザーにて編集中です";
134
+ request.setAttribute("message", message);
135
+ request.setAttribute("name", wiki.getName());
136
+ request.getRequestDispatcher("/list").forward(request, response);
137
+ }
138
+ } else {
139
+ request.getRequestDispatcher("/wikiView/update.jsp").forward(request, response);
140
+ }
141
+
142
+ sc.setAttribute("map", map); // アプリケーションスコープにセット
143
+ }
144
+ }
145
+
146
+ ```
147
+
148
+
87
149
  ```jsp
88
150
  <h1>${param.name}の編集</h1>
89
151
  <form action="/wiki/update">

10

内容を修正

2017/01/23 08:09

投稿

n000n00
n000n00

スコア25

title CHANGED
File without changes
body CHANGED
@@ -1,5 +1,5 @@
1
1
  ◆追記
2
- UsercheckServletを編集しました。
2
+ UpdateUserCheckを編集しました。
3
3
  最初にページに入ったログインユーザIDとページネームをtmpに格納して、比較しようと考えています。
4
4
 
5
5
  途中の段階ですが、引き続き知恵を貸していただけないでしょうか。

9

ソースを編集

2017/01/22 12:06

投稿

n000n00
n000n00

スコア25

title CHANGED
File without changes
body CHANGED
@@ -35,35 +35,48 @@
35
35
 
36
36
  import java.io.IOException;
37
37
 
38
+ import javax.servlet.ServletContext;
38
39
  import javax.servlet.ServletException;
39
40
  import javax.servlet.http.HttpServlet;
40
41
  import javax.servlet.http.HttpServletRequest;
41
42
  import javax.servlet.http.HttpServletResponse;
43
+ import javax.servlet.http.HttpSession;
42
44
 
45
+ import Account.AccountBean;
46
+
43
47
  public class UpdateUserCheck extends HttpServlet {
44
48
 
45
- private static int tmpId;
46
- private static String tmpName;
47
-
48
49
  @Override
49
- public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
50
+ public synchronized void doGet(HttpServletRequest request, HttpServletResponse response)
51
+ throws ServletException, IOException {
50
52
 
51
- System.out.println("22: " + tmpId);
53
+ HttpSession session = request.getSession(true);
52
- System.out.println("23: " + tmpName);
54
+ ServletContext sc = getServletContext(); // アプリケーションスコープの取得
53
55
 
54
- int id = Integer.parseInt(request.getParameter("id").toString());
55
- String name = request.getParameter("name");
56
+ WikiBean wiki = (WikiBean) session.getAttribute("wikiPage");
57
+ AccountBean account = (AccountBean) session.getAttribute("account");
56
58
 
59
+ /* 比較のため、アプリケーションスコープ変数がnullなら現在のログインIDとwikiの名前をtmpにセット */
60
+ int tmpId = 0;
61
+ if (sc.getAttribute("tmpId") == null) {
62
+ sc.setAttribute("tmpId", account.getId());
63
+ }
64
+ tmpId = Integer.parseInt(sc.getAttribute("tmpId").toString());
65
+
66
+ String tmpName = null;
67
+ if (sc.getAttribute("tmpName") == null) {
68
+ sc.setAttribute("tmpName", wiki.getName());
69
+ }
70
+ tmpName = sc.getAttribute("tmpName").toString();
71
+
72
+ /* 同じユーザーかチェックし、異なれば編集中の旨を表示する */
57
73
  String message = null;
58
- if (tmpId != id && tmpName != null && tmpName.equals(name)) {
74
+ if (tmpId != account.getId() && tmpName.equals(wiki.getName())) {
59
75
  message = "他のユーザーにて編集中です";
60
76
  request.setAttribute("message", message);
61
- response.sendRedirect("/wiki/list");
77
+ request.setAttribute("name", wiki.getName());
78
+ request.getRequestDispatcher("/list").forward(request, response);
62
79
  } else {
63
- tmpId = id;
64
- tmpName = name;
65
- System.out.println("41: " + tmpId);
66
- System.out.println("42: " + tmpName);
67
80
  request.getRequestDispatcher("/wikiView/update.jsp").forward(request, response);
68
81
  }
69
82
  }
@@ -73,7 +86,7 @@
73
86
 
74
87
  ```jsp
75
88
  <h1>${param.name}の編集</h1>
76
- <form action="../update">
89
+ <form action="/wiki/update">
77
90
  <input type="hidden" name="cmd" value="update">
78
91
  <input type="hidden" name="name" value="${param.name}">
79
92
  <textarea rows="15" cols="60" name="content">${param.content}</textarea>
@@ -81,6 +94,6 @@
81
94
  <br>
82
95
  <input type="submit" value="更新ˆ">
83
96
  <input type="submit" value="削除" onclick="cmd.value='delete'">
84
- <input type="button" value="キャンセル" onclick="location.href='../refer?name=${param.name}'">
97
+ <input type="button" value="キャンセル" onclick="location.href='/wiki/refer?name=${param.name}">
85
98
  </form>
86
99
  ```

8

ソースの編集

2017/01/22 11:56

投稿

n000n00
n000n00

スコア25

title CHANGED
File without changes
body CHANGED
@@ -27,7 +27,7 @@
27
27
  ${wikiPage.formatedContent}
28
28
  <hr>
29
29
  <c:if test="${login == 'OK' && wikiPage.name != 'welcome'}">
30
- <a href="/wiki/UpdateUserCheck?name=${wikiPage.name}&content=${wikiPage.content}">このページを更新</a>
30
+ <a href="/wiki/UpdateUserCheck?id=${account.id}&name=${wikiPage.name}&content=${wikiPage.content}">このページを更新</a>
31
31
  </c:if>
32
32
  ```
33
33
  ```Servlet

7

内容を編集

2017/01/21 23:15

投稿

n000n00
n000n00

スコア25

title CHANGED
File without changes
body CHANGED
@@ -3,6 +3,7 @@
3
3
  最初にページに入ったログインユーザIDとページネームをtmpに格納して、比較しようと考えています。
4
4
 
5
5
  途中の段階ですが、引き続き知恵を貸していただけないでしょうか。
6
+
6
7
  ------------------
7
8
 
8
9
 

6

内容を修正

2017/01/21 23:09

投稿

n000n00
n000n00

スコア25

title CHANGED
File without changes
body CHANGED
@@ -1,12 +1,12 @@
1
- ◆追記
1
+ ◆追記
2
2
  UsercheckServletを編集しました。
3
3
  最初にページに入ったログインユーザIDとページネームをtmpに格納して、比較しようと考えています。
4
4
 
5
5
  途中の段階ですが、引き続き知恵を貸していただけないでしょうか。
6
+ ------------------
6
7
 
7
8
 
8
9
 
9
-
10
10
  現在、ServletとJSPで掲示板を作成しています。
11
11
 
12
12
  ◆実現したいこと

5

ソースを編集

2017/01/21 23:08

投稿

n000n00
n000n00

スコア25

title CHANGED
File without changes
body CHANGED
@@ -1,3 +1,12 @@
1
+ ◆追記
2
+ UsercheckServletを編集しました。
3
+ 最初にページに入ったログインユーザIDとページネームをtmpに格納して、比較しようと考えています。
4
+
5
+ 途中の段階ですが、引き続き知恵を貸していただけないでしょうか。
6
+
7
+
8
+
9
+
1
10
  現在、ServletとJSPで掲示板を作成しています。
2
11
 
3
12
  ◆実現したいこと
@@ -24,35 +33,37 @@
24
33
  package wiki;
25
34
 
26
35
  import java.io.IOException;
36
+
27
37
  import javax.servlet.ServletException;
28
38
  import javax.servlet.http.HttpServlet;
29
39
  import javax.servlet.http.HttpServletRequest;
30
40
  import javax.servlet.http.HttpServletResponse;
31
- import javax.servlet.http.HttpSession;
32
41
 
33
42
  public class UpdateUserCheck extends HttpServlet {
34
43
 
44
+ private static int tmpId;
45
+ private static String tmpName;
46
+
35
47
  @Override
36
- public synchronized void doGet(HttpServletRequest request, HttpServletResponse response)
48
+ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
37
- throws ServletException, IOException {
38
49
 
39
- HttpSession session = request.getSession(true);
50
+ System.out.println("22: " + tmpId);
51
+ System.out.println("23: " + tmpName);
40
52
 
53
+ int id = Integer.parseInt(request.getParameter("id").toString());
41
- String tmpName = request.getParameter("name");
54
+ String name = request.getParameter("name");
42
- String tmpId = session.getAttribute("id").toString();
43
- String flg = "on";
44
55
 
45
- System.out.println(tmpId);
46
- System.out.println(tmpName);
47
-
48
56
  String message = null;
49
- if (flg.equals("on")) {
50
- if (tmpId == session.getAttribute("id").toString() && tmpName == request.getParameter("name"))
57
+ if (tmpId != id && tmpName != null && tmpName.equals(name)) {
51
- message = "他のユーザーにて編集中です";
58
+ message = "他のユーザーにて編集中です";
52
59
  request.setAttribute("message", message);
53
60
  response.sendRedirect("/wiki/list");
54
61
  } else {
62
+ tmpId = id;
63
+ tmpName = name;
64
+ System.out.println("41: " + tmpId);
65
+ System.out.println("42: " + tmpName);
55
- request.getRequestDispatcher("wikiView/update.jsp").forward(request, response);
66
+ request.getRequestDispatcher("/wikiView/update.jsp").forward(request, response);
56
67
  }
57
68
  }
58
69
  }

4

codeタブの修正

2017/01/21 23:08

投稿

n000n00
n000n00

スコア25

title CHANGED
File without changes
body CHANGED
@@ -30,7 +30,6 @@
30
30
  import javax.servlet.http.HttpServletResponse;
31
31
  import javax.servlet.http.HttpSession;
32
32
 
33
- ```java Servlet
34
33
  public class UpdateUserCheck extends HttpServlet {
35
34
 
36
35
  @Override

3

ソース部分の編集

2017/01/21 11:07

投稿

n000n00
n000n00

スコア25

title CHANGED
File without changes
body CHANGED
@@ -20,7 +20,6 @@
20
20
  <a href="/wiki/UpdateUserCheck?name=${wikiPage.name}&content=${wikiPage.content}">このページを更新</a>
21
21
  </c:if>
22
22
  ```
23
-
24
23
  ```Servlet
25
24
  package wiki;
26
25
 

2

内容の修正

2017/01/21 11:06

投稿

n000n00
n000n00

スコア25

title CHANGED
File without changes
body CHANGED
@@ -21,11 +21,10 @@
21
21
  </c:if>
22
22
  ```
23
23
 
24
+ ```Servlet
24
25
  package wiki;
25
26
 
26
27
  import java.io.IOException;
27
-
28
- ```Servlet
29
28
  import javax.servlet.ServletException;
30
29
  import javax.servlet.http.HttpServlet;
31
30
  import javax.servlet.http.HttpServletRequest;
@@ -60,8 +59,8 @@
60
59
  }
61
60
  }
62
61
  ```
63
- ```
64
62
 
63
+
65
64
  ```jsp
66
65
  <h1>${param.name}の編集</h1>
67
66
  <form action="../update">

1

内容を修正

2017/01/21 11:04

投稿

n000n00
n000n00

スコア25

title CHANGED
File without changes
body CHANGED
@@ -25,6 +25,7 @@
25
25
 
26
26
  import java.io.IOException;
27
27
 
28
+ ```Servlet
28
29
  import javax.servlet.ServletException;
29
30
  import javax.servlet.http.HttpServlet;
30
31
  import javax.servlet.http.HttpServletRequest;
@@ -59,6 +60,7 @@
59
60
  }
60
61
  }
61
62
  ```
63
+ ```
62
64
 
63
65
  ```jsp
64
66
  <h1>${param.name}の編集</h1>