Java Web garbled

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.