首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

表单提交中文至Servlet乱码,该怎么解决

2012-01-08 
表单提交中文至Servlet乱码环境:NetBeans+JDK1.6源代码:index.jspHTMLHEADTITLE TEST /TITLE/HEA

表单提交中文至Servlet乱码
环境:NetBeans   +   JDK1.6

源代码:
index.jsp

<HTML>
<HEAD>
<TITLE> TEST </TITLE>
</HEAD>
<BODY>
<FORM   METHOD=GET   ACTION= "/Servlet/Hello ">
<INPUT   TYPE=TEXT   NAME= "name "   > <P>
<INPUT   TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>

Hello.JAVA

import   java.io.*;
import   javax.servlet.*;
import   javax.servlet.http.*;

public   class   Hello   extends   HttpServlet   {
       
        public   void   doGet(HttpServletRequest   req,   HttpServletResponse   res)
        throws   ServletException,   IOException   {
               
                res.setContentType( "text/html;charset=UTF-8 ");
                res.setHeader( "Content-language ",   "zh ");
               
                PrintWriter   out   =   res.getWriter();
                //String   name   =   req.getParameter( "name ")

                String   name   =new   String(req.getParameter( "name ").getBytes( "iso-8859-1 "), "UTF-8 ");
               
                out.println( " <HTML> ");
                out.println( " <HEAD> <META   content=text/html;   charset=gb2312   http-equiv=Content-Type> </HEAD> ");
                out.println( " <BODY> ");
                out.println( "你好,   "   +   name);
                out.println( " </BODY> </HTML> ");
        }
       
        public   String   getServletInfo()   {
                return   "A   servlet   that   knows   the   name   of   the   person   to   whom   it 's "   +
                                "saying   hello ";
        }
}

也看了之前不少朋友关于乱码的文章,可是一直没有解决

上面的程序用POST方法则一点问题也没有,用GET方法就是乱码

我是个超级新手,基本上啥也不会,过滤器也不知道怎么加进去~~

还是NETBEANS5.5的傻瓜环境

请教~~



[解决办法]
写一个filter,在其doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 方法中调用request.setCharacterEncoding( "GB2312 ");

package com.forecast.filter;

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;

public class GBKFilter implements Filter
{
public void destroy()
{
// XXX Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException
{
request.setCharacterEncoding( "GBK ");
}
public void init(FilterConfig arg0) throws ServletException
{
// XXX Auto-generated method stub
}
}

然后在web.xml中配置这个这个filter映射为*模式,

<filter>
<filter-name> gbk </filter-name>
<filter-class> com.forecast.filter.GBKFilter </filter-class>
</filter>
<filter-mapping>
<filter-name> gbk </filter-name>
<url-pattern> /* </url-pattern>
</filter-mapping>
[解决办法]
如果是像你所做的那样,可以把“String name =new String(req.getParameter( "name ").getBytes( "iso-8859-1 "), "UTF-8 ");
”这行给去掉,如果你要转换到后台应用,也就是后台要用到表单的数据,可以如下做法:

String name =new String(req.getParameter( "name ").getBytes( "UTF-8 "), "iso-8859-1 ");

String name =new String(req.getParameter( "name ").getBytes( "UTF-8 "), "gbk2316 ");


Thanks
Hima

热点排行