From b8053f1e84cc74f3ea5f7aaf4a7024e1dfa174e5 Mon Sep 17 00:00:00 2001 From: Kirill Buslovskii Date: Mon, 12 Jan 2026 23:21:08 +0300 Subject: [PATCH 1/2] create InitServlet and LogicServlet, read some logic. some feature in index.jsp and pom --- pom.xml | 22 +++++---- src/main/java/com/tictactoe/InitServlet.java | 30 ++++++++++++ src/main/java/com/tictactoe/LogicServlet.java | 47 +++++++++++++++++++ src/main/webapp/index.jsp | 21 ++++++++- src/main/webapp/static/main.css | 11 +++++ 5 files changed, 122 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/tictactoe/InitServlet.java create mode 100644 src/main/java/com/tictactoe/LogicServlet.java diff --git a/pom.xml b/pom.xml index cb226e88..6bedcc42 100644 --- a/pom.xml +++ b/pom.xml @@ -12,21 +12,27 @@ UTF-8 - 18 - 18 + 21 + 21 + - javax.servlet - javax.servlet-api - 4.0.1 + jakarta.servlet + jakarta.servlet-api + 6.0.0 provided - javax.servlet - jstl - 1.2 + jakarta.servlet.jsp.jstl + jakarta.servlet.jsp.jstl-api + 3.0.0 + + + org.glassfish.web + jakarta.servlet.jsp.jstl + 3.0.1 diff --git a/src/main/java/com/tictactoe/InitServlet.java b/src/main/java/com/tictactoe/InitServlet.java new file mode 100644 index 00000000..9e44c1e4 --- /dev/null +++ b/src/main/java/com/tictactoe/InitServlet.java @@ -0,0 +1,30 @@ +package com.tictactoe; + +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpSession; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +@WebServlet(name = "InitServlet", value = "/start") +public class InitServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + HttpSession currentSession = req.getSession(true); + + Field field = new Field(); + Map fieldData = field.getField(); + + List data = field.getFieldData(); + + currentSession.setAttribute("field", field); + currentSession.setAttribute("data", data); + + getServletContext().getRequestDispatcher("/index.jsp").forward(req, resp); + } +} diff --git a/src/main/java/com/tictactoe/LogicServlet.java b/src/main/java/com/tictactoe/LogicServlet.java new file mode 100644 index 00000000..979bbaed --- /dev/null +++ b/src/main/java/com/tictactoe/LogicServlet.java @@ -0,0 +1,47 @@ +package com.tictactoe; + +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpSession; + +import java.io.IOException; +import java.util.List; + +@WebServlet(name = "LogicServlet", value = "/logic") +public class LogicServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + HttpSession currentSession = req.getSession(); + + Field field = extractField(currentSession); + + int index = getSelectedIndex(req); + + field.getField().put(index, Sign.CROSS); + + List data = field.getFieldData(); + + currentSession.setAttribute("data", data); + currentSession.setAttribute("field", field); + + resp.sendRedirect("/index.jsp"); + } + + private Field extractField(HttpSession currentSession) { + Object fieldAttribute = currentSession.getAttribute("field"); + if (Field.class != fieldAttribute.getClass()) { + currentSession.invalidate(); + throw new RuntimeException("Session is broken, try one more time"); + } + return (Field) fieldAttribute; + } + + private int getSelectedIndex(HttpServletRequest request) { + String click = request.getParameter("click"); + boolean isNumeric = click.chars().allMatch(Character::isDigit); + return isNumeric ? Integer.parseInt(click) : 0; + } +} diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp index 964cc071..89371816 100644 --- a/src/main/webapp/index.jsp +++ b/src/main/webapp/index.jsp @@ -4,10 +4,29 @@ Tic-Tac-Toe + + + <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

Tic-Tac-Toe

- + + + + + + + + + + + + + + + + +
${data.get(0).getSign()}${data.get(1).getSign()}${data.get(2).getSign()}
${data.get(3).getSign()}${data.get(4).getSign()}${data.get(5).getSign()}
${data.get(6).getSign()}${data.get(7).getSign()}${data.get(8).getSign()}

Tic-Tac-Toe

@@ -27,9 +29,36 @@ ${data.get(8).getSign()} +
+ + -