web工程乱码的解决方法

两个小测试而已。
1、jsp页面webtest.jsp的代码如下:
 

<html>
    <head>
        <title></title>
    </head>
    <body>
        <form name="" action="GetUserNameServlet" method="POST">
            <input type="text" name="username"/>
            <input type="submit" value="submit"/>
        </form>
        <a href="GetUserNameServlet?username=哈哈">哈哈</a>
    </body>
</html>

servlet的代码如下:

public class GetUserNameServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        System.out.println(request.getParameter("username"));
        
        request.setAttribute("username", request.getParameter("username"));
        
        RequestDispatcher dispatcher = request.getRequestDispatcher("resultjsp.jsp");
        dispatcher.forward(request, response);
        
    }
}

转向到resultjsp.jsp的页面代码如下:

<html>
    <head>
        <title></title>
    </head>
    <body>
        <%=request.getAttribute("username")%>
    </body>
</html>

现在要完成的任务是:在webtest.jsp的文本框中输入汉字和通过点击超级链接都不能出现乱码。

首先,要做一个filter,网上源码很多这里就不列举了,这个filter需要把编码格式转为GB2312。

其次,在两个jsp页面的最顶端加入如下代码:

<%@ page contentType="text/html; charset=GB2312" %>

最后,在tomcat的server.xml文件中找到Connector标签,并添加useBodyEncodingForURI="true"。注:tomcat5.0需要修改,4.0就不用,默认就有这句话。

重启tomcat后,就达到目的了。

2、解决freemarker的乱码问题

依照上面那个例子,只需替换jsp页面为ftl页面,修改方法为:将.jsp改为.ftl,并将页面中

<%@ page contentType="text/html; charset=GB2312" %>

这行代码去掉即可,最后在修改好的resultftl.ftl中,将

<%=request.getAttribute("username")%>

改成

${username}

别忘了把servlet中转发的页面也改了。

修改完上面那些,最后一步就是在web.xml中将引入freemarker的那句编码格式改成GB2312就OK了。

 

附上我忘记从那里摘得转化编码的filter:
 

public class EncodingFilter extends HttpServlet implements Filter {

    private String encoding = null;
    protected FilterConfig filterConfig = null;
    private 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 strencoding = selectEncoding(request);
            if (strencoding != null) {
                request.setCharacterEncoding(strencoding);
            }
        }

        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中这样配置:

<filter>
    <filter-name>EncodingFilter</filter-name>
    <display-name>EncodingFilter</display-name>
    <description>EncodingFilter</description>
    <filter-class>com.webtest.filter.EncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>GB2312</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>EncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值