package ru.job4j.servlets;

import ru.job4j.servlets.userstore.User;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.*;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;
import com.fasterxml.jackson.databind.ObjectMapper;

public class ItemsController extends HttpServlet {

    private final static String baseFile = "user.json";
    ConcurrentHashMap<String, User> map = new ConcurrentHashMap<>();

    public static void toJSON(User user) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValue(new File(baseFile), user);
        System.out.println("json created!");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        req.getRequestDispatcher("/WEB-INF/jsp/Items.html").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ObjectMapper mapper = new ObjectMapper();
        User user = new User(0, "name", "login", req.getParameter("email"), new Date(), req.getParameter("pwd"), 1);
        toJSON(user);
        map.put(req.getParameter("pwd"), mapper.readValue(new File(baseFile), User.class));


    }
}

@@ -749,53 +749,8 @@
 r);%0A
-        System.out.println(%22json created!%22);%0A
     
@@ -914,20 +914,20 @@
 e(%22text/
-html
+json
 %22);%0A    
@@ -1010,405 +1010,518 @@
     
-%7D%0A%0A    @Override%0A    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException %7B%0A        ObjectMapper mapper = new ObjectMapper();%0A        User user = new User(0, %22name%22, %22login%22, req.getParameter(%22email%22), new Date(), req.getParameter(%22pwd%22), 1);%0A        toJSON(user);%0A        map.put(req.getParameter(%22pwd%22), mapper.readValue(new File(baseFile)
+    PrintWriter writer = new PrintWriter(resp.getOutputStream());%0A        ObjectMapper mapper = new ObjectMapper();%0A        String json = mapper.writeValueAsString(map);%0A        writer.append(json);%0A        writer.flush();%0A    %7D%0A%0A    @Override%0A    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException %7B%0A        ObjectMapper mapper = new ObjectMapper();%0A        BufferedReader reader = req.getReader();%0A        final User user = mapper.convertValue(reader
 , Us
@@ -1529,20 +1529,17 @@
 r.class)
-);%0A%0A
+;
 %0A    %7D%0A%7D

task #58534