Ошибка при отправке json запроса: Failed to load resource: the server responded with a status of 500 ()

Запрос на сервлет:

      let Club={
        name:"",
        country:"",
        city:"",
        year:"",
        place:""
    }
    Club.name=document.getElementById("name").value;
    Club.name=document.getElementById("country").value;
    Club.name=document.getElementById("city").value;
    Club.name=document.getElementById("year").value;
    Club.name=document.getElementById("placeUEFA").value;
    const url="hello-servlet"
    function a(){
        let xhr = new XMLHttpRequest();
        xhr.open('POST', url, true)
        xhr.setRequestHeader('Content-Type', 'application/json');
        let jsonob=JSON.stringify(Club)
        xhr.onload = function() {
            if (xhr.status != 200) {
                alert(`Ошибка ${xhr.status}: ${xhr.statusText}`); 
            } else { 
                alert(`Готово, получили ${xhr.response.length} байт`); 
            }
        };
        xhr.send(jsonob)

}

Сервлет:

    package com.example.demo13;

import java.io.*;
import java.lang.reflect.Type;
import java.net.ContentHandler;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

@WebServlet(name = "helloServlet", value = "/hello-servlet")
public class HelloServlet extends HttpServlet {
    private String message;

    public void init() {
        message = "Hello World!";
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.getRequestDispatcher("index.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String country = req.getParameter("country");
        String city = req.getParameter("city");
        int year = Integer.parseInt(req.getParameter("year"));
        int place = Integer.parseInt(req.getParameter("place"));

        SomeData club= new SomeData(name,country,city,year,place);
String json = new Gson().toJson(club);
    String path = "C:\\Users\\User\\IdeaProjects\\demo13\\src\\main\\webapp\\data.json";
        try (PrintWriter out = new PrintWriter(new FileWriter(path))) {
            Gson gson = new Gson();
            String jsonString = gson.toJson(club);
            out.write(jsonString);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void destroy() {
    }
}

В чем может быть ошибка?


Ответы (0 шт):