我做的是简单的login测试,点提交后,输入的中文变成了乱码,用validation验证的中文能正常显示,page也<%@ page language="java" contentType="text/html; charset=UTF-8" %>这样设定了,还有哪里会出问题呢?

解决方案 »

  1.   

    如果是tomcat的话可以尝试重新编码
    new String(q.getBytes("ISO-8859-1"),"UTF-8");
      

  2.   

    兄弟,这个问题我刚接触的时候也遇到过,如果是request获得的话
    要通过
    request.request.setCharacterEncoding("gb2312");来解决,
    要是页面显示问题 通过 <%page contentType="text/html; charset=gb2312" "%>
    来稿定!
      

  3.   

    上面多了个request 不好意思
      

  4.   

    这是我的Login.jsp代码:<%@ page language="java" contentType="text/html; charset=UTF-8" %>
    <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%> 
    <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
     
    <html> 
    <head>
    <title>JSP for loginForm form</title>
    </head>
    <body>
    <html:form action="/login">
    username : <html:text property="username"/><html:errors property="username"/><br/>
    password : <html:password property="password"/><html:errors property="password"/><br/>
    <html:submit/><html:cancel/>
    </html:form>
    </body>
    </html>
      

  5.   

    这是我LoginAction的代码:package com.test.struts.action;import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.validator.DynaValidatorForm;public class LoginAction extends Action { public ActionForward execute(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response) {
    DynaValidatorForm loginForm = (DynaValidatorForm) form;

    String username=loginForm.getString("username");
    String password=loginForm.getString("password");
    if(username.equals("test")||password.equals("test")){
       return mapping.findForward("indexGo");
    }else{
       return mapping.getInputForward();
    } }}
      

  6.   

    写一个myActionServlet来并覆盖ActionServlet中的process()方法。  protected void process(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException {   
     /**@todo Override this org.apache.struts.action.ActionServlet method*/      request.setCharacterEncoding("GB2312");//就加着一行一切都解决了  
      super.process(request, response); 
     }当然别忘了改一下web.xml里面的配置 
     <servlet>    
    <servlet-name>action</servlet-name>   
     <servlet-class>strutsdemo.myActionServlet</servlet-class> 
       <init-param>    
      <param-name>debug</param-name>   
       <param-value>2</param-value>   
     </init-param>   
     <init-param>    
      <param-name>config</param-name>  
        <param-value>/WEB-INF/struts-config.xml</param-value>  
      </init-param>  
      <load-on-startup>2</load-on-startup> 
     </servlet>改一下servlet-class标签中的内容就可以!
      应该可以的,你试一下!
      

  7.   

    tianming2002() 我在LoginAction里加了
    try{
        request.setCharacterEncoding("UTF-8");
    }catch(Exception e){}但是还是没管用。。
      

  8.   

    自己写一个转换的类
    import java.io.*;public class ToGBK {
        public static String toGBK(String instr){
            String outstr=null;
            if(instr!=null){
                byte[] bytes = null;
                try {
                    bytes = instr.getBytes("ISO8859-1");
                    outstr=new String(bytes,"GBK");
                } catch (UnsupportedEncodingException ex) {
                     System.out.println("ToGBK_Error");
                }
                return outstr;
            }else{
                return null;
            }
        }}
      

  9.   

    然后String username=ToGBK.toGBK(loginForm.getString("username"));
      

  10.   

    to tianzhijie11(抓青蛙) 你的方法我刚才试了,也不行啊
      

  11.   

    用过滤器处理所有的POST提交,设置TOMCAT的ENCODING处理所有GET的提交
    这样才完美啊
      

  12.   

    编码方式指定一下,一般的话用UTF-8(国际化),还有就是设置一下你的应用服务器连接池的characterEncoding和8080端口的URIEncoding,最好是自己再加一个Filter
      

  13.   

    zhangj0571(笨鸟飞飞)应用服务器连接池的characterEncoding
    8080端口的URIEncoding这两个在哪里设定?我是用eclipse的myeclipse插件做的。
      

  14.   

    chq32(无情)怎么设置能说具体点吗
      

  15.   

    要看看你数据库的编码是不是也是utf-8
      

  16.   

    filter也试了,没用!!!继续无奈ING!!!
      

  17.   

    tomcat中解决提交中文乱码问题,针对post 和 get是不同的
    post方式提交需要设置一个Filter,在里面设置request.setCharacterEncoding("gb2312")就可以了。
    get方式提交需要在server.xml中connector后面加入URLEncoding="GBK"
      

  18.   

    我重写了actionservlet的process方法在我的MyActionServlet的名字下提示:
    The serializable class EncodeActionServlet does not declare a static final serialVersionUID field of 
     type long
    这个错误不知道怎么解决 。。
      

  19.   

    还是用了重写process方法,设定字符集!!
      

  20.   

    写个自己的EncodeActionServlet,设定Process()方法:package com.test.struts;import org.apache.struts.action.*;
    import javax.servlet.http.*;
    import javax.servlet.*;
    import java.io.*;public class EncodeActionServlet extends ActionServlet
    {
        protected void process(
            HttpServletRequest request,
            HttpServletResponse response)
            throws IOException, ServletException
        {
            HttpSession session = request.getSession(true);
            if (session.isNew())//session超时
            {
                response.sendRedirect("/login.jsp");
                return;
            }
            request.setCharacterEncoding("utf-8"); //进行统一的中文转码
            super.process(request, response);
        }
    }
    然后在web.xml里加入如下配置:<!-- Action Servlet Configuration -->
      <servlet>
        <servlet-name>actionServlet</servlet-name>
        <servlet-class>com.test.struts.EncodeActionServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>  <!-- Action Servlet Mapping -->
      <servlet-mapping>
        <servlet-name>actionServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
      </servlet-mapping>重启tomcat,就OK了.
      

  21.   

    <%@ page language="java" contentType="text/html; charset=UTF-8" %>
    改成
    <%@ page language="java" contentType="text/html; charset=GBK" %>
    试试?
      

  22.   

    写个filter就不用那么麻烦了