How to write a servlet which can transfer data written in ISO8859-1 into GB2312 or another kind of character encoding?
Created May 4, 2012
Jonhi Ma You can use this method to transfer your data from one standard encoding to another one. I have written it out and tested it under JSDK 2.0, and it runs well. The following is the java code I have written.
import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class commonServlet extends HttpServlet { /*the method of Convert transfer data in typeof String (String Input) from one format(StandardFrom) to another format(StandardTo)*/ public static String convert (String Input, String StandardFrom, String StandardTo) { byte[] bytes; String conResult = new String(); try { bytes = Input.getBytes(StandardFrom); conResult = new String (bytes, StandardTo); } catch(Exception e) { //debugging begins here System.out.println(e); //debugging ends here return("null"); } return conResult; }//method convert ends here //Do nothing within methods doGet & doPost public void doGet(HttpServletRequest req, HttpServletResponse res) { } public void doPost(HttpServletRequest req, HttpServletResponse res) { } }
swarraj kulkarni also contributed to this answer and adds:
You should always check and verify character encoding and Supported Encodings. In addition, the international version of the JDK/JRE is required to have all the encodings available.