An example of jsp value passing Chinese random code problem is introduced

  • 2020-06-03 08:03:37
  • OfStack

In jsp, we often read data from the database and send it back to the client, but we often have messy code during production, so we can use < %request.setCharacterEncoding("UTF-8");% > This method ensures the correct output of Chinese, for example,
Before we catch the value of the form or print out the data from the database, let's put in < %request.setCharacterEncoding("UTF-8");% > Put it in front of them. Then, the form must be submitted post, i.e. method="post", so that it can be closed without confusion. See below:
 
<%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 the user logs in, we need to use the user name or password on one page, we can use the following method to remember, in other pages can be called at will, such as:
 
<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); The password and username are thus remembered, and can be called anywhere else, 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

Related articles: