In jsp, we often read data from the database and return data to the client, but we often encounter garbled characters during production, so we can use <% request. setCharacterEncoding ("UTF-8"); %> This method ensures the correct output of Chinese characters. Here is an example,
Before we capture the form value or print the database data, we need to <% request. setCharacterEncoding ("UTF-8"); %> put in front of them, then the form must be submitted in post, that is, method = "post", so that you can close to avoid garbled, see the following:
Copy codeThe Code is as follows:
<% Request. setCharacterEncoding ("UTF-8"); %>
<Form action = "" method = "post">
Name: <input type = "text" name = "name"/>
<Br/>
Gender: <input type = "text" name = "sex"/>
<%
String name = requset. getParameter ("name ");
String sex = request. getParameter ("sex"); out. print (name );
Out. print (sex );
%>
</Form>
Or sometimes, when a user logs in, we need to use the user name or password on a page. We can use the following method to remember that other pages can be called at will, for example:
Copy codeThe Code is as follows:
<Form action = "" method = "post">
Username: <input type = "text" name = "name"/>
<Br/>
Password: <input type = "password" name = "password"/>
<Form/>
String name = requset. getParameter ("name ");
String password = request. getParameter ("password ");
Application. setAttribute ("names", name );
Application. setAttribute ("passwords", password); the password and user name can be remembered. You can also call them as needed, as shown below:
Application. getAttribute ("names ");
Application. getAttribute ("passwords ");
<%
Out. print (application. getAttribute ("names "));
Out. print (application. getAttribute ("passwords ")
%>
In this way, the value of the text box is output.