Имеется класс сервлета(указанный в web.xml), задача которого - вывести таблицу из БД.
public class ResumeServlet extends HttpServlet {
private SqlStorage sqlStorage;
@Override
public void init() {
sqlStorage = new SqlStorage(Config.get().getDbUrl(),
Config.get().getDbUser(),
Config.get().getDbPassword());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
printResume(response);
}
private void printResume(HttpServletResponse response) throws IOException {
List<Resume> list = sqlStorage.getSortedStorage();
PrintWriter writer = response.getWriter();
writer.println("<table>\n" +
"<tbody>\n" +
"<tr>\n" +
"<th>UUID</th>\n" +
"<th>Full Name</th>\n" +
"</tr>");
for (Resume r : list) {
writer.println("<tr>");
writer.println("<td>" + r.getUuid() + "</td>");
writer.println("<td>" + r.getFullName() + "</td>");
writer.println("</tr>");
}
writer.println("</tbody\n" +
"</table>");
}
}
Далее прилагаю сами настройки index.html, web.xml и стектрейс из Tomcat. Проблема - при вызове сервлета вылетают ошибки; но т.к. я в сервлетах новичок - я вообще не пойму, куда здесь нужно копать
//index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Практика Java. Разработка Web приложения.</title>
</head>
<body>
<h2>Приложение вебинара <a href="https://javaops.ru/view/basejava" target="_blank"> "Практика Java. Разработка Web
приложения"</a></h2>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>resumeServlet</servlet-name>
<servlet-class>ru.javawebinar.basejava.web.ResumeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>resumeServlet</servlet-name>
<url-pattern>/resume</url-pattern>
</servlet-mapping>
</web-app>
Ну и собственно сами ошибки:
HTTP Status 500 – Internal Server Error
Type Exception Report
Message Servlet.init() for servlet [resumeServlet] threw exception
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
javax.servlet.ServletException: Servlet.init() for servlet [resumeServlet] threw exception
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:660)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:798)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:808)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:748)
Root Cause
java.lang.NoClassDefFoundError: Could not initialize class ru.javawebinar.basejava.Config
ru.javawebinar.basejava.web.ResumeServlet.init(ResumeServlet.java:20)
javax.servlet.GenericServlet.init(GenericServlet.java:158)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:660)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:798)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:808)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:748)
Note The full stack trace of the root cause is available in the server logs.
В чем я ошибаюсь и что я упустил?
Upd. - добавил код самого конфига. JUnit без проблем подключается через него к БД и тесты проходят; жалуется именно сервлет
public class Config {
private static final File PROPS = new File("config/resumes.properties");
private static final Config INSTANCE = new Config();
private Properties props = new Properties();
private File storageDir;
private String dbUrl;
private String dbUser;
private String dbPassword;
public static Config get() {
return INSTANCE;
}
private Config() {
try (InputStream is = new FileInputStream(PROPS)) {
props.load(is);
storageDir = new File(props.getProperty("storage.dir"));
dbUrl = props.getProperty("db.url");
dbUser = props.getProperty("db.user");
dbPassword = props.getProperty("db.password");
} catch (IOException e) {
throw new IllegalStateException("Invalid config file " + PROPS.getAbsolutePath());
}
}
public File getStorageDir() {
return storageDir;
}
public String getDbUrl() {
return dbUrl;
}
public String getDbUser() {
return dbUser;
}
public String getDbPassword() {
return dbPassword;
}
}