javaweb submit form to servlet prompt download

     I recently learned javaweb, and I encountered an interesting error when writing code. Now I will share it with you.

When I was writing and submitting a form to a servlet, I didn't know what was going on. After submitting the form, it was automatically prompted to download the file. When I got the browser to run it, the code was garbled. It is estimated that there is an error in response.setContentType().


  The form submission page is correct, and the servlet receives the data is not wrong, very strange! Later, I found similar codes that I had written before and checked them carefully, and found that the code I was writing to prevent garbled characters was a little different:

response.setContentType("charset=utf-8");

Correct spelling:

response.setContentType("text/html;charset=utf-8");

  The reason is that when a user requests a network resource, it receives an httpresponse response. The response header contains congtent-type and other information, indicating which way the browser should handle the information in the content. Prompt download instructions content-type indicates not a format that browsers can handle. We have to write the format correctly, programming is a sloppy thing.


  Last friendly reminder:

// Make sure to recognize Chinese in the request
request.setCharacterEncoding("utf-8");
//Ensure that the response output Chinese is not messy
response.setContentType("text/html;charset=utf-8");


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325599614&siteId=291194637