maven + java + vscodeでwebアプリ開発を進めています(初学者です)
試しでservletから画像を出力しようと思っていますが、コンパイル、ビルド後のwarファイルをtomcatサーバ上で実行したところ画像が出力されません。
参考により、src/main/resources
にリソースを配備するものと読み取りましたが、この場合、.javaから画像ファイル等を呼び出す場合のパス指定をどのようにすればよいかご教授いただければ幸いです。
現状、
out.println( " <img src=\"hoge1.jpg\" width=\"250\" height=\"175\"> " );
のような呼び出し方をしていますが正常に出力できていません。
- プロジェクト構成
│ pom.xml │ ├─src │ └─main │ ├─java │ │ └─servlet │ │ SelfIntroduction.java │ │ │ ├─resources │ │ hoge1.jpg │ │ │ └─webapp │ │ index.jsp │ │ │ └─WEB-INF │ web.xml │ └─target │ hoge.war │ ├─classes │ │ hoge1.jpg │ │ │ └─servlet │ SelfIntroduction.class │ │ ├─generated-sources │ └─annotations ├─hoge │ │ index.jsp │ │ │ ├─META-INF │ └─WEB-INF │ │ web.xml │ │ │ ├─classes │ │ │ hoge1.jpg │ │ │ │ │ └─servlet │ │ SelfIntroduction.class │ │ │ │ │ └─lib │ jakarta.servlet-api-5.0.0.jar │ ├─maven-archiver │ pom.properties │ └─maven-status └─maven-compiler-plugin └─compile └─default-compile createdFiles.lst inputFiles.lst
- SelfIntroduction.java
package servlet; import java.io.IOException; import java.io.PrintWriter; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; public class SelfIntroduction extends HttpServlet { private static final long serialVersionUID = 1L; public SelfIntroduction() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.println( "<html> " ); out.println( "<body> " ); out.println( " <img src=\"hoge1.jpg\" width=\"250\" height=\"175\"> " ); out.println( "</body> " ); out.println( "</html> " ); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
- pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>hoge</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>hoge Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>jakarta.servlet</groupId> <artifactId>jakarta.servlet-api</artifactId> <version>5.0.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>hoge</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
- 実行環境
vscode: 1.67.2 apatche-tomcat: 10.0.21 java: 16
あなたの回答
tips
プレビュー