質問編集履歴

1

別のコードに変えました。詳しく書きなおしました。

2018/11/17 14:21

投稿

takahirooshiro
takahirooshiro

スコア15

test CHANGED
File without changes
test CHANGED
@@ -1,71 +1,79 @@
1
- 初心者です。eclipseでjavaサーブレットプログラムを書いているのですが、次のコードが実行できません。
1
+ プログラミング初心者で申し訳ないです。eclipseでjavaサーブレットプログラムを書いているのですが、次のコードが実行できません。
2
+
3
+ package名 sample クラス名 Sample1
4
+
5
+ ```ここに言語を入力
6
+
7
+ package sample;
8
+
9
+ import java.io.IOException;
10
+
11
+ import java.io.PrintWriter;
2
12
 
3
13
 
4
14
 
5
- ```ここに言語を入力
15
+ import javax.servlet.ServletException;
6
16
 
7
- public void doGet(HttpServletRequest request,
17
+ import javax.servlet.annotation.WebServlet;
8
18
 
9
- HttpServletResponse response)
19
+ import javax.servlet.http.HttpServlet;
10
20
 
11
- throws ServletException
21
+ import javax.servlet.http.HttpServletRequest;
12
22
 
13
- {
14
-
15
- final long serialVersionUID = 1L;
23
+ import javax.servlet.http.HttpServletResponse;
16
-
17
- try {
18
-
19
- response.setContentType("text/html; charset = UTF-8");
20
24
 
21
25
 
22
26
 
27
+ @WebServlet("/Sample1")
28
+
29
+ public class Sample1 extends HttpServlet{
30
+
31
+ public void doGet(HttpServletRequest request, HttpServletResponse response)
32
+
23
- Date dt = new Date();
33
+ throws IOException, ServletException{
24
34
 
25
35
 
26
36
 
27
- PrintWriter pw = response.getWriter();
37
+ response.setContentType("text/html");
28
38
 
29
- pw.println("<!DOCTYPE html><html>\n"
39
+ PrintWriter out = response.getWriter();
30
40
 
31
- + "<head><title>サンプル</title></head>\n"
32
41
 
33
- + "<body><div style=\"text-alogn: center;\">\n"
34
42
 
35
- + "<h2>ようこそ</h2>"
43
+ out.println("<html>");
36
44
 
37
- + "<hr/>\n"
45
+ out.println("<head>");
38
46
 
39
- + "今" + dt + "です。<br/>\n"
47
+ out.println("<title>Hello World!</title>");
40
48
 
41
- + "お選びください。<br/>\n"
49
+ out.println("</head>");
42
50
 
43
- + "<br/>\n"
51
+ out.println("<body>");
44
52
 
45
- + "<a href=\"../car1.html\">乗車用)</a><br/>\n"
53
+ out.println("<h1>Hello World!</h1>");
46
54
 
47
- + "<a href=\"../car2.html\">トラック</a><br/>\n"
55
+ out.println("</body>");
48
56
 
49
- + "<a href=\"../car3.html\">オープンカー</a><br/>\n"
57
+ out.println("</html>");
50
58
 
51
- + "</div></body>\n"
52
59
 
53
- + "</html>\n");
54
60
 
55
- }
61
+ out.close();
56
62
 
57
- catch(Exception e) {
63
+ }
58
64
 
59
- e.printStackTrace();
60
-
61
- }
65
+ }
62
-
63
- }
64
66
 
65
67
  ```
66
68
 
69
+ エラーの説明
70
+
67
- ちなみに、" 404notfound The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. " というエラーが出ます。
71
+ 404-notfound  /sample/Sample1  The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
68
72
 
69
73
 
70
74
 
71
- ちなみにほかの簡単なサーブレットプログラムだと実行できました。よろしくお願いします。
75
+ アクセスしたURL http://localhost:8080/sample/Sample1
76
+
77
+
78
+
79
+ 動的プロジェクトでプロジェクトを作成して、サーバーを追加して、サーバーで実行で実行しました。初心者なのでよくわからず、サーバーの設定などはあまりいじってません。とあるサイトの解説どおりにやってみました。なにか修正する必要がありましたら、コメントでよろしくお願いします。