Causes of various coding problems and solutions---------response encoding, request encoding, URL encoding

Source: Internet
Author: User

Response encoding

Causes and Solutions:

Example:

1  packagecn.yzu;2 Importjava.io.IOException;3 Importjavax.servlet.ServletException;4 Importjavax.servlet.http.HttpServlet;5 Importjavax.servlet.http.HttpServletRequest;6 Importjavax.servlet.http.HttpServletResponse;7  public classAservletextendsHttpServlet {8      public voiddoget (httpservletrequest request, httpservletresponse response)9             throwsservletexception, IOException {TenResponse.setcontenttype ("text/html;charset=utf-8"); oneResponse.getwriter (). println ("zhang san"); aResponse.getwriter (). println ("john doe"); -     } -}
View Code

Run:

Request encoding

Post Request:

GET Request (conf/server.xml):

Example:

index.jsp

1 <%@ Page Language="Java"Import="java.util.*"pageencoding="UTF-8"%>2 <%3 StringPath=Request.getcontextpath ();4 StringBasePath=Request.getscheme ()+"://"+request.getservername ()+":"+Request.getserverport ()+Path+"/";5 %>6 <!DOCTYPE HTML public "-//w3c//dtd HTML 4.01 transitional//en">7 <HTML>8   <Head>9     <Basehref= "<%=basePath%>">Ten     <title>My JSP ' index.jsp ' starting page</title> one     <Metahttp-equiv= "pragma"content= "no-cache"> a     <Metahttp-equiv= "cache-control"content= "no-cache"> -     <Metahttp-equiv= "expires"content= "0">     -     <Metahttp-equiv= "keywords"content= "keyword1,keyword2,keyword3"> the     <Metahttp-equiv= "description"content= "this is my page"> -   </Head> -   <Body> -       <formAction= "aservlet"Method= "post"> +           <inputtype= "text"value= "zhang san"name= "zs"> -           <inputtype= "submit"value= "post request"> +       </form><HR> a       <formAction= "aservlet"Method= "get"> at           <inputtype= "text"value= "john doe"name= "ls"> -           <inputtype= "submit"value= "get request"> -       </form> -   </Body> - </HTML>
View Code

Aservlet

1  packagecn.yzu;2 Importjava.io.IOException;3 Importjavax.servlet.ServletException;4 Importjavax.servlet.http.HttpServlet;5 Importjavax.servlet.http.HttpServletRequest;6 Importjavax.servlet.http.HttpServletResponse;7  public classAservletextendsHttpServlet {8      public voiddoget (httpservletrequest request, httpservletresponse response)9             throwsservletexception, IOException {Ten         //Handling GET Requests oneSystem.out.println (request.getparameter ("ls"));//Do not process aString name = Request.getparameter ("ls");  -         byte[] B = Name.getbytes ("iso-8859-1");// fallback -Name =NewString (b, "utf-8");//re-editing the System.out.println (name); -          -     } -      public voiddoPost (httpservletrequest request, httpservletresponse response) +             throwsservletexception, IOException { -         //Processing Post Requests +Request.setcharacterencoding ("utf-8"); aString username = Request.getparameter ("zs"); at System.out.println (username); -     } -}
View Code

Page:

Output result:

URL encoding:

URL encoding decoding example:

1 Importjava.io.UnsupportedEncodingException;2 Importjava.net.URLDecoder;3 Importjava.net.URLEncoder;4 Importorg.junit.Test;5  public classDemo {6 @Test7      public voidFun1 ()throwsunsupportedencodingexception {8String name = "zhang san";9String s = Urlencoder.encode (name, "UTF-8");//CodingTen System.out.println (s); ones = Urldecoder.decode (s, "utf-8");//decoding a System.out.println (s); -     } -}
View Code

Results:

Causes of various coding problems and solutions---------response encoding, request encoding, URL encoding

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.