日期:2014-05-17  浏览次数:20664 次

表单提交中文无效+乱码,怎么解决?
login.jsp

Java code

<%@ page  pageEncoding="UTF-8" %>
<%@ page import="bin.*" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>

<body>

<% if(session.getAttribute("loginUser")!=null)
    {
%>

        <jsp:forward page="loginSuccess.jsp"/>
<%
    }
%>    
    <jsp:useBean id="loginForm" class="bin.LoginFormBean" scope="page"/>
    <jsp:setProperty name="loginForm" property="*" />
    
<%
    if(request.getParameter("s")!=null)
    {
        if (loginForm.validate())
        {
          DbUtil db=DbUtil.getInstance();
          UserBean user=db.getUser(loginForm.getName());
          
          if(user==null)
          {
            loginForm.setErrorMsg("name","no this user!");
          }
          
          else
          {
          if(user.validatePassword(loginForm.getPassword()))
          {
          session.setAttribute("loginUser",user);
          
%>
          <jsp:forward page="loginSuccess.jsp"/>
    
<%                
          }
          else
          {
           loginForm.setErrorMsg("password","password error!");
          }
          }
          
        }
    
    }
    
%>





<form  action="login.jsp" method="post" >
  <p>姓名 
    <input type="text" name="name" value="<%=loginForm.getName() %>">
    <font color=red><%=loginForm.getErrorMsg("name")%></font>
  </p>
  <p>密码 
    <input type="password" name="password" value=<%=loginForm.getPassword() %>>
    <font color=red><%=loginForm.getErrorMsg("password") %></font>
    
  </p>
  <p>
    <input type="submit" name="s" value="登录">
  </p>
</form>

</body>
</html>




------解决方案--------------------
搞个字符集过滤器
------解决方案--------------------
这个是web字符转换的问题. 你少了一行代码

<%
request.setCharacterEncoding("GBK");
response.setCharacterEncoding("GBK"):
%>
------解决方案--------------------
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
------解决方案--------------------
有几种方法可以解决乱码:
一:添加一个过滤器
二:在页面上添加一个request.setCharactor("utf-8") ;
三:在传递的时间转码:使用string的getbyts()方法!
------解决方案--------------------
一般都如下:
<%
request.setCharacterEncoding("GBK");
response.setCharacterEncoding("GBK"):
%>
如解决单个字符串乱码问题可以用:
new String(name.getBytes("ISO-8859-1","GBK"))
------解决方案--------------------
为啥jsp页面上有标签<%@ page pageEncoding="UTF-8" %> 这个应该是保持页面的编码
了,还要加<%
request.setCharacterEncoding("utf-8"); 这个是请求时编码的转化 页面传来的有汉字 转换
response.setCharacterEncoding("utf-8"); 这个是 响应的时候编码的转化 数据取出来有汉字 转换
%>

我是这样理解的 源代码 没看过 也不想看

------解决方案--------------------
探讨

response.setContentType("text/html";charset=GB2312);//确保汉字信息的正确编码方式显示
request.setCharacterEncoding(gb2312);确保参数信息以汉字编码方式提取

------解决方案--------------------
探讨
引用: