Java servlet based encoding / exception handling (detailed explanation)

1. Servlet output Chinese

(1) Why are there random codes?

out. When println method is output or the form is submitted, the browser will encode the Chinese parameter values in the form;

Note: the encoding method used when the page where the form is located is opened will be used for encoding. By default, iso-8859-1 will be used for decoding on the server, so garbled code will be generated

(2) How to solve it?

1) Post request: requset setcharcterencoding(string charset); Added before all get parameters, and this method only supports the post method

2) Get request: modify server settings

2. Read request parameter value

(1)String request. getParameter(String paramName)

a. If the request parameter name is misspelled, a null value will be obtained.

b. For the form (text input box), if nothing is filled in, you will get "".

(2)String[] request. getParameterValues(String paramName)

a. This method should be used when multiple request parameter names are the same.

b. For multiple selection boxes, if none is selected, a null value is obtained.

Set the output encoding method arg1 setContentType("text/html;charset=utf-8");

effect

1) Set the data (MIME) type returned to the browser; Tell the server the type and encoding of the data returned

2) Set out What character set encoding is used when the print method outputs

Exception handling: step1 Log (keep on site)

step2. Check whether the exception can be recovered immediately. If it cannot be recovered (such as database service stop and network interruption, which can be called system exception), prompt the user to try again later (we need to use one day). If it can be recovered, recover immediately.

The above article based on java servlet coding / exception handling (detailed explanation) is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>