質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
XML

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

servlet

Servletとは、Webページの動的な生成やデータ処理などをサーバ上で実行するために、Javaで作成されたプログラムです。 ショッピングサイトやオンラインバンキングといった、動的なウェブサイトの構築に用いられています。

Tomcat

TomcatはApache Software Foundation (ASF)で開発されたオープンソースのWebコンテナです。

Q&A

解決済

1回答

29193閲覧

サーブレットにリクエストすると404エラーとなる

Linkey

総合スコア77

XML

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

servlet

Servletとは、Webページの動的な生成やデータ処理などをサーバ上で実行するために、Javaで作成されたプログラムです。 ショッピングサイトやオンラインバンキングといった、動的なウェブサイトの構築に用いられています。

Tomcat

TomcatはApache Software Foundation (ASF)で開発されたオープンソースのWebコンテナです。

0グッド

1クリップ

投稿2017/05/03 03:20

htmlページからサーブレットにリクエストしてページを表示させようとしています。しかし「HTTPステータス 404 - Not Found」が表示されました。

イメージ

発生したエラーについて海外サイトを含めて調べてみましたが有効な解決方法が見つかりませんでした。

htmlページ及びソースコードは以下のように設定しました。

html

1<!DOCTYPE html> 2<html> 3<head> 4<meta charset="UTF-8"> 5<link href="css/image_width.css" rel="stylesheet" type="text/css" 6 media="screen" /> 7<link href="css/button.css" rel="stylesheet" type="text/css" 8 media="screen" /> 9 10<script type="text/javascript"> 11 function exec(){ 12 location.href = "http://localhost:8080/java-rastaurant/ResistrationServlet"; 13 } 14</script> 15<title>いらっしゃいませ!</title> 16</head> 17<body> 18 19 <h1 align="center">ようこそJavaレストランへ!</h1> 20 <marquee>当店は会員制のレストランです。お客様一人一人に最高のサービスを提供いたします。</marquee> 21 <div class="button_wrapper"> 22 <table border="1"> 23 <tr><th>会員登録がまだの方</th><th>会員登録済みの方</th></tr> 24 <tr><td> 25 <button class="color red button" onclick="exec()">会員登録</button> 26 </td> 27 /tr> 28 </table> 29 </div> 30 31</body> 32</html>

Java

1package registration.servlet; 2 3import java.io.IOException; 4import java.io.InputStream; 5import java.io.PrintWriter; 6import java.util.Properties; 7 8import javax.servlet.ServletException; 9import javax.servlet.http.HttpServlet; 10import javax.servlet.http.HttpServletRequest; 11import javax.servlet.http.HttpServletResponse; 12 13/** 14 * ここでは受け渡しのみを実施する。 コントローラーの役割はビジネスロジックのクラスが担当する 15 */ 16public class RegistrationServlet extends HttpServlet { 17 private static final long serialVersionUID = 1L; 18 final static String COMMON_URL = "CommonUrl"; 19 final static String PROPERTY_FILE = "url.properties"; 20 final Properties prop = new Properties(); 21 22 public void init() throws ServletException { 23 24 InputStream inStream = null; 25 try { 26 inStream = RegistrationServlet.class.getClassLoader().getResourceAsStream(PROPERTY_FILE); 27 prop.load(inStream); 28 } catch (IOException e) { 29 System.out.println("プロパティファイルの読み込みに失敗"); 30 e.printStackTrace(); 31 } finally { 32 try { 33 if (inStream != null) { 34 inStream.close(); 35 } 36 } catch (IOException e) { 37 e.printStackTrace(); 38 } 39 } 40 } 41 42 protected void doGet(HttpServletRequest request, HttpServletResponse response) 43 throws ServletException, IOException { 44 //確認用ページ。 45 response.setContentType("text/html;charset=UTF-8"); 46 PrintWriter out = response.getWriter(); 47 out.print("<html><head><title>テストページ</title></head><body>"); 48 out.print("<h1>ようこそ!HelloServletへ</h1>"); 49 out.print("</body></html>"); 50 } 51 52 //入力フォームからのリクエストはdoPostメソッド 53 protected void doPost(HttpServletRequest request, HttpServletResponse response) 54 throws ServletException, IOException { 55 //TODO ログ出力 56 } 57}

XML

1pom.xml 2 3<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 <groupId>com.sample.mvnproject</groupId> 7 <artifactId>java-restaurant</artifactId> 8 <packaging>war</packaging> 9 <version>0.0.1-SNAPSHOT</version> 10 <name>java-restaurant Maven Webapp</name> 11 <url>http://maven.apache.org</url> 12 <dependencies> 13 <dependency> 14 <groupId>junit</groupId> 15 <artifactId>junit</artifactId> 16 <version>3.8.1</version> 17 <scope>test</scope> 18 </dependency> 19 20 <!--<dependency> <groupId>org.glassfish.web</groupId> <artifactId>jstl-impl</artifactId> 21 <version>1.2</version> <exclusions> <exclusion> <groupId>javax.servlet</groupId> 22 <artifactId>servlet-api</artifactId> </exclusion> <exclusion> <groupId>javax.servlet.jsp</groupId> 23 <artifactId>jsp-api</artifactId> </exclusion> <exclusion> <groupId>javax.servlet.jsp.jstl</groupId> 24 <artifactId>jstl-api</artifactId> </exclusion> </exclusions> </dependency> --> 25 26 <!-- added servlet info --> 27 <dependency> 28 <groupId>javax.servlet</groupId> 29 <artifactId>javax.servlet-api</artifactId> 30 <version>3.1.0</version> 31 </dependency> 32 <!-- added JSP info --> 33 <dependency> 34 <groupId>javax.servlet</groupId> 35 <artifactId>jstl</artifactId> 36 <version>1.2</version> 37 </dependency> 38 <!-- added JSTL info --> 39 <dependency> 40 <groupId>javax.servlet.jsp</groupId> 41 <artifactId>javax.servlet.jsp-api</artifactId> 42 <version>2.3.1</version> 43 </dependency> 44 <!-- added mockit for ut --> 45 <dependency> 46 <groupId>org.mockito</groupId> 47 <artifactId>mockito-all</artifactId> 48 <version>1.9.5</version> 49 </dependency> 50 <!-- added Hibernate info --> 51 <dependency> 52 <groupId>org.hibernate</groupId> 53 <artifactId>hibernate-core</artifactId> 54 <version>4.3.0.Final</version> 55 </dependency> 56 <dependency> 57 <groupId>org.hibernate</groupId> 58 <artifactId>hibernate-entitymanager</artifactId> 59 <version>4.3.0.Final</version> 60 </dependency> 61 <dependency> 62 <groupId>org.eclipse.persistence</groupId> 63 <artifactId>org.eclipse.persistence.jpa</artifactId> 64 <version>2.6.4</version> 65 </dependency> 66 <!-- added MySQL Connector Info --> 67 <dependency> 68 <groupId>mysql</groupId> 69 <artifactId>mysql-connector-java</artifactId> 70 <version>5.1.42</version> 71 </dependency> 72 <!-- added log4j info --> 73 <dependency> 74 <groupId>org.apache.logging.log4j</groupId> 75 <artifactId>log4j-core</artifactId> 76 <version>2.7</version> 77 </dependency> 78 <dependency> 79 <groupId>org.apache.logging.log4j</groupId> 80 <artifactId>log4j-api</artifactId> 81 <version>2.7</version> 82 </dependency> 83 <!-- added jBcrypt --> 84 <dependency> 85 <groupId>org.mindrot</groupId> 86 <artifactId>jbcrypt</artifactId> 87 <version>0.4</version> 88 </dependency> 89 <!-- persistence --> 90 </dependencies> 91 <build> 92 <finalName>java-restaurant</finalName> 93 <plugins> 94 <plugin> 95 <groupId>org.apache.maven.plugins</groupId> 96 <artifactId>maven-compiler-plugin</artifactId> 97 <version>3.1</version> 98 <configuration> 99 <!--<source>{java.version}</source>--> 100 <!--<target>${java.version}</target>--> 101 <source>1.8</source> 102 <target>1.8</target> 103 <encoding>UTF-8</encoding> 104 </configuration> 105 </plugin> 106 <!-- added tomcat maven plugin info --> 107 <plugin> 108 <groupId>org.apache.tomcat.maven</groupId> 109 <artifactId>tomcat7-maven-plugin</artifactId> 110 <version>2.2</version> 111 <configuration> 112 <path>/foo</path> 113 <server>tomcat-localhost</server> 114 <url>http://localhost:8080/manager/text</url> 115 </configuration> 116 </plugin> 117 </plugins> 118 </build> 119</project> 120

リクエストするURLに間違えがあるのでしょうか?それともpom.xmlの設定に間違いがあるのでしょうか?
ちなみにmavenプロジェクトで開発をしています。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

まずは ResistrationServlet なのか RegistrationServlet なのかを決めるところからでしょうね。

投稿2017/05/03 04:09

SugiTK

総合スコア495

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Linkey

2017/05/04 00:18

単なる自分の見落としでした。URLはクラス名である「RegistrationServlet」に統一することにしました。ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問