欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Struts小项目二

程序员文章站 2022-07-01 09:28:27
...
一 需求
利用过滤器解决中文乱码问题
 
二 实现方法
开发一个过滤器,并设置setCharacterEncoding("utf-8")
 
三 实现代码
Myfilter1
package com.cakin.filters;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Myfilter1 extends HttpServlet implements Filter {
    public void doFilter(ServletRequest arg0, ServletResponse arg1,
            FilterChain arg2) throws IOException, ServletException {
        arg0.setCharacterEncoding("utf-8");
        arg2.doFilter(arg0, arg1);
        
    }
    public void init(FilterConfig arg0) throws ServletException {
        // TODO Auto-generated method stub
        
    }
}
 
web.xml重新配置过滤器
<?xml version="1.0" encoding="UTF-8"?>
  <display-name />
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <!-- 配置struts-config.xml -->
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>3</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>3</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <filter>
        <filter-name>Myfilter1</filter-name>
        <filter-class>com.cakin.filters.Myfilter1</filter-class>
  </filter>
  <filter-mapping>
        <filter-name>Myfilter1</filter-name>
        <url-pattern>/*</url-pattern>
  </filter-mapping>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
 
四 测试

 


Struts小项目二
            
    
    博客分类: Struts Struts 
 
  • Struts小项目二
            
    
    博客分类: Struts Struts 
  • 大小: 21 KB
相关标签: Struts