Importjava.io.IOException;ImportJava.io.PrintWriter;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse; Public classCharSetextendsHttpServlet {/*** Constructor of the object. */ PublicCharSet () {Super(); } /*** Destruction of the servlet. <br>*/ Public voiddestroy () {Super. Destroy ();//Just puts "destroy" string in log//Put Your code here } /*** The Doget method of the servlet. <br> * * This method was called when a form have its tag value me Thod equals to get. * * @paramRequest * The request send by the client to the server *@paramResponse * The response send by the server to the client *@throwsservletexception * If an error occurred *@throwsIOException * If an error occurred*/ Public voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {//String str = request.getparameter ("XH"); //String str2 = new String (Str.getbytes ("iso-8859-1"), "Utf-8"); //System.out.println (str); //System.out.println (STR2); //This is to tell the browser how to follow the code to parse//Response.setheader ("Content-type", "text/html;charset=utf-8"); // //Response.getoutputstream (). Write (Str2.getbytes ("Utf-8")); //response.getwriter (). write (STR2); //tells the servlet to parse by encoding//response.setcharacterencoding ("Utf-8");GETTJ (request, response);//Method 2 } /*** The DoPost method of the servlet. <br> * * This method was called when a form have its tag value Method equals to * post. * * @paramRequest * The request send by the client to the server *@paramResponse * The response send by the server to the client *@throwsservletexception * If an error occurred *@throwsIOException * If an error occurred*/ Public voidDoPost (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {request.setcharacterencoding ("Utf-8"); String str1= Request.getparameter ("XH"); Response.setcontenttype ("Text/html;charset=utf-8"); Response.getwriter (). write (STR1); } /*** Initialization of the servlet. <br> * *@throwsservletexception * If an error occurs*/ Public voidInit ()throwsservletexception {//Put Your code here } Public Static voidGETTJ (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {response.setheader ("Content-type", "Text/html;charset=utf-8"); String str1=request.getquerystring (); String str2= Java.net.URLDecoder.decode (str1, "Utf-8"); String[] Arr= Str2.split ("&"); for(String Str:arr) {string[] str3= Str.split ("="); Response.getwriter (). Write (str3[1]); } }}
View Code
Servlet submits "get post"