症状
サーブレットを2つ作ると
StandardWrapperValve[sql]: Allocate exception for servlet sql
java.lang.ClassNotFoundException: jakarta.servlet.http.HttpServletRequest
といったエラーを返し404エラーを返し2つ目のサーブレットが認識されません。
ですが、war上ではしっかり認識されているようです。
payaraのバージョンは5.0です。
ファイル構造はこの通りです。
filetree
1practice. 2├─.vscode 3├─artifact 4├─src 5│ ├─main 6│ │ ├─java 7│ │ │ ├─com 8│ │ │ │ └─example 9│ │ │ │ ├─config 10│ │ │ │ └─service 11│ │ │ └─javax 12│ │ │ └─ws 13│ │ │ └─rs 14│ │ │ └─core 15│ │ ├─resources 16│ │ └─webapp 17│ │ ├─com 18│ │ │ └─example 19│ │ │ └─practice 20│ │ │ └─1 21│ │ └─WEB-INF 22│ └─test 23│ ├─java 24│ │ └─com 25│ │ └─example 26│ └─resources 27└─target 28 ├─classes 29 │ ├─com 30 │ │ └─example 31 │ │ ├─config 32 │ │ └─service 33 │ └─javax 34 │ └─ws 35 │ └─rs 36 │ └─core 37 ├─generated-sources 38 │ └─annotations 39 ├─maven-archiver 40 ├─maven-status 41 │ └─maven-compiler-plugin 42 │ ├─compile 43 │ │ └─default-compile 44 │ └─testCompile 45 │ └─default-testCompile 46 ├─practice 47 │ ├─com 48 │ │ └─example 49 │ │ └─practice 50 │ │ └─1 51 │ ├─META-INF 52 │ └─WEB-INF 53 │ ├─classes 54 │ │ ├─com 55 │ │ │ └─example 56 │ │ │ ├─config 57 │ │ │ └─service 58 │ │ └─javax 59 │ │ └─ws 60 │ │ └─rs 61 │ │ └─core 62 │ └─lib 63 └─test-classes 64 └─com 65 └─example
コード
pom.xml
1<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>com.example</groupId> 5 <artifactId>practice</artifactId> 6 <version>1</version> 7 <packaging>war</packaging> 8 <dependencies> 9 <dependency> 10 <groupId>jakarta.platform</groupId> 11 <artifactId>jakarta.jakartaee-api</artifactId> 12 <version>8.0.0</version> 13 </dependency> 14 <dependency> 15 <groupId>jakarta.platform</groupId> 16 <artifactId>jakarta.jakartaee-web-api</artifactId> 17 <version>9.0.0</version> 18 <scope>provided</scope> 19 </dependency> 20 21 <dependency> 22 <groupId>org.eclipse.persistence</groupId> 23 <artifactId>org.eclipse.persistence.core</artifactId> 24 <version>2.7.7</version> 25 </dependency> 26 <dependency> 27 <groupId>org.eclipse.persistence</groupId> 28 <artifactId>org.eclipse.persistence.asm</artifactId> 29 <version>2.7.7</version> 30 </dependency> 31 <dependency> 32 <groupId>org.eclipse.persistence</groupId> 33 <artifactId>org.eclipse.persistence.antlr</artifactId> 34 <version>2.7.7</version> 35 </dependency> 36 <dependency> 37 <groupId>org.eclipse.persistence</groupId> 38 <artifactId>org.eclipse.persistence.jpa</artifactId> 39 <version>2.7.7</version> 40 </dependency> 41 <dependency> 42 <groupId>org.eclipse.persistence</groupId> 43 <artifactId>org.eclipse.persistence.jpa.jpql</artifactId> 44 <version>2.7.7</version> 45 </dependency> 46 <dependency> 47 <groupId>org.eclipse.persistence</groupId> 48 <artifactId>org.eclipse.persistence.moxy</artifactId> 49 <version>2.7.7</version> 50 </dependency> 51 <dependency> 52 <groupId>org.eclipse.persistence</groupId> 53 <artifactId>javax.persistence</artifactId> 54 <version>2.2.1</version> 55 </dependency> 56 </dependencies> 57 <build> 58 <finalName>practice</finalName> 59 <plugins> 60 <plugin> 61 <groupId>org.apache.maven.plugins</groupId> 62 <artifactId>maven-war-plugin</artifactId> 63 <version>3.2.3</version> 64 <configuration> 65 <failOnMissingWebXml>false</failOnMissingWebXml> 66 <warName>${project.build.finalName}</warName> 67 <outputDirectory>artifact</outputDirectory> 68 </configuration> 69 </plugin> 70 </plugins> 71 </build> 72 <properties> 73 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 74 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 75 <maven.compiler.source>11</maven.compiler.source> 76 <maven.compiler.target>11</maven.compiler.target> 77 <failOnMissingWebXml>false</failOnMissingWebXml> 78 </properties> 79 <distributionManagement> 80 <repository> 81 <id>myServlet</id> 82 <name>myServlet</name> 83 <url>file:///src/main/webapp/</url> 84 </repository> 85 </distributionManagement> 86</project>
web.xml
1<?xml version="1.0" encoding="UTF-8"?> 2<web-app xmlns="http://java.sun.com/xml/ns/javaee" 3 version="2.5" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 6 7 <servlet> 8 <servlet-name>test</servlet-name> 9 <servlet-class>com.example.service.TestService</servlet-class> 10 </servlet> 11 <servlet> 12 <servlet-name>sql</servlet-name> 13 <servlet-class>com.example.service.SQLtest</servlet-class> 14 </servlet> 15 16 17 <servlet-mapping> 18 <servlet-name>test</servlet-name> 19 <url-pattern>/test</url-pattern> 20 </servlet-mapping> 21 <servlet-mapping> 22 <servlet-name>sql</servlet-name> 23 <url-pattern>/sql</url-pattern> 24 </servlet-mapping> 25</web-app>
TestService.java
1package com.example.service; 2 3import java.io.IOException; 4import java.io.PrintWriter; 5import java.sql.SQLException; 6 7import javax.servlet.ServletException; 8import javax.servlet.http.HttpServlet; 9import javax.servlet.http.HttpServletRequest; 10import javax.servlet.http.HttpServletResponse; 11 12public class TestService extends HttpServlet { 13 protected void doGet(HttpServletRequest request, HttpServletResponse response) 14 throws ServletException, IOException { 15 response.setContentType("text/html; charset=UTF-8"); 16 PrintWriter out = response.getWriter(); 17 try { 18 out.println(createHTML("GET", "null")); 19 } catch (SQLException e) { 20 StringBuffer sb = new StringBuffer(); 21 22 sb.append(e); 23 e.printStackTrace(); 24 25 } 26 out.close(); 27 } 28 29 protected void doPost(HttpServletRequest request, HttpServletResponse response) 30 throws ServletException, IOException { 31 response.setContentType("text/html; charset=UTF-8"); 32 PrintWriter out = response.getWriter(); 33 try { 34 out.println(createHTML("POST", request.getParameter("posttext"))); 35 } catch (SQLException e) { 36 37 StringBuffer sb = new StringBuffer(); 38 39 sb.append(e); 40 e.printStackTrace(); 41 42 } 43 out.close(); 44 } 45 46 protected String createHTML(String methodType, String textString) throws SQLException { 47 StringBuffer sb = new StringBuffer(); 48 String text = textString; 49 50 sb.append("<html>"); 51 sb.append("<head>"); 52 sb.append("<title>サンプル</title>"); 53 sb.append("<link rel=\"stylesheet\" href=\"style.css\">"); 54 sb.append("</head>"); 55 sb.append("<body>"); 56 57 sb.append("<p>"); 58 sb.append(methodType); 59 sb.append("メソッドで呼び出されました</p>"); 60 61 sb.append("<p><a href=\"/practice/test\">リンク</a></p>"); 62 sb.append("<p>" + text + "</p>"); 63 64 sb.append("<form action=\"/practice/test\" method=\"get\">"); 65 sb.append("<input type=\"submit\" value=\"GETで送信\">"); 66 sb.append("</form>"); 67 68 sb.append("<form action=\"/practice/test\" method=\"post\">"); 69 sb.append("<input type=\"text\" name=\"posttext\">"); 70 sb.append("<input type=\"submit\" value=\"POSTで送信\">"); 71 sb.append("</form>"); 72 73 sb.append("</body>"); 74 sb.append("</html>"); 75 76 return (new String(sb)); 77 } 78 79}
SQLtest.java
1package com.example.service; 2 3import java.io.IOException; 4import java.io.PrintWriter; 5import jakarta.servlet.http.HttpServletRequest; 6import jakarta.servlet.http.HttpServletResponse; 7 8public class SQLtest { 9 // @Resource(lookup = "java:global/jdbc/MySQL") 10 // public DataSource source; 11 12 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { 13 response.setContentType("text/html; charset=UTF-8"); 14 15 PrintWriter out = response.getWriter(); 16 out.append("this page is only opened by Doget"); 17 out.close(); 18 } 19 20 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { 21 response.setContentType("text/html; charset=UTF-8"); 22 23 PrintWriter out = response.getWriter(); 24 out.append("this page is only opened by DoPost"); 25 out.close(); 26 } 27}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。