In JSP, we often read data from the database back to the client, but we often in the production of garbled phenomenon, so we can use <%request.setcharacterencoding ("UTF-8");%> this method to ensure the correct output of Chinese, Here's an example,
Before we catch the value of the form or print out the database data, put the <%request.setcharacterencoding ("UTF-8");%> in front of them, and then the form must be submitted by post, that is, method= " Post ", so you can close to avoid garbled, please see below:
Copy Code code as follows:
<%request.setcharacterencoding ("UTF-8");%>
<form action= "" method= "POST" >
Name: <input type= "text" name= "name"/>
<br/>
Sex: <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 the user login, we need to use a user name or password on a page, we can use the following method to remember, in other pages can be arbitrarily called, such as:
Copy Code code as follows:
<form action= "" method= "POST" >
User name: <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); Passwords and usernames are so remembered that they can be arbitrarily invoked in others, as follows
Application.getattribute ("names");
Application.getattribute ("passwords");
<%
Out.print (Application.getattribute ("names"));
Out.print (Application.getattribute ("Passwords")
%>
This will output the value of the text box