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

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

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

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

Eclipse

Eclipseは、IBM社で開発された統合開発環境のひとつです。2001年11月にオープンソース化されました。 たくさんのプラグインがあり自由に機能を追加をすることができるため、開発ツールにおける共通プラットフォームとして位置づけられています。 Eclipse自体は、Javaで実装されています。

servlet

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

Q&A

0回答

626閲覧

XSLを指定して、web上に表示させることができない。

hon.ki

総合スコア157

Java

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

Eclipse

Eclipseは、IBM社で開発された統合開発環境のひとつです。2001年11月にオープンソース化されました。 たくさんのプラグインがあり自由に機能を追加をすることができるため、開発ツールにおける共通プラットフォームとして位置づけられています。 Eclipse自体は、Javaで実装されています。

servlet

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

0グッド

0クリップ

投稿2020/01/12 05:49

###環境
・Eclipse IDE for Enterprise Java Developers.

Version: 2019-06 (4.12.0)
Build id: 20190614-1200

・mac os mojive 10.14.4
・Apache Tomcat v8.5[Tomcat8(Java8)]
・db-derby-10.14.2.0

###行った操作
project1という動的プロジェクトを作成し、web.xmlを以下のように設定する。また、Sample6.javaを以下のように記述する。
以下のように記述されたSample2.xslとSample.xmlを、WebContent以下に配置する。
・web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>project1</display-name> <servlet> <servlet-name>Sample6</servlet-name> <servlet-class>Sample6</servlet-class> </servlet> <servlet-mapping> <servlet-name>Sample6</servlet-name> <url-pattern>/servlet/project1.Sample6</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>

・Sample6.java

package project1; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; public class Sample6 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException { try{ //コンテンツタイプの設定 response.setContentType("text/html; charset=UTF-8"); //書き出し PrintWriter pw = response.getWriter(); StreamSource in = new StreamSource("http://localhost:8080/project1/Sample.xml"); StreamSource ss = new StreamSource("http://localhost:8080/project1/Sample2.xsl"); StreamResult out = new StreamResult(pw); TransformerFactory tff = TransformerFactory.newInstance(); Transformer tf = tff.newTransformer(ss); tf.transform(in, out); } catch(Exception e){ e.printStackTrace(); } } }

・Sample.xml

<?xml version="1.0" encoding="UTF-8" ?> <cars><car id="1001" country="日本"><name>乗用車</name><price>150</price><description>5人まで乗車することができます。<em>家族用</em>の車です。</description><img file="car1.jpg"/></car><car id="2001" country="日本"><name>トラック</name><price>500</price><description><em>荷物の運搬</em>にご利用できます。<em>業務用</em>の車です。</description><img file="car2.jpg"/></car><car id="1005" country="USA"><name>オープンカー</name><price>200</price><description>晴天時には天窓を開閉できます。<em>レジャー用</em>に最適です。</description><img file="car3.jpg"/></car></cars>

・Sample2.xsl

<?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" encoding="UTF-8"/> <!-- 文書 --> <xsl:template match="/"> <html> <xsl:apply-templates select="cars"/> </html> </xsl:template> <!-- 車リスト --> <xsl:template match="cars"> <body> <table border="3"> <xsl:apply-templates select="car[price &gt;= 200]"/> </table> </body> </xsl:template> <!-- 車 --> <xsl:template match="car"> <tr> <xsl:apply-templates select="name"/> <xsl:apply-templates select="price"/> <xsl:apply-templates select="img"/> <xsl:apply-templates select="description"/> </tr> </xsl:template> <!-- 品名 --> <xsl:template match="name"> <td> <xsl:value-of select="."/> </td> </xsl:template> <!-- 価格 --> <xsl:template match="price"> <td> <xsl:value-of select="."/>万円 </td> </xsl:template> <!-- 説明 --> <xsl:template match="description"> <td> <xsl:value-of select="."/> </td> </xsl:template> <!-- 図 --> <xsl:template match="img"> <td> <img> <xsl:attribute name="src"> <xsl:text>../</xsl:text> <xsl:value-of select="@file"/> </xsl:attribute> </img> </td> </xsl:template> </xsl:stylesheet>

###期待していた動作
サーバーで実行すると、http://localhost:8080/project1/servlet/project1.Sample6に、トラックとオープンカーについて書いてある表が表示される。

###エラーメッセージ、実際の動作
HTTPステータス 404 - Not Found

Type ステータスレポート

説明 The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

Apache Tomcat/8.5.42

###試してみたこと
web.xmlのurl-patternを、/project1.Sample6に変更しましたが、結果は同じでした。

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

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

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

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

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

dameo

2020/01/13 08:47

(誤)<servlet-class>Sample6</servlet-class> (正)<servlet-class>project1.Sample6</servlet-class>
A-pZ

2020/01/31 13:35

回答は回答欄に入力しましょう
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問