I wanted to find out what I'm doing wrong .. Not strong in serialization .. The full JSON string is not displayed, it is displayed only up to Field! When transmitting with JS code, you can see that everything is being transmitted. So, bytes are lost somewhere? Who will be able to send using Java.IO will be grateful.

@WebServlet(urlPatterns = "/ContactUs") public class ContactUs extends HttpServlet { protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException { ServletInputStream inputStream = request.getInputStream(); byte[] bytes = inputStream.toString().getBytes(); int number = inputStream.read(bytes); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(number); byteArrayOutputStream.write(bytes,0,number); System.out.println("Converted to string: "+byteArrayOutputStream.toString()); System.out.println("The size of byte array: "+byteArrayOutputStream.size()); System.out.println("Number of bytes was readed: "+number); 

 $("#submit").click(function () { var contactInfo = { email:$("[name=emailField]").val(), userName:$("[name=userName]").val(), field:$("[name=field]").val() } console.log(contactInfo); var JSONString = JSON.stringify(contactInfo); console.log(JSONString); $.ajax({ url:"http://localhost:9090/Demo/ContactUs", method:"post", data:JSONString, contentType:"application/json", error:function (message) { console.log(message); }, success:function (data) { console.log(data); } }); }); 

 [![Вывод в консоле IDEA : Converted to string: {"email":"Ludmila@gmail.com","userName":"Ludmila","field The size of byte array: 56 Number of bytes was readed: 56] 

enter image description here

Attempt with scanner:

  ServletInputStream inputStream = request.getInputStream(); Scanner s = new Scanner(inputStream); String data = s.next(); System.out.println("Scanner: "+data); Вывод: Scanner: {"email":"Ludmila@gmail.com","userName":"Ludmila","field":"Hello! Scanner: {"email":"Aleksander@gmail.com","userName":"Aleksandrovich","field":"Hello! 

enter image description here

Working option:

  StringBuilder stringBuilder = new StringBuilder(request.toString().length()); Scanner scanner = new Scanner(request.getInputStream()); while (scanner.hasNextLine()) { stringBuilder.append(scanner.nextLine()); } String body = stringBuilder.toString(); System.out.println("Scanner: " + body) 

    1 answer 1

    Your attempt with the scanner is incomplete, so try:

     protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException { StringBuilder stringBuilder = new StringBuilder(1000); Scanner scanner = new Scanner(request.getInputStream()); while (scanner.hasNextLine()) { stringBuilder.append(scanner.nextLine()); } String body = stringBuilder.toString(); System.out.println("Scanner: " + body); } 
    • Thank! Gained! - Maks.Burkov