Home > Java > javaTutorial > Detailed explanation of Java Servlet coding and exception handling

Detailed explanation of Java Servlet coding and exception handling

黄舟
Release: 2017-10-17 09:29:59
Original
1421 people have browsed it

The following editor will bring you an article based on java Servlet coding/exception handling (detailed explanation). The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor and take a look.

1. Servlet outputs Chinese

(1) Why are there garbled characters? ?

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

Note: The form will be used The encoding method used when opening the page is encoded. The server side will use ISO-8859-1 for decoding by default, so garbled characters 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 post method

2)Get request: modify server settings

2. Read the request parameter value

(1)String request.getParameter(String paramName)

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

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

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

a. When there are multiple request parameter names with the same name, it should Use this method.

b. For multi-select boxes, if none is selected, a null value will be obtained.

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

Function

1)Set return The data (mime) type given to the browser; tell the server the data type and encoding returned

2) Set what character set encoding to use when outputting the out.print method

Exception handling: step1. Note Log (keep on-site)

step2. Check whether the exception can be recovered immediately. If it cannot be recovered (for example, the database service stops, the network is interrupted, we can call such an exception a system exception), prompt the user to try again later. Try it (we will use an operation and maintenance sacrifice), and if it can be restored, restore it immediately.

The above is the detailed content of Detailed explanation of Java Servlet coding and exception handling. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template