各种编码问题产生原因以及解决办法---------响应编码,请求编码,URL编码

 响应编码

产生原因以及解决办法:

示例:

 1 package cn.yzu;
 2 import java.io.IOException;
 3 import javax.servlet.ServletException;
 4 import javax.servlet.http.HttpServlet;
 5 import javax.servlet.http.HttpServletRequest;
 6 import javax.servlet.http.HttpServletResponse;
 7 public class AServlet extends HttpServlet {
 8     public void doGet(HttpServletRequest request, HttpServletResponse response)
 9             throws ServletException, IOException {
10         response.setContentType("text/html;charset=utf-8");
11         response.getWriter().println("张三");
12         response.getWriter().println("李四");
13     }
14 }
View Code

 运行截图:

请求编码

 POST请求:

GET请求(conf/server.xml):

示例:

index.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 7 <html>
 8   <head>
 9     <base href="<%=basePath%>">
10     <title>My JSP 'index.jsp' starting page</title>
11     <meta http-equiv="pragma" content="no-cache">
12     <meta http-equiv="cache-control" content="no-cache">
13     <meta http-equiv="expires" content="0">    
14     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
15     <meta http-equiv="description" content="This is my page">
16   </head>
17   <body>
18       <form action="AServlet" method="post">
19           <input type="text" value="张三" name="zs">
20           <input type="submit" value="post请求">
21       </form><hr>
22       <form action="AServlet" method="get">
23           <input type="text" value="李四" name="ls">
24           <input type="submit" value="get请求">
25       </form>
26   </body>
27 </html>
View Code

 AServlet

 1 package cn.yzu;
 2 import java.io.IOException;
 3 import javax.servlet.ServletException;
 4 import javax.servlet.http.HttpServlet;
 5 import javax.servlet.http.HttpServletRequest;
 6 import javax.servlet.http.HttpServletResponse;
 7 public class AServlet extends HttpServlet {
 8     public void doGet(HttpServletRequest request, HttpServletResponse response)
 9             throws ServletException, IOException {
10         //处理get请求
11         System.out.println(request.getParameter("ls"));    //不处理
12         String name = request.getParameter("ls");        
13         byte[] b = name.getBytes("iso-8859-1");            //回退
14         name = new String(b, "utf-8");                    //重编
15         System.out.println(name);
16         
17     }
18     public void doPost(HttpServletRequest request, HttpServletResponse response)
19             throws ServletException, IOException {
20         //处理post请求
21         request.setCharacterEncoding("utf-8");
22         String username = request.getParameter("zs");
23         System.out.println(username);
24     }
25 }
View Code

页面:

输出结果:

URL编码:

URL编码解码示例:

 1 import java.io.UnsupportedEncodingException;
 2 import java.net.URLDecoder;
 3 import java.net.URLEncoder;
 4 import org.junit.Test;
 5 public class Demo {
 6     @Test
 7     public void fun1() throws UnsupportedEncodingException {
 8         String name = "张三";
 9         String s = URLEncoder.encode(name, "UTF-8");//编码
10         System.out.println(s);
11         s = URLDecoder.decode(s, "utf-8");            //解码
12         System.out.println(s);
13     }
14 }
View Code

结果:

 

posted @ 2016-10-24 11:17  凌晨。。。三点  阅读(675)  评论(0编辑  收藏  举报