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 user names are remembered in this way, and can be easily 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