###環境
・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 >= 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に変更しましたが、結果は同じでした。
あなたの回答
tips
プレビュー