输出中文数据

        
输出中文数据        



        字节流:
                      // 以字节流默认的本地GBK编码输出中文数据,没有乱码  
                      String s = "幸福是神马 ";
                      ServletOutputStream out = response.getOutputStream();
                      out.write(s.getBytes());
                      
                      // 以字节流用UTF-8编码输出中文数据,有乱码  
                      out.write("中文".getBytes("UTF-8"));
                      解决办法:
                              方式一:更改浏览器的查看编码(不可取)      
                                                           
                              注:  方式二、三、四都是一样的,即通知浏览器,使用的码表
                              方式二:response.setHeader("Content-Type", "text/html;charset=UTF-8");
                              方式三:response.getOutputStream().write("<meta http-equiv='Content-Type'           
                                             content='text/html;charset=UTF-8'>".getBytes("UTF-8"));
                             *方式四:response.setContentType("text/html;charset=UTF-8");
                             
                      *****************************************************************************************************************       
                       例:
                              import java.io.IOException;
                              import java.io.UnsupportedEncodingException;
                              import javax.servlet.ServletException;
                              import javax.servlet.ServletOutputStream;
                              import javax.servlet.http.HttpServlet;
                              import javax.servlet.http.HttpServletRequest;
                              import javax.servlet.http.HttpServletResponse;


                              public class ResponseDemo1 extends HttpServlet implements test1 {
                                      public void doGet(HttpServletRequest request, HttpServletResponse response)
                                          throws ServletException, IOException {
                                                  test1(response);
                                      }
                                     
                                      // 以字节流默认的本地GBK编码输出中文数据,没有乱码       
                                      public void test1(HttpServletResponse response) throws IOException {
                                                  String s = "幸福是神马 ";
                                                  ServletOutputStream out = response.getOutputStream();
                                                  out.write(s.getBytes());
                                      }


                                      // 以字节流用UTF-8编码向客户端输出中文数据
                                      public void test2(HttpServletResponse response) throws IOException,
                                          UnsupportedEncodingException {
                                                  String s = "幸福与贫富无关";
                                                  
                                                  // 通知客户端/浏览器查UTF-8码表  
                                                  response.setHeader("Content-Type", "text/html;charset=UTF-8");
                                                  
                                                  ServletOutputStream out = response.getOutputStream();
                                                  out.write(s.getBytes("UTF-8"));
                                      }


                                      // 以字节流用UTF-8编码向客户端输出中文数据
                                      public void test3(HttpServletResponse response) throws IOException,
                                          UnsupportedEncodingException {
                                                  String s = "幸福与内心相连";
                                                  
                                                  // 通知客户端/浏览器查UTF-8码表  
                                                  ServletOutputStream out = response.getOutputStream();
                                                  out.write("<meta http-equiv='Content-Type' content='text/html;chatset=UTF-8'>".getBytes("UTF-8"));
                                                  
                                                  out.write(s.getBytes("UTF-8"));
                                      }
                                      
                                      // 以字节流用UTF-8编码向客户端输出中文数据
                                      public void test4(HttpServletResponse response) throws IOException,
                                          UnsupportedEncodingException {
                                                  String s = "学人要学陈冠希";
                                                  
                                                  // 通知客户端/浏览器查UTF-8码表  
                                                  response.setContentType("text/html;charset=UTF-8");
                                                  
                                                  ServletOutputStream out = response.getOutputStream();
                                                  out.write(s.getBytes("UTF-8"));
                                      }
                                      
                                      public void doPost(HttpServletRequest request, HttpServletResponse response)
                                          throws ServletException, IOException {
                                                  doGet(request, response);
                                      }
                              }                                  
                                      
                     *************************************************************************************************                                            
        字符流:
                    Servlet中的字符流默认查ISO-8859-1(SUN的Servlet规范要求的)
                    更改这个默认的编码的方式: response.setCharacterEncoding("UTF-8");
                    
                    //不要忘记通知浏览器的编码
                    response.setCharacterEncoding("UTF-8");
                    
                    方式1:response.setHeader("Content-Type", "text/html;charset=UTF-8");
                    方式2:response.setContentType("text/html;chatset=UTF-8");
                    
                    PrintWriter out = response.getWriter();
                    out.write(s);
                                                   
                    在字符流输出中文数据时:response.setContentType("text/html;charset=UTF-8");
                        有两个作用:通知字符流以UTF-8编码输出
                                              通知客户端以UTF-8解码显示
                    注:  输出整数,转换成字符串形式,即可,比如98无论查哪个码表都是b                  
                     *****************************************************************************************************************       
                       例:
                              import java.io.IOException;
                              import java.io.PrintWriter;
                              import javax.servlet.ServletException;
                              import javax.servlet.http.HttpServlet;
                              import javax.servlet.http.HttpServletRequest;
                              import javax.servlet.http.HttpServletResponse;


                              public class ResponseDemo2 extends HttpServlet {
                                      public void doGet(HttpServletRequest request, HttpServletResponse response)
                                          throws ServletException, IOException {
                                                  test3(response);
                                      }
                                      
                                      // 注: Servlet中的字符流默认查的是ISO-8859-1码表(SUN的servlet规范要求的)
                                      public void test1(HttpServletResponse response) throws IOException {
                                                  String s = "**要带摄像机";
                                                  response.setCharacterEncoding("UTF-8");
                                                  response.setHeader("Content-Type", "text/html;charset=UTF-8");
                                                  PrintWriter out = response.getWriter();
                                                  out.write(s); 
                                      }
                                      
                                      public void test2(HttpServletResponse response) throws IOException {
                                                  String s = "请问你是逗逼么";
                                                  response.setCharacterEncoding("UTF-8");
                                                  response.setContentType("text/html;chatset=UTF-8");
                                                  PrintWriter out = response.getWriter();
                                                  out.write(s); 
                                      }
                                      
                                      public void test3(HttpServletResponse response) throws IOException {
                                                  String s = "请问你是猴子请来的么?";
                                                response.setCharacterEncoding("UTF-8");  
                                                  response.setContentType("text/html;chatset=UTF-8");
                                                  PrintWriter out = response.getWriter();
                                                  out.write(s); 
                                      }
                                      
                                      public void doPost(HttpServletRequest request, HttpServletResponse response)
                                          throws ServletException, IOException {
                                        doGet(request, response);
                                      }
                              }
                                        
                     *****************************************************************************************************************
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,我可以帮你遍历链表并输出链表数据。以下是一个示例代码: ```python # 定义一个链表节点类 class Node: def __init__(self, data): self.data = data self.next = None # 定义一个链表类 class LinkedList: def __init__(self): self.head = None # 在链表末尾添加新节点 def append(self, data): new_node = Node(data) if not self.head: self.head = new_node return curr_node = self.head while curr_node.next: curr_node = curr_node.next curr_node.next = new_node # 遍历链表并输出节点数据 def traverse(self): curr_node = self.head while curr_node: print(curr_node.data) curr_node = curr_node.next # 创建一个链表并添加节点 my_list = LinkedList() my_list.append(1) my_list.append(2) my_list.append(3) # 遍历链表并输出节点数据 my_list.traverse() ``` 运行以上代码,将输出链表中的数据: ``` 1 2 3 ``` ### 回答2: 假设有一个链表,其中每个节点包含一个数据字段和一个指向下一个节点的指针。我们需要遍历这个链表并输出每个节点的数据。 首先,我们创建一个指针指向链表的头节点。然后,我们使用一个循环来遍历链表,直到整个链表被遍历完成。 在每一次循环中,我们访问当前指针指向的节点,输出该节点的数据。然后,我们将指针指向下一个节点,即指针指向当前节点的下一个节点。我们重复这个过程,直到指针指向空节点,表示遍历已经结束。 下面是一个示例代码: ```python class Node: def __init__(self, data): self.data = data self.next = None def print_linked_list(head): current = head while current is not None: print(current.data) current = current.next # 创建一个链表 node1 = Node('A') node2 = Node('B') node3 = Node('C') node1.next = node2 node2.next = node3 # 遍历链表并输出数据 print_linked_list(node1) ``` 运行以上代码,输出为: ``` A B C ``` 这样,我们就成功地遍历了链表并输出了链表数据。 ### 回答3: 遍历链表是指按顺序访问链表中的每个节点,并输出节点的数据。以下是用300字中文回答如何遍历链表并输出链表数据的过程: 链表是一种线性数据结构,由一系列节点组成。每个节点包含一个数据项和一个指向下一个节点的指针。 要遍历链表并输出数据,首先需要定义一个指向链表头节点的指针。通过这个指针,可以逐个遍历链表中的每个节点。 遍历链表的基本思路是从链表的头部开始,依次访问每个节点,直到达到链表的尾部。为了实现这个过程,可以使用一个循环来迭代访问链表中的每个节点。 具体地,可以定义一个临时指针,初始时指向链表头节点。然后,可以通过循环遍历的方式,依次将临时指针指向下一个节点,直到遍历到链表的尾部。 在循环的每一轮中,可以通过访问当前节点的数据项来输出节点的数据输出可以通过打印到控制台或保存到文件等方式进行。 如果链表中的每个节点的数据项是整数类型,可以直接输出数据项。如果节点的数据项是其他类型,如字符串,可以根据具体需求选择适当的方式输出。 需要注意的是,在遍历链表的过程中,要确保链表不为空。也就是说,在开始遍历之前,要检查链表头指针是否为null,如果为null,则说明链表为空。 综上所述,遍历链表并输出链表数据的步骤如下:定义一个指向链表头节点的指针,通过循环遍历的方式依次访问每个节点,输出节点的数据
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值