我有两个文件如下,在运行的时候出现了乱码:
information.html文件
<html>
<head>
<title>花花公子主页</title>
</head>
<body>
<center><h3>请输入您的信息</h3>
<form name="Form1" method="GET" action="JSPDeal.jsp">
姓名:<input type=text name="Name" ><br>
性别:<select name="Sex">
     <option value="1" selected>男</option>
 <option value"2" > 女</option>
 </select><br>&nbsp;&nbsp;&nbsp;&nbsp;
 <input type=submit name="Submit" value="提交">&nbsp;&nbsp;
 <input type=reset name="Reset" value="重置">
</form>
</center>
</body>
</html>JSPDeal.jsp文件
<%@ page contentType="text/html;charset=GBK" %>
<% 
response.setContentType("text/html;charset=GBK"); 
%> 
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=GBK" />
<title>您的信息</title>
</head>
<body>
<center>
<h3>Data you Posted</H3>
<%
  String name=request.getParameter("Name");
  String sex=request.getParameter("Sex");
%>
<table>
<tr>
<td>用户的姓名:</td>
<td><%=name%></td></tr>
<tr>
<td>用户的性别:</td>
<td><% if(sex.equals("1")) out.print("男");
        else out.print("女");%></td></tr>
</table>
</center>
</body>
</html>
点击提交时,姓名出了乱码!求高手帮忙!

解决方案 »

  1.   

    在你的<title>花花公子主页 </title> 后面加一句 试试看
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
      

  2.   

    method="GET"
    改为
    method="post"
      

  3.   

    information.html文件
    <html>
    <head>
    <title>花花公子主页 </title>
    </head>
    <body>
    <center> <h3>请输入您的信息 </h3>
    <form name="Form1" method="post" action="JSPDeal.jsp">
    姓名: <input type=text name="Name" > <br>
    性别: <select name="Sex">
        <option value="1" selected>男 </option>
    <option value"2" > 女 </option>
    </select> <br>&nbsp;&nbsp;&nbsp;&nbsp;
    <input type=submit name="Submit" value="提交">&nbsp;&nbsp;
    <input type=reset name="Reset" value="重置">
    </form>
    </center>
    </body>
    </html> 
    <%@ page contentType="text/html;charset=GBK" %>
    <%
    response.setContentType("text/html;charset=GBK");
    %>
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html;charset=GBK" />
    <title>您的信息 </title>
    </head>
    <body>
    <center>
    <h3>Data you Posted </H3>
    <%
    request.setCharacterEncoding("GBK");
    System.out.println("aaaaaaaa");
      String name=request.getParameter("Name");
      String sex=request.getParameter("Sex");
    %>
    <table>
    <tr>
    <td>用户的姓名: </td>
    <td> <%=name%> </td> </tr>
    <tr>
    <td>用户的性别: </td>
    <td> <% if(sex.equals("1")) out.print("男");
            else out.print("女");%> </td> </tr>
    </table>
    </center>
    </body>
    </html> 
      

  4.   

    如果有问题的话,先将tomcat下的wrok里的东西清空,然后重启tomcat
      

  5.   


    在JSPDeal.jsp文件 

    <% 
      String name=request.getParameter("Name"); 
      String sex=request.getParameter("Sex"); 
    %> 
    里面改为:<% 
     response.setContentType("text/html;charset=GBK")
      String name=request.getParameter("Name"); 
      String sex=request.getParameter("Sex"); 
    %> 
      

  6.   

    把页面的头部<%@ page contentType="text/html;charset=GBK" %> 
    <% 
    response.setContentType("text/html;charset=GBK"); 
    %>改为:<%@ page language="java"contentType="text/html; charset=gb2312" pageEncoding="GB2312" import="java.sql.*" errorPage="error.jsp" %>
    <%request.setCharacterEncoding("gb2312");%>
      

  7.   

    <% 
      String name=request.getParameter("Name"); 
      String sex=request.getParameter("Sex"); 
    %>改为<%
    String name=new String(request.getParameter("Name").getBytes("iso-8859-1"),"gbk");
    String sex=new String(request.getParameter("Sex").getBytes("iso-8859-1"),"gbk"); %>
      

  8.   

    中文乱码问题:应该说所有的情况都碰到了吧,解决也是曲折的,经过摸索,总结如下: 1、所有页面都用UTF-8。 
    2、写过滤器,设置request.setCharacterEncoding("UTF-8")。 
    3、javascript脚本里用encodeURI(str)。 
    4、适当的时候,在jsp里用java.net.URLDecoder.decode(request.getParameter("str"),"UTF-8")。 
    5、最容易忽略的是使用"":value=" <%=paraFiles%>",一定要有引号。 
    6、new String(content.getBytes("ISO-8859-1"), "GBK");
      

  9.   


    我還添加一点就是在JSP头文件设置字符集编码:
    <%page@ pageEcoding="UTF-8"%>
    个人觉得还是使用过滤器过滤所有请求比较方便!
      

  10.   

    1 编码最好多设置一样的
     
    2
      在请求范围要设置编码问题
      request.setCharacterEncoding("gbk") ; 
      响应范围最好也要设置编码问题
       response.setCharacterEncoding("gbk");  3 可以用监听器filter 不过要配置 package filter ; 
    import javax.servlet.*; public class CharsetFilter implements Filter{ 
    public void destroy() { 

    public void doFilter(ServletRequest request, ServletResponse response, 
    FilterChain chain) throws IOException, ServletException {    
      request.setCharacterEncoding("gbk") ; 
      response.setCharacterEncoding("gbk") ; 
      chain.doFilter(request, response) ; 

    public void init(FilterConfig config) throws ServletException { 


    在web.xml中配置 
      <filter> 
        <filter-name>Charset </filter-name> 
        <filter-class>filter.CharsetFilter </filter-class> 
      </filter> 
      <filter-mapping> 
          <filter-name>Charset </filter-name> 
          <url-pattern>/* </url-pattern> 
      </filter-mapping>