In the project on the Java Web, sometimes the whole project is set to UTF-8 encoding, you will still find the project or garbled.
1. Garbled instances appear
As shown in:
Although in the code, added: Request.setcharacterencoding ("Utf-8"); The results will still be garbled.
/** * Insert Admin */@WebServlet ("/intsertinfo") public class Intsertadmininfoservlet extends HttpServlet {private static final L Ong serialversionuid = 1L; Public Intsertadmininfoservlet () {super (); }protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Updateadmintrueservlet u=new Updateadmintrueservlet ();//TODO auto-generated method stubrequest.setcharacterencoding ("Utf-8"); String loginname=request.getparameter ("LoginName"); String password=request.getparameter ("Password"); String realname=request.getparameter ("Realname"); String lastloginip= "127.0.0.1"; String sql= "INSERT into [dbo]. [Admin] ([Loginname],[password],[realname],[lastloginip]) VALUES (' "+loginname+" ', ' "+password+",
' "+realname+" ', ' "+lastloginip+" ') ", try {sqlhelp sh=new sqlhelp (); Boolean h=sh.insert (SQL); if (!h) Request.getrequestdispatcher ("/listalladmin"). Forward (request, response); else System.out.println ("Insert failed! ");} catch (Exception e) {e.printstacktrace ();}}}2. Solve garbled problems
To solve this problem, we have to define a class with the following code:
/** * Decode String * * @param string * required to transcode String * @param codemode * target format * @return transcoded string */public static Str ing decoder (string string, String Codemode) {try {string s = new String (string.getbytes ("Iso-8859-1"), Codemode); return s; } catch (Exception e) {e.printstacktrace ();} return null;}
Add this method. Then call this method, pass in the decoded string (string) and Codemode (encoded in the way), and you can.
String Lname=decoder (Request.getparameter ("LoginName"), "Utf-8"); String Rname=decoder (Request.getparameter ("Realname"), "Utf-8");
Hope to help you, welcome to praise.
Java Web garbled