Перепиши jsp так чтобы не было java кода внутри. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>--%>

<html>
<head>
    <title>Chat</title>
    <style>
        #chat-box {
            border: 1px solid #ccc;
            padding: 10px;
            width: 300px;
            height: 300px;
            overflow-y: scroll;
            resize: both; /* Enables resizing both vertically and horizontally */
        }

        #message-form {
            margin-top: 10px;
        }

        #message-input {
            width: calc(100% - 10px); /* Width adjustment for padding */
            padding: 5px;
            resize: horizontal; /* Enables resizing horizontally */
            overflow: auto; /* Ensures proper text rendering */
        }
    </style>
    <script>
        document.addEventListener("DOMContentLoaded", function () {
            const chatId = new URLSearchParams(window.location.search).get('ID_CHAT');
            const chatBox = document.getElementById('chat-box');

            // Function to fetch chat data
            function fetchChatData() {
                fetch(`/api/chat/${chatId}`)
                    .then(response => response.json())
                    .then(data => {
                        document.title = `Чат: ${data.idChat}`;
                        renderMessages(data.messages);
                    })
                    .catch(error => console.error('Error fetching chat data:', error));
            }

            // Function to render messages
            function renderMessages(messages) {
                chatBox.innerHTML = ''; // clear previous messages
                messages.forEach(function (message) {
                    const messageElement = document.createElement('div');
                    messageElement.innerHTML = `<strong>${message.username}:</strong> ${message.content}`;
                    chatBox.appendChild(messageElement);
                });
            }

            // Fetch chat data on page load
            fetchChatData();

            // Handle message form submission
            document.getElementById('message-form').addEventListener('submit', function (event) {
                event.preventDefault();

                const content = document.getElementById('message-input').value;

                fetch(`/api/chat/${chatId}/messages`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ content })
                })
                    .then(response => response.json())
                    .then(data => {
                        renderMessages(data.messages);
                        document.getElementById('message-input').value = ''; // clear input field
                    })
                    .catch(error => console.error('Error sending message:', error));
            });
        });
    </script>
</head>
<body>
<h1 id="chat-title">Чат</h1>
<div id="chat-box"></div>
<form id="message-form">
    <label>
        <textarea id="message-input" name="content" placeholder="Введите сообщение" required></textarea>
    </label>
    <button type="submit">Отправить</button>
</form>
</body>
</html> Вот Servlet обрабатывающий его package org.webchat.servlets;

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 org.webchat.domain.Chat;
import org.webchat.domain.Message;
import org.webchat.domain.User;
import org.webchat.repository.ChatRepoImpl;

import java.io.IOException;
import java.util.Optional;

@WebServlet(name = "ChatServlet", value = "/chat")

public class ChatServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        String idChat = request.getParameter("ID_CHAT");
        Optional<Chat> chatOptional= ChatRepoImpl.getChat(idChat);

        if (chatOptional.isEmpty()){
            return;
        }

        Chat thisChat = ChatRepoImpl.getChat(idChat).get();
        if (request.getSession().getAttribute("user") == null) {
            response.sendRedirect(request.getContextPath() + "/profile");
            return;
        }
        request.setAttribute("chat", thisChat);
        request.getRequestDispatcher("/chat.jsp").forward(request, response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String author = ((User) request.getSession().getAttribute("user")).getId();
        String content = request.getParameter("content");
        if (author != null && content != null && !author.trim().isEmpty() && !content.trim().isEmpty()) {
            Message newMessage = new Message(author, content);
            ChatRepoImpl.addMessage(request.getParameter("ID_CHAT"), newMessage);
            response.sendRedirect(request.getContextPath() + "/chat?ID_CHAT=" + request.getParameter("ID_CHAT"));
        }
    }
}