Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 74 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,84 @@
<version>1.0-SNAPSHOT</version>
<name>quest Maven Webapp</name>
<url>http://maven.apache.org</url>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- Tomcat 9 поддерживает Servlet 4.0, JSP 2.3 -->
</properties>
<dependencies>
<!-- Servlet 4.0 для Tomcat 9 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>

<!-- JSP 2.3 -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>

<!-- JSTL 1.2 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<!-- JSTL Implementation -->
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jakarta.servlet.jsp.jstl</artifactId>
<version>1.2.6</version>
</dependency>

<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom2</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.42</version>
<scope>provided</scope>
</dependency>
</dependencies>


<build>
<finalName>quest</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>17</source>
<target>17</target>
<compilerArgs>--enable-preview</compilerArgs>
</configuration>
</plugin>

<!-- Tomcat 9 Maven Plugin -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<path>/quest</path>
<uriEncoding>UTF-8</uriEncoding>
<contextReloadable>true</contextReloadable>
<!-- Для Tomcat 9 нужны дополнительные настройки -->
</configuration>
</plugin>
</plugins>
</build>
</project>
56 changes: 56 additions & 0 deletions src/main/BookPageServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/books")
public class BookPageServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

HttpSession session = req.getSession();
String playerName = (String) session.getAttribute("playerName");
// Если имя не введено, возвращаем на страницу ввода
if (playerName == null || playerName.trim().isEmpty()) {
resp.sendRedirect("name.jsp");
return;
}
// Получаем список книг
List<String> bookTitles = getBookTitles(req);
// Передаем данные в JSP
req.setAttribute("playerName", playerName);
req.setAttribute("bookTitles", bookTitles);
req.getRequestDispatcher("/books.jsp").forward(req, resp);
}

private List<String> getBookTitles(HttpServletRequest req) {
List<String> titles = new ArrayList<>();

// Путь относительно webapp
String booksPath = req.getServletContext().getRealPath("/books");
File booksDir = new File(booksPath);

if (booksDir.exists() && booksDir.isDirectory()) {
File[] files = booksDir.listFiles((dir, name) -> name.endsWith(".fb2"));

if (files != null) {
for (File file : files) {
String title = file.getName().replace(".fb2", "");
titles.add(title);
}
}
}

return titles;
}
}
Loading