各位大侠  小弟在JSP页面上面有一个TEXT 
然后我在TEXT里面写入数据
点确定后 提交到ACTION里面
然后在ACTION里面处理添加
从FORM里面获得插入的值但是我页面获取TEXT里面的值的时候是正常的
但是在ACTION里面我在后台通过SYSTEM.OUT.PRINTLN打印出TEXT里面的值就是乱码
而且数据库里面也是乱码请问这是怎么回事啊???
先谢谢了

解决方案 »

  1.   

    由于Java默认的编码方式是UNICODE,所以用中文易出问题,常用解决:
    String s2 = new String(s1.getBytes(“ISO-8859-1”),”GBK”);utf-8解决JSP中文乱码问题,在页面的开始处加:
    <%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%> <%request.setCharacterEncoding("UTF-8");%>如果仍不能解决问题,还需要这样处理一下:
    String msg = request.getParameter("message");
    String str=new String(msg.getBytes("ISO-8859-1"),"UTF-8");out.println(st);
      

  2.   

    可以写一个编码过滤器,统一一下代码,或是在Action上再次转下码,如二楼。
      

  3.   

    把jsp定义为UTF-8个编码
    from 用method='post'的方式提交
    在servlet中
    response.setContentType("text/html;charset=UTF-8");
    request.setCharacterEncoding("UTF-8");
    如果是用get方式提交的话要单独进行处理
      

  4.   


    麻烦在问一下  
    就是我用了
    String s2 = new String(s1.getBytes(“ISO-8859-1”),”GBK”);
    但是报了一个错Unhandled exception type UnsupportedEncodingException好像不支持~这个应该怎么搞啊
      

  5.   

    ISO-8859-1应该改为ISO8859_1吧!
      

  6.   

    String s2 = new String(s1.getBytes(“ISO-8859-1”),”GBK”); 
    这样写是可以的。需要放入try,catch中
      

  7.   

    所有的web程序都有可能存在编码问题,web程序为解决这个问题,提供了一个很好的解决方案--过滤器,它可以对所有的web请求进行编码过滤,统一编码。
    以下是过滤器源码:package com.maca.servlet;
    import java.io.IOException;import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    /**
     * 编码过滤器
     * 
     * @author shobos
     *
     */
    public class SetCharacterEncodingFilter implements Filter {
       
        protected String encoding = null;    protected FilterConfig filterConfig = null;    protected boolean ignore = true;    public void destroy() {        this.encoding = null;
            this.filterConfig = null;    }
        public void doFilter(ServletRequest request, ServletResponse response,
                             FilterChain chain)
    throws IOException, ServletException {        if (ignore || (request.getCharacterEncoding() == null)) {
                String encoding = selectEncoding(request);
                if (encoding != null)
                    request.setCharacterEncoding(encoding);
            }        chain.doFilter(request, response);    }
        public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig;
            this.encoding = filterConfig.getInitParameter("encoding");
            String value = filterConfig.getInitParameter("ignore");
            if (value == null)
                this.ignore = true;
            else if (value.equalsIgnoreCase("true"))
                this.ignore = true;
            else if (value.equalsIgnoreCase("yes"))
                this.ignore = true;
            else
                this.ignore = false;    }
        protected String selectEncoding(ServletRequest request) {        return (this.encoding);    }
    }web.xml的配置文件如下:<?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <filter>
    <filter-name>Encoding</filter-name>
    <filter-class>
    com.maca.servlet.SetCharacterEncodingFilter
    </filter-class>
    <init-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>   <!-- utf-8  设置编码方式 -->
    </init-param>
    </filter>
    <filter-mapping>
    <filter-name>Encoding</filter-name>
    <url-pattern>/*</url-pattern>         <!-- /*表示过滤所有请求 -->
    </filter-mapping>
    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    </web-app>看看以上对楼主有没有帮助。