• form的method用get导致中文乱码


     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10 <form action="disp.jsp"> <!-- 相当于使用get -->
    11     <input type="text" name="info">
    12     <input type="submit" value="submit">
    13 </form>
    14 </body>
    15 </html>

    display.jsp

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <%@ taglib prefix="c" uri="http://www.mldn.cn/jst/core" %>
     4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     5 <html>
     6 <head>
     7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     8 <title>Insert title here</title>
     9 </head>
    10 <body>
    11 <%=request.getParameter("info") %>
    12 </body>
    13 </html>

    我使用了filter,代码如下:

     1 package org.lxh.filterdemo;
     2 
     3 import java.io.IOException;
     4 import java.nio.charset.Charset;
     5 
     6 import javax.servlet.Filter;
     7 import javax.servlet.FilterChain;
     8 import javax.servlet.FilterConfig;
     9 import javax.servlet.ServletException;
    10 import javax.servlet.ServletRequest;
    11 import javax.servlet.ServletResponse;
    12 import javax.servlet.http.HttpServletRequest;
    13 import javax.servlet.http.HttpServletResponse;
    14 
    15 public class CharacterEncodingFilter implements Filter {
    16     protected String encoding = null; 
    17     protected FilterConfig filterConfig = null; 
    18     protected boolean enable = false; 
    19     public void destroy() { 
    20         this.encoding = null; 
    21         this.filterConfig = null; 
    22     } 
    23     public void doFilter(ServletRequest request, ServletResponse response, 
    24                            FilterChain chain) throws IOException, ServletException { 
    25         if (this.enable) {
    26             String encoding = this.selectEncoding(request); 
    27             if (encoding != null && !encoding.equals("")) { 
    28                 System.out.println("~~" + this + ": request :" + encoding);
    29                 request.setCharacterEncoding(encoding); //Overrides the name of the character encoding used in the body of this request. This method must be called prior to reading request parameters or reading input using getReader().
    30                 // response.setCharacterEncoding(encoding); // 暂时不太清楚
    31             }
    32         }
    33         // Pass control on to the next filter 
    34         chain.doFilter(request, response);
    35         if (this.enable) {
    36             String encoding = this.selectEncoding(request); 
    37             if (encoding != null && !encoding.equals("")) { 
    38                 System.out.println("~~" + this + ": response :" + encoding);
    39                 response.setCharacterEncoding(encoding);
    40             }
    41         }
    42      } 
    43      public void init(FilterConfig filterConfig) throws ServletException { 
    44          this.filterConfig = filterConfig; 
    45          this.encoding = filterConfig.getInitParameter("encoding");
    46          if (!Charset.isSupported(encoding)) {
    47             encoding = null;
    48          }
    49          String enableString = filterConfig.getInitParameter("enable");
    50          if (enableString.equalsIgnoreCase("true")) {
    51             this.enable = true;
    52          } else {
    53             this.enable = false;
    54          }
    55       } 
    56       protected String selectEncoding(ServletRequest request) { 
    57           return (this.encoding); 
    58       } 
    59 }

    filter开启,但是仍然有乱码问题,把method="post"添加上就好了

    主要的问题在于对get和post理解的不透彻:

    http://www.w3schools.com/tags/att_form_method.asp

  • 相关阅读:
    剑指offer-第五章优化时间和空间效率(从1到n的整数中1出现的次数)
    《需求工程--软件建模与分析》读书笔记05
    《需求工程--软件建模与分析》读书笔记04
    软件需求第二次课后作业
    2018年春季个人阅读计划
    软件需求与分析——大二下需会知识点
    《需求工程--软件建模与分析》读书笔记03
    《需求工程--软件建模与分析》读书笔记02
    《需求工程--软件建模与分析》读书笔记01
    学习过程总结及对教师授课给出的意见和建议
  • 原文地址:https://www.cnblogs.com/qrlozte/p/3189674.html
Copyright © 2020-2023  润新知