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

質問編集履歴

1

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

2018/11/17 14:21

投稿

takahirooshiro
takahirooshiro

スコア15

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